-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
39 lines (29 loc) · 721 Bytes
/
example.py
File metadata and controls
39 lines (29 loc) · 721 Bytes
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
from pyfun import *
from operator import *
# Lets pre wrap some standard functions
mul = Fn(mul)
add = Fn(add)
reduce = Fn(reduce)
range = Fn(range)
# Curry a function
add2 = add % 2
# Compose a function
add4 = add2 | add2
# Compose a function
add6 = add2 | add2 | add2
# factorial example
fac = reduce % mul | range % 1 | add % 1
# > fac(5)
# 120
# the above example is the same as:
# def fac(x):
# return reduce(mul,range(1,x+1),1)
def part(ls):
return (ls[0],ls[1:])
# Quicksort example using Fn as a decorator.
@Fn
def qsort(ls):
if ls == []: return []
else:
(x,xs) = part(ls)
return qsort (filter(Fn(lt) % x, xs)) + [x] + qsort (filter (Fn(ge) % x, xs))