digitalmars.D - Currying and composition
- bearophile (62/63) Jul 27 2014 In std.functional there is a curry(), that looks more like a
- Matt Soucy (11/80) Jul 27 2014 So, in the next release std.functional.curry has been renamed to std.fun...
- bearophile (11/15) Jul 27 2014 Is its syntax usage similar to the one I've shown for Python?
- Matt Soucy (7/26) Jul 27 2014 Mine didn't get very far, but the goal was to have that behavior
- bearophile (6/11) Jul 27 2014 If you compose (multiply) a "curried" with a regular or a regular
- Matt Soucy (5/15) Jul 27 2014 Try to compose a regular and a regular. If you expect them to all work t...
In std.functional there is a curry(), that looks more like a partial application. In the Python land I've recently seen a library for a more functional style of coding: https://pypi.python.org/pypi/PyMonad/ I don't like for Python most of the stuff in that PyMonad library, but two bits look nice. I quote them here: << curry def add(x, y): return x + y curry def func(x, y, z): ... The above fuctions can be partially applied by passing them less than their full set of arguments: expected. taking one argument. last argument. last two arguments. again, then applying the l Curried functions can be composed with the * operator. Functions are applied from right to left: curry def head(aList): return aList[0] curry def tail(aList): return aList[1:] its result passed to 'head' You can also compose partially applied functions: curry def add(x, y): return x + y curry def mul(x, y): return x * y it's result passed to 'add(7)'Perhaps I'd like in Phobos a "curry" (or another name) template that returns a callable struct that allows both those operations: the currying with arbitrary partial application and the "*" operator to perform function composition (I think the two pieces of functionality go well together despite being different things, because they are both used in a very functional style of coding). Bye, bearophile
Jul 27 2014
On 07/27/2014 10:48 AM, bearophile wrote:In std.functional there is a curry(), that looks more like a partial application. In the Python land I've recently seen a library for a more functional style of coding: https://pypi.python.org/pypi/PyMonad/ I don't like for Python most of the stuff in that PyMonad library, but two bits look nice. I quote them here: << curry def add(x, y): return x + y curry def func(x, y, z): ... The above fuctions can be partially applied by passing them less than their full set of arguments: argument. arguments. applying the l Curried functions can be composed with the * operator. Functions are applied from right to left: curry def head(aList): return aList[0] curry def tail(aList): return aList[1:] passed to 'head' You can also compose partially applied functions: curry def add(x, y): return x + y curry def mul(x, y): return x * y passed to 'add(7)'So, in the next release std.functional.curry has been renamed to std.functional.partial: https://github.com/D-Programming-Language/phobos/pull/1979 I was starting to work on a proper curry replacement, but due to some real-life stuff I haven't had time. There's some starting code at https://issues.dlang.org/show_bug.cgi?id=4391 that I was basing mine off of, but I was running into some issues involving static, and a ton of issues with templated functions. This might be something worth looking into? -- Matt Soucy http://msoucy.me/ -- Matt Soucy http://msoucy.me/Perhaps I'd like in Phobos a "curry" (or another name) template that returns a callable struct that allows both those operations: the currying with arbitrary partial application and the "*" operator to perform function composition (I think the two pieces of functionality go well together despite being different things, because they are both used in a very functional style of coding). Bye, bearophile
Jul 27 2014
Matt Soucy:So, in the next release std.functional.curry has been renamed to std.functional.partial: https://github.com/D-Programming-Language/phobos/pull/1979Oh, good. (And the "sigh" by Andrei is cute).I was starting to work on a proper curry replacement,Is its syntax usage similar to the one I've shown for Python? func(1, 2, 3) func(1, 2)(3) func(1)(2, 3) func(1)(2)(3) Is it a good idea to also mix in the function composition operator overloading? Bye, bearophile
Jul 27 2014
On 07/27/2014 12:09 PM, bearophile wrote:Matt Soucy:I was hoping that was a "sigh, alright I guess this is something we sould do"So, in the next release std.functional.curry has been renamed to std.functional.partial: https://github.com/D-Programming-Language/phobos/pull/1979Oh, good. (And the "sigh" by Andrei is cute).Mine didn't get very far, but the goal was to have that behaviorI was starting to work on a proper curry replacement,Is its syntax usage similar to the one I've shown for Python? func(1, 2, 3) func(1, 2)(3) func(1)(2, 3) func(1)(2)(3)Is it a good idea to also mix in the function composition operator overloading?I'm not so sure about that one - mainly because then it's possible with some functions (the curried ones) but not all (including "regular" and delegates). -- Matt Soucy http://msoucy.me/
Jul 27 2014
Matt Soucy:If you compose (multiply) a "curried" with a regular or a regular with a "curried", you produce a "curried". Is this a problem and not enough? Bye, bearophileIs it a good idea to also mix in the function composition operator overloading?I'm not so sure about that one - mainly because then it's possible with some functions (the curried ones) but not all (including "regular" and delegates).
Jul 27 2014
On 07/27/2014 04:59 PM, bearophile wrote:Matt Soucy:Try to compose a regular and a regular. If you expect them to all work the same, you're in for a surprise. -- Matt Soucy http://msoucy.me/If you compose (multiply) a "curried" with a regular or a regular with a "curried", you produce a "curried". Is this a problem and not enough? Bye, bearophileIs it a good idea to also mix in the function composition operator overloading?I'm not so sure about that one - mainly because then it's possible with some functions (the curried ones) but not all (including "regular" and delegates).
Jul 27 2014