Code Monkey home page Code Monkey logo

curry.py's Issues

Write more unit tests

I'm sure there are even more cases where this function fails in its' current form, it'd be great to find them!

Wraps function

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

Allow passing of keyword arguments

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):
        ...

Use default arguments

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 implement
  • add(10, done=True) - more flexible, easy to implement (I like this better)

Add clear explanation of how the curry function works

It would be nice if there was an explanation of how it works for new contributors. Particularly:

  • Why are there two nested functions?
  • Why is the nonlocal keyword being used?

Either via comments or in the README.

Not working with *args

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

Arguments persist between function calls

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!

Remove Executable Bit From `curry.py`

$ 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

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.