-
Notifications
You must be signed in to change notification settings - Fork 0
/
FuncPy.py
49 lines (42 loc) · 1.22 KB
/
FuncPy.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
from functools import wraps
from typing import Generator, List, Tuple
def _type_guard(callback):
@wraps(callback)
def wrapper(*args, **kwargs):
if any([type(n) == type( tuple() ) or type(n) == type( set() ) or type(n) == range for n in args]):
raise Exception('Those functions are suposed to be used with Lists only. ')
return callback(*args, **kwargs)
return wrapper
@_type_guard
def _head_tail(lst: List) -> Tuple:
return ([lst[0]], lst[1:])
@_type_guard
def head(lst: List) -> List:
if len(lst) > 1:
return _head_tail(lst)[0]
else:
return lst
@_type_guard
def tail(lst: List) -> List:
if len(lst) > 1:
return _head_tail(lst)[1]
else:
return lst
@_type_guard
def reverse(lst: List) -> List:
if len(lst) > 1:
return reverse(tail(lst)) + head(lst)
else:
return lst
@_type_guard
def take(n: int, lst: List) -> List:
if n > len(lst):
return lst
if n == 1:
return head(lst)
return head(lst) + take(n-1, tail(lst))
def generator(fn, start = 0, end = None) -> Generator:
if end != None and fn(start) > end:
return fn(start)
yield fn(start)
yield from generator(fn, fn(start), end)