Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples in chapter 10 (in Laws) #161

Closed
rpominov opened this issue Sep 8, 2015 · 8 comments
Closed

Examples in chapter 10 (in Laws) #161

rpominov opened this issue Sep 8, 2015 · 8 comments

Comments

@rpominov
Copy link

rpominov commented Sep 8, 2015

Hey, @DrBoolean! I just read chapter 10, it's great as always! Noticed couple of minor issues in examples.

# 1

https://github.com/MostlyAdequate/mostly-adequate-guide/blob/master/ch10.md#laws

var tOfM = compose(Task.of, Maybe.of);
var assemble = _.compose(_.concat, _.concat);

liftA3(assemble, tOfM('Rainy Days'), tOfM(' and Mondays'), tOfM(' always get me down'));
// Task(Maybe(Rainy Days and Mondays always get me down))

I think assemble should be defined as:

var assemble = liftA3(_.concat);

# 2

https://github.com/MostlyAdequate/mostly-adequate-guide/blob/master/ch10.md#interchange

var v = Task.of(_.reverse);
var x = Task.of('Sparklehorse');

v.ap(Task.of(x)) == Task.of(function(f) { return f(x) }).ap(v)

Here var x = Task.of('Sparklehorse'); should be var x = 'Sparklehorse';.

@DrBoolean
Copy link

Thank you! For # 1, I think _.concat will get called prematurely since it partially applies during each ap. You're intuition is right and we could do it with liftA3(curry3(_.concat)), but it works as is. # 2 is totally right, I'll update now. Thanks!

@rpominov
Copy link
Author

rpominov commented Sep 8, 2015

I still don't understand how it will work. _.compose(_.concat, _.concat) creates a curried function that operates on arrays or strings, but liftA3(assemble, tOfM...) will call it with Maybies as arguments. Is that right?

var assemble = liftA3(_.concat) also won't work though. Because _.concat is two arity. So the correct implementation is:

var assemble = liftA3(_.compose(_.concat, _.concat));

Btw, it was pretty tricky to grasp that _.compose(_.concat, _.concat) creates a 3-arity concat :)

@DrBoolean
Copy link

Yeah, that's a really bad example... I'll try to come up with something a little more digestible.

@rpominov
Copy link
Author

I think I finally get it. I didn't know that some Maybe implementations include .concat() and when called call .concat() on the wrapped value. The example should work with a Maybe like that. Although, if I remember correctly, it was stated somewhere in previous chapters that the implementation from Folktale is used in the book. And it doesn't seem to include .concat() https://github.com/folktale/data.maybe/blob/master/lib/maybe.js

And yeah, the example in general seems a little too hard to grasp :)

@salper
Copy link

salper commented Oct 1, 2015

The current definition for #1 in chapter 10 is:

var tOfM = compose(Task.of, Maybe.of);

liftA2(_.concat, tOfM('Rainy Days and Mondays'), tOfM(' always get me down'));
// Task(Maybe(Rainy Days and Mondays always get me down))

But using the book definition of Maybe and data.task, I can make it work using:

const tOfM = compose(Task.of, Maybe.of);

const concatMap = curry((m1, m2) => liftA2(concat, m1, m2));

liftA2(concatMap, tOfM('Rainy Days and Mondays'), tOfM(' always get me down'));
// Task(Maybe(Rainy Days and Mondays always get me down))

Am I missing something ?

@DrBoolean
Copy link

Thanks @salper, yes I really need to get on fixing this. You're definition is a good fix, but I should update to a simpler example anyways.

@salper
Copy link

salper commented Oct 1, 2015

Arf, I could actually have made it point free:

const concatMap = liftA2(concat);

What do you mean by simpler ?

@DrBoolean
Copy link

Instead of liftA2(liftA2(concat)) I should do:

var CTM = Compose.of(Task.of(Maybe.of(x)))
where
Compose.prototype.map = function(f) { return this.map(map(f)) }

Well...on second thought. Maybe I'll just sneak in the extra liftA2

@KtorZ KtorZ closed this as completed in ab1353b Feb 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants