christianscott / curry.py Goto Github PK
View Code? Open in Web Editor NEW:curry: Utility for currying functions
License: MIT License
:curry: Utility for currying functions
License: MIT License
As described by Alex Martelli here
I'm sure there are even more cases where this function fails in its' current form, it'd be great to find them!
You could use the wraps function, it already does everything you need when you are working with decorator.
from functools import wraps
def curry(func):
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
There's not a whole lot left to do for the curry function. I'm open to suggestions!
This will require some pretty big changes to the way the function works. Will need to seperate the signature(fun).parameters
list into two: args and kwargs.
The kwargs could be stored in a dict, then could check if the dict has enough keys of the right type before calling the function.
Not sure how this would interact with default values.
Something like this:
def curried(*args, **kwargs):
assert only_one_has_value(args, kwargs)
if kwargs:
# set value
if len(arguments) == arg_count or all_keys_set(kwarguments, needed_keys):
...
Currently there's no way to use default arguments. For example:
@curry
def add(a, b=10):
return a + b
x = add(10) # sometimes will want this to be 20, but *always* returns another function
Will need to think of some way to specify that this is the behaviour we want. There are two ways that I can imagine:
@curry(use_default=True)
- less flexible, harder to implementadd(10, done=True)
- more flexible, easy to implement (I like this better)It would be nice if there was an explanation of how it works for new contributors. Particularly:
nonlocal
keyword being used?Either via comments or in the README.
Here's an example:
>>> @curry
... def prefix_all_with(prefix, *words):
... return [prefix + word for word in words]
...
>>> un_ify_all = prefix_all_with("un")
>>> un_ify_all("cool", "fun")
<function prefix_all_with at 0x10d7039d8>
>>> un_ify_all("cool", "fun")()
<function prefix_all_with at 0x10d703ae8>
>>> un_ify_all("cool", "fun")()()
<function prefix_all_with at 0x10d7039d8>
>>>
I imagine this has something to do with have_enough_args
The args
object is created when the function is curried. That means that the arguments keep getting added each time the function is called, oops...
If we add a print statement to the curried
function, this is what is printed out:
>>> add(1)(2)
[]
[1]
3
>>> add(1)(2)
[1, 2]
[1, 2, 1]
Thanks to @praveensvsrk for pointing this out!
$ git clone https://github.com/chrfrasco/curry.py.git
$ ls -l curry.py/curry.py
-rwxr-xr-x 1 bew bew 1640 Oct 11 13:32 curry.py/curry.py*
curry.py
isn't a script, so I think it shouldn't be executable.
Doesn't work on single argument functions (precisely, single call)
>>> increment = curry(lambda x: x + 1)
<function <lambda> at 0x030F90C0>
>>> increment(3) # curried is returned here, a side effect of using the two level nesting
<function <lambda> at 0x030EA540>
>>> increment(3)() #had to explicitly call curried
4
Also, this:
>>> add3 = curry(lambda a, b, c: a + b + c)
>>> add3(1,2)(3) # works as expected
6
>>> add3(1,2,3) # supplied all the arguments in one go, returns curried
<function <lambda> at 0x030EA660>
>>> add3(1,2,3)() # explicit call
6
Possible fix:
In the curried_factory
function, first check if we already have the required arguments. If yes, call the function, otherwise return curried
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.