folktale / data.task

Migrating to https://github.com/origamitower/folktale
MIT License
425 stars 45 forks source link

fold is bimap #41

Open puffnfresh opened 6 years ago

puffnfresh commented 6 years ago

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 commented 6 years ago

Yeah, that was removed in the new version :)

I should update the documentation to note that here though.

Avaq commented 6 years ago

@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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

Avaq commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

@robotlolita thank you for the explanation!