Coverage for src / pyfplib / functions.py: 21%
41 statements
« prev ^ index » next coverage.py v7.13.2, created at 2026-02-04 23:48 +0300
« prev ^ index » next coverage.py v7.13.2, created at 2026-02-04 23:48 +0300
1from typing import Any, Callable, Iterable, Sequence, Union
3from pyfplib.option import Nothing, Option, Some
6def for_each(callback: Callable[[Any], None], iterable: Iterable[Any]):
7 """Applies the given callback for each elements of the given iterable object
9 Args:
10 callback: callback to execute with elements
11 iterable: provides elements to apply the callback
12 """
13 for item in iterable:
14 callback(item)
17def any_of(callback: Callable[[Any], bool], iterable: Iterable[Any]) -> bool:
18 ret_value = False
19 for item in iterable:
20 ret_value = callback(item)
21 if ret_value:
22 break
23 return ret_value
26def all_of(callback: Callable[[Any], bool], iterable: Iterable[Any]) -> bool:
27 ret_value = True
28 for item in iterable:
29 ret_value = callback(item)
30 if not ret_value:
31 break
32 return ret_value
35def none_of(callback: Callable[[Any], bool], iterable: Iterable[Any]) -> bool:
36 ret_value = True
37 for item in iterable:
38 ret_value = not callback(item)
39 if not ret_value:
40 break
41 return ret_value
44def fold(
45 callback: Callable[[Any, Any], Any],
46 iterable: Iterable[Any],
47 first: Union[Any, None] = None,
48) -> Any:
49 ret_value = first
50 for item in iterable:
51 ret_value = callback(ret_value, item)
52 return ret_value
55def head(iterable: Iterable[Any]) -> Option[Any]:
56 return Some(iterable[0]) if len(iterable) else Nothing()
59def tail(sequence: Sequence[str]) -> Sequence[str]:
60 return sequence[1:] if len(sequence) else sequence
63def last(iterable: Iterable[Any]) -> Option[Any]:
64 return Nothing() if len(iterable) == 0 else Some(iterable[-1])
67def is_empty(iterable: Iterable[Any]) -> bool:
68 return len(iterable) == 0
71def is_not_empty(iterable: Iterable[Any]) -> bool:
72 return len(iterable) != 0