News for hackers. Stuff that matters.
Re:Curry flavored promises by lhorie on Monday April 28, 2014 @09:44AM (#7659869)
Hi author here.

Thanks for the feedback! Just wanted to clarify some points:

I should probably have noted that the usage of console.log is illustrative and is typically only used for debugging (the idea being that console.log could be used in Firefox, or you could wrap it in a log helper function, or use alert or whatever).

Typically, in a Mithril app, you'd either assign the promise to a variable or bind to an m.prop at the end of the chain, as per the docs on web services ( http://lhorie.github.io/mithril/web-services.html )

Regarding console.log returning undefined: Mithril does deviate from A+ promises (again, as per the docs - http://lhorie.github.io/mithril/mithril.deferred.html ). The reason for the spec deviation is precisely to allow the use case of mixing procedural impurity into a chain (whereas the undefined case covered by the spec isn't really useful, given that undefined is the lack of a value, as opposed to null, which is a value with no meaning).

@anaphor: huh, reading about currying again, you're right. Little misunderstanding on my part. Sorry about that.

Re:Curry flavored promises by anaphor on Monday April 28, 2014 @04:00PM (#7662547)
That's okay, I've seen people with PhDs confuse them before, it's a common mistake, and currying gets you partial application "for free". Good article nevertheless!
Re:Curry flavored promises by anaphor on Sunday April 27, 2014 @11:13PM (#7658044)
>Currying is a functional programming technique that lets us pre-define arguments in a function

No it isn't, that's partial application. Currying is (lambda (a b c) ...) => (lambda (a) (lambda (b) (lambda (c) ...))) generalized to any number of parameters.

Re:Curry flavored promises by gberger on Sunday April 27, 2014 @10:24PM (#7657930)

    //here's an AJAX request and a chain of promises
    m.request({method: "GET", url: "/api/projects"})
        .then(console.log) // log all projects
        .then(pastItems) // filter past projects
        .then(console.log) // log past projects only
The calls to console.log will not work.

You need to do this:

    .then(function() { console.log(arguments); })

Or this:

    .then(console.log.bind(console))
Re:Curry flavored promises by gonzoisme on Sunday April 27, 2014 @10:40PM (#7657967)
Yeah, the example couldn't work. Also, isn't this an example of partial application, not currying?
Re:Curry flavored promises by ephemeralgomi on Sunday April 27, 2014 @10:53PM (#7658001)
Yup.
Re:Curry flavored promises by judk on Sunday April 27, 2014 @10:55PM (#7658005)
Funny this mistake rarely goes the other direction. Probably because 'curry' is a fin word.

I propose 'parry' for "partial application", to satisfy the punsters without compromising accuracy.

Re:Curry flavored promises by cwmma on Monday April 28, 2014 @08:47AM (#7659513)
this

    console.log.bind(console)
doesn't work in old IE you need to do

    Function.prototype.bind.call(console.log, console)
Re:Curry flavored promises by _ZeD_ on Sunday April 27, 2014 @10:47PM (#7657985)
well, it depends on the console.log implementation :)

On firefox I get

    >>> console.log.call(undefined, "Hi")
    Hi
Re:Curry flavored promises by bendrucker on Monday April 28, 2014 @08:06AM (#7659377)
console.log is bound in Firefox and in Node. In all other browsers calling console.log.call(null) is a TypeError.
Re:Curry flavored promises by bilalq on Monday April 28, 2014 @02:47AM (#7658550)
Do these promises work differently from most other implementations? Just sticking a `.then(console.log)` seems like it would yield `undefined` to the next function in the promise chain, no?
Re:Curry flavored promises by esailija on Monday April 28, 2014 @02:53AM (#7658574)
Maybe the code was untested or only tested in firefox or node, where `console.log` is bound (which is sane default for singletons if you ask me)
Re:Curry flavored promises by bendrucker on Monday April 28, 2014 @08:04AM (#7659373)
It's true that console.log is bound in FF and Node but that s not the issue here. Passing console.log to then will cause the next handler to receive the return value of the console.log call, i.e. undefined.
Re:Curry flavored promises by esailija on Monday April 28, 2014 @08:16AM (#7659406)
In mithril promises if you return undefined from then, the promise will fulfill with the previous value, I.E. it behaves like .tap in other promise libraries (because doing this in a .then like mithril does violates the spec - which is evidently surprising)

However, with unbound console.log the code doesn't work at all, this is the case in Chrome for example.

Re:Curry flavored promises by bendrucker on Monday April 28, 2014 @12:23PM (#7660961)
The breaks from A+ are pretty serious, both the treatment of undefined and releasing zalgo (http://blog.izs.me/post/59142742143/designing-apis-for-async...) and calling it a feature. Not on the hunt for a client side micro-framework but since I use a lot of modules that supply promises there's no way I'd use this.
Re:Curry flavored promises by phpnode on Monday April 28, 2014 @04:41AM (#7658867)
agree, this sounds like a bug, console.log() returns undefined so that's what the next promise in the chain should receive.
Re:Curry flavored promises by reddyonrails on Monday April 28, 2014 @12:38AM (#7658232)
total_sum = total.curry

sum_of_cubes = total_sum.(lambda { |x| x^3} )

sum_of_cubes.(1,2)

In ruby way