-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexercises.py
34 lines (30 loc) · 929 Bytes
/
exercises.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
# WARNING: THIS CODE IS ONLY INTENDED AS A GUIDELINE
# FOR THE KIND OF LOGIC NEEDED TO IMPLEMENT exercises.js
# invoke_once and enforce_argument_count don't actually work.
def map(list, mapper):
return [mapper(item) for item in list]
def reduce(list, reducer, initializer=None):
accum_value = initializer
for x in list:
accum_value = reducer(accum_value, x)
return accum_value
# THIS DOES NOT ACTUALLY WORK IN PYTHON
# IT'S ONLY A GUIDELINE FOR THE JAVASCRIPT VERSION
def invoke_once(func):
called = False
def wrapper():
nonlocal called
if called:
return
called = True
func()
return wrapper
# THIS DOES NOT ACTUALLY WORK IN PYTHON
# IT'S ONLY A GUIDELINE FOR THE JAVASCRIPT VERSION
def enforce_argument_count(func, default_value, argument_count):
def wrapper(*args):
if len(args) < argument_count:
return default_value
else:
return func(*args)
return wrapper