Predicates
|
Returns its argument as it is. |
|
if only |
|
if only |
|
if only |
|
if only |
|
if only |
|
if only |
|
Takes in a predicate or a Boolean expression and returns a negated version of the predicate or expression. |
|
Returns true when num is even. |
|
Returns true when num is odd |
|
Returns true if dividend is divisible by divisor. |
|
Takes a |
|
Return True if |
|
Everything that is NOT an iterable(except strings) are considered atoms. |
|
Checks whether collection contains the given entity. |
|
Returns true if the collection is empty. |
|
returns true if a collection is nested. |
|
Takes a set of predicates and returns a function that takes an entity and checks if it satisfies all the predicates. |
|
Takes a set of predicates and returns a function that takes an entity and checks if it satisfies some of the predicates. |
- functionali.identity(x)
Returns its argument as it is.
- functionali.equals(a, b=None, *args)
if only
a
is passed, a function is returned that returns True when thearg
passed to it is equal toa
; else returns True whena
,``b`` and*args
are equal.with one argument
>>> equals_one = equals(1) >>> equals_one(1) True >>> equals_one(2) False
with two or more arguments
>>> equals(1,1,1) True >>> equals(1,1,2) False
Added in version: 0.1.0
- functionali.is_(a, b=None, *args)
if only
a
is passed, a function is returned that returns True when thearg
passed is the same object asa
; else returns True whena
,``b`` and*args
are.with one argument
>>> d1 = {1,2,3} >>> d2 = {1,2,3} >>> is_d1 = is_(d1) >>> is_d1(d2) >>> False >>> d1 == d2 >>> True
with two or more arguments
>>> is_(d1,d1) >>> True >>> is_(d1,d1,d2) >>> False
Added in version: 0.1.0
- functionali.less_than(a, b=None, *args)
if only
a
is passed, a function is returned that returns True when thearg
passed to is less thana
; else returns True whena
is less than``b`` and*args
.with one argument
>>> less_than_one = less_than(1) >>> less_than_one(2) False >>> less_than_one(0) True
with two or more arguments
>>> less_than(1,2) >>> True >>> less_than(1,2,3) True >>> less_than(1,2,3,1) False
Useful to use with filter
>>> list(filter(less_than(5),range(10))) [0,1,2,3,4]
Added in version: 0.1.0
- functionali.greater_than(a, b=None, *args)
if only
a
is passed, a function is returned that returns True when thearg
passed to is greater thana
; else returns True whena
is greater than``b`` and*args
.with one argument
>>> greater_than_one = greater_than(1) >>> greater_than_one(2) True >>> greater_than_one(0) False
with two or more arguments
>>> greater_than(2,1) >>> True >>> greater_than(3,2,1) True >>> greater_than(3,2,1,3) False
Useful to use with filter
>>> list(filter(greater_than(5),range(10))) [6,7,8,9]
Added in version: 0.1.0
- functionali.less_than_eq(a, b=None, *args)
if only
a
is passed, a function is returned that returns True when thearg
less than or equal toa
; else returns True whena
is less than or equal tob
and*args
.with one argument
>>> less_than_or_eq_to_one = less_than_eq(1) >>> less_than_or_eq_to_one(2) False >>> less_than_or_eq_to_one(1) True
with two or more arguments
>>> less_than_eq(1,2) >>> True >>> less_than_eq(1,2,3) True >>> less_than_eq(1,2,3,1) True
Useful to use with filter
>>> list(filter(less_than_eq(5),range(10))) [0,1,2,3,4,5]
Added in version: 0.1.0
- functionali.greater_than_eq(a, b=None, *args)
if only
a
is passed, a function is returned that returns True when thearg
greater than or equal toa
; else returns True whena
is greater than or equal tob
and*args
.with one argument
>>> greater_than_eq_one = greater_than_eq(1) >>> greater_than_eq_one(2) True >>> greater_than_eq_one(1) True
with two or more arguments
>>> greater_than_eq(2,1) >>> True >>> greater_than_eq(3,2,1) True >>> greater_than_eq(3,2,1,3) True
Useful to use with filter
>>> list(filter(greater_than_eq(5),range(10))) [5,6,7,8,9]
Added in version: 0.1.0
- functionali.complement(expr: Union[bool, Callable[[Any], bool]]) Union[bool, Callable[[Any], bool]]
Takes in a predicate or a Boolean expression and returns a negated version of the predicate or expression.
>>> complement(True) >>> False
>>> def fn(el): # returns the Boolean of el return bool(el) >>> negated_fn = complement(fn) >>> fn(1) >>> True >>> negated_fn(1) >>> False
Added in version: 0.1.0
- functionali.is_even(num: int) bool
Returns true when num is even.
Added in version: 0.1.0
- functionali.is_odd(num: int) bool
Returns true when num is odd
Added in version: 0.1.0
- functionali.is_divisible(divident: Union[int, float], divisor: Union[int, float]) bool
Returns true if dividend is divisible by divisor.
Added in version: 0.1.0
- functionali.is_divisible_by(divisor: Union[int, float]) Callable[[Union[int, float]], bool]
Takes a
divisor
And returns a function (closure) That expects a dividend. returns true if it passes the divisibility test. for e.g.>>> f = is_divisible_by(5) >>> f(10) True >>> f(7) False
This is particularly useful to use with a filter.
>>> list(filter(is_divisible_by(5), [1,2,3,4,5,6,7,8,9,10])) [5, 10]
Suppose you want to filter out numbers that are divisible by 2 or 3
>>> list(filter(some_predicates([is_divisible_by(2), is_divisible_by(3)]), range(1, 10))) [2, 3, 4, 6, 8, 9, 10]
Added in version: 0.1.0
- functionali.is_numeric(entity: Any) bool
Return True if
entity
Is anint
,float
, or acomplex
.Added in version: 0.1.0
- functionali.is_atom(entity: Any) bool
Everything that is NOT an iterable(except strings) are considered atoms.
>>> is_atom("plain string") True >>> is_atom(1) True >>> is_atom([1, 2]) False
Added in version: 0.1.0
- functionali.contains(entity: Any, collection: Iterable) bool
Checks whether collection contains the given entity. Note, won’t automatically convert a tuple of keys and values to a
dict
.Added in version: 0.1.0
- functionali.is_empty(collection: Iterable) bool
Returns true if the collection is empty.
Added in version: 0.1.0
- functionali.is_nested(collection: Iterable) bool
returns true if a collection is nested. Added in version: 0.1.0
- functionali.all_predicates(*predicates: Callable[[Any], bool]) Callable[[Any], bool]
Takes a set of predicates and returns a function that takes an entity and checks if it satisfies all the predicates.
>>> even_and_prime = all_predicates(is_even, is_prime) >>> even_and_prime(2) True >>> even_and_prime(4) False >>> even_and_prime(3) False
Added in version: 0.1.0
- functionali.some_predicates(*predicates: Callable[[Any], bool]) Callable[[Any], bool]
Takes a set of predicates and returns a function that takes an entity and checks if it satisfies some of the predicates.
>>> even_or_prime = some_predicates(is_even, is_prime) >>> even_or_prime(2) True >>> even_and_prime(4) True >>> even_and_prime(3) True
Added in version: 0.1.0