User loginNavigation |
Currying in non-curried languagesThere are a lot of currying-in-X tutorials floating around, for example I've written my own for Javascript and PHP. Recently this one appeared on Hacker News and I got involved in the discussion, which made me think a bit harder about a peculiarity of my implementations which I've not seen in anyone else's. My blog post on PHP discusses this peculiarity in the "Returning Functions" section. My Javascript implementation linked above was written before this discovery, but I've detailed it in this followup. The idea with all implementations is that we make a 'curry' function such that the following hold:
The difference in my implementations is what happens when we're given more arguments than the function's arity:
Two consequences of this are:
My current implementations either pass all remaining arguments to the first return value (PHP) or pass one argument at a time to successive return values (Javascript; essentially "foldl ($) f args"). These work best when the returned functions are also curried, but there's no reason we can't curry them on-the-fly, or look up how many to supply and loop until we run out. We could even use such a loop to execute tail-calls while we're at it ;) This uncurrying is effectively the same as the convenience functionality shown in the first code block, but extended to our return values as well as our arguments. In other words:
I'd love to hear people's opinions on this. Is my approach better, worse or complementary to the standard approach? Has this duality been noticed before? Is this just a reason not to use n-ary functions? ;) Edit: In case you can't tell, my aim is to have expressions like "a(b, c, d, e, f, g, ...)" behave like a Haskell expression "a b c d e f g ...". By Chris Warburton at 2014-05-07 23:23 | LtU Forum | previous forum topic | next forum topic | other blogs | 3482 reads
|
Browse archives
Active forum topics |
Recent comments
23 weeks 2 days ago
23 weeks 3 days ago
23 weeks 3 days ago
45 weeks 4 days ago
49 weeks 6 days ago
51 weeks 3 days ago
51 weeks 3 days ago
1 year 2 weeks ago
1 year 6 weeks ago
1 year 6 weeks ago