Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

fold is bimap #41

Open
puffnfresh opened this issue Oct 25, 2017 · 9 comments
Open

fold is bimap #41

puffnfresh opened this issue Oct 25, 2017 · 9 comments

Comments

@puffnfresh
Copy link

I got a question about fold, so I took a look at the signature:

@Task(α, β) => (α → γ), (β → γ) → Task(δ, γ)

This is not a fold, this is bimap.

@robotlolita
Copy link
Member

Yeah, that was removed in the new version :)

I should update the documentation to note that here though.

@Avaq
Copy link

Avaq commented Oct 26, 2017

@puffnfresh it differs a little bit from bimap in that this fold always returns a "resolved" task. You could use the following to convert a Task a b to Task c (Either a b), for example:

task.fold(Left, Right) 

@Avaq
Copy link

Avaq commented Oct 26, 2017

Another way to view fold is (I believe) that it's similar to a proper fold, except that we cannot force the asynchronous operation to return synchronously, therefore we must return in a new Task.

@Avaq
Copy link

Avaq commented Oct 26, 2017

And for the sake of clarity, let's view the signatures side-by-side:

fold  :: Future a b ~> (a -> c, b -> c) -> Future d c
bimap :: Future a b ~> (a -> c, b -> d) -> Future c d

@puffnfresh
Copy link
Author

@Avaq I missed that detail, thank you. It's still not fold, but it's also not bimap.

@Avaq
Copy link

Avaq commented Oct 26, 2017

It's still not fold

I know. :(

It's what I was referring to when I said similar to a proper fold. I'm open to rename the function in Fluture for the sake of correctness, but I do find that fold captures the idea of the function quite nicely.

If Fantasy Land were to adopt a spec that uses the fold name, I will definitely do the rename, just to avoid confusion.

@ivan-demchenko
Copy link

ivan-demchenko commented Oct 26, 2017

fold :: Future a b ~> (a -> c, b -> c) -> Future d c

if I'm allowed to ask an unrelated question, what is d in this signature? A unit, I suppose?

@robotlolita
Copy link
Member

Folktale 2's task only has willMatchWith. No way of writing fold without deep coroutines/fibers or threads :(

@raqystyle an arbitrary type variable. There it just means it's not the same type as a, but it doesn't have any equivalent runtime set of values (it's not really unit or null). In that sense, it's closer to TypeScript's never.

@ivan-demchenko
Copy link

@robotlolita thank you for the explanation!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants