Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

davekch/pycurry

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
6 Commits
 
 
 
 
 
 

Repository files navigation

PyCurry — Currying functions in Python

You want to use PyCurry if you

  • think that currying is really cool
  • want to force a function to only take arguments of certain types

To curry a function in Python using this module, you can decorate it with the curry-decorator. curry takes as arguments the types that the curried function's arguments should have. If you later call the function with different types, it will raise a TypeError. If you don't care about the types, you can use pycurry.Any as a placeholder for an arbitrary type.

Check this out:

from pycurry import curry

@curry(int, int, int)
def f(x,y,z):
  return x+y+z

f(1,2,3)    # => 6
f(1,2)(3)   # => also 6
f(1)(2,3)   # => also 6
f(1)(2)(3)  # => also 6

g = f(3)
g(2,1)  # => 6
h = g(2)
h(1)  # => 6

g("haha")  # => TypeError: in f: expected <class 'int'>, got <class 'str'>

Another example:

from pycurry import Any

@curry(int,Any)
def mult(x,y):
  return x*y

times3 = mult(3)
times3("haha ")  # => "haha haha haha "
times3(4)  # => 12

Why would I ever want to do this?

This is useful when you have a general function and you need to generate more special versions of it on the fly, for example to pass them to other functions like map or filter.

Real life scenario:

@curry(int, Any, Any)
def myfunc(x, m,t):
  return t + m*x

list(map(myfunc(3,"!"), ["I'm hungry", "I need sleep", "I love functional programming"]))
# => ["I'm hungry!!!", 'I need sleep!!!', 'I love functional programming!!!']

This software comes with no warranty.

About

Currying functions in Python

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.