-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfunc.py
51 lines (38 loc) · 1.18 KB
/
func.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Jonathan Reem
January 2014
Functional programming utility functions.
"""
# pylint: disable=C0103, W0142, W0622
from infix import Infix
from collections import namedtuple
Unit = namedtuple("Unit", "")
def curry(func, args):
"Curries a function."
return lambda *a: func(args + a)
def const(first, _):
"const from the functional paradigm"
return first
def id(x):
"id from the functional paradigm"
return x
def flip(func):
"Flips function arguments."
return lambda a, b: func(b, a)
def foldl(helper, acc, itr):
"foldl from Haskell Prelude, but optimized to a loop."
return reduce(helper, itr, acc)
def foldr(helper, acc, itr):
"foldr from Haskell Prelude, but optimized to a loop."
return foldl(lambda x, y: helper(y, x), acc, list(reversed(itr)))
def replicate(item, replications):
"replicate from the Haskell Prelude"
return [item for _ in xrange(replications)]
c = Infix(curry)
c.__doc__ = "infix version of curry"
def unzip(pair_list):
"Undoes zip."
return zip(*pair_list)
def zip_with(function, lefts, rights):
"Generalize zip to non-tuple functions."
return [function(left, right) for left, right in zip(lefts, rights)]