Sequence transforming functions

cons(arg, iterable)

Returns a deque with arg as the first element.

conj(iterable, *args)

Short for conjoin, adds element to the iterable, at the appropriate end.

concat(iterable, *args)

Add items to the end of the iterable.

argmap(functions, args)

Maps the same argument(s) to multiple functions.

argzip(sequence, *args)

Similar to zip, but instead of zipping iterables, It zips an argument(s) with all the values of the iterable.

unzip(sequence)

Opposite of zip.

interleave(*seqs)

Similar to clojure's interleave.

flatten(sequence)

Returns the contents of a nested sequence as a flat sequence.

insert(element, iterable, *[, key])

Inserts element right before the first element in the iterable that is greater than element

remove(predicate, iterable)

Opposite of filter; Constructs an iterable of elements that falsify the predicate.

functionali.remove(predicate: Callable, iterable: Iterable) Tuple

Opposite of filter; Constructs an iterable of elements that falsify the predicate.

>>> remove(lambda x: x==1, [1,1,9,1,1]
[9]
>>> remove(lambda x: x%2==0, range(10))
[1,3,5,7,9] # filter would return [2,4,6,8]

Added in version: 0.1.0

functionali.cons(arg: Any, iterable: Iterable) collections.deque

Returns a deque with arg as the first element.

Adds to the left of a deque.

>>> cons(5, [1,2,3,4])
deque([5, 1, 2, 3, 4])
>>> cons(3, deque([1,2]))
deque([3, 1, 2])
>>> cons((3, "c"), {1:"a", 2: "b"})
deque([(3, "c"), (1, "a"), (2, "b")])

Added in version: 0.1.0

functionali.conj(iterable: Iterable, *args: Any) Iterable

Short for conjoin, adds element to the iterable, at the appropriate end. Adds to the left of a deque.

>>> conj([1,2,3,4],5)
[1, 2, 3, 4, 5]
>>> conj(deque([1,2]), 3,4)
deque([4, 3, 1, 2])
>>> conj([1,2,3,4],5,6,7,8)
[1, 2, 3, 4, 5, 6, 7, 8]
>>> conj([1,2,3,4],[5,6,7])
[1, 2, 3, 4, [5, 6, 7]]
>>> conj((1,2,3,4),5,6,7)
(1, 2, 3, 4, 5, 6, 7)
>>> conj(range(10), 11)
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11)
>>> conj({1:"a", 2:"b"}, {3:"c"})
{1: 'a', 2: 'b', 3: 'c'}

Added in version: 0.1.0

functionali.concat(iterable, *args)

Add items to the end of the iterable.

>>> concat([1,2,3,4],5)
[1, 2, 3, 4, 5]
>>> concat(deque([1,2]), 3,4)
deque([1, 2, 3, 4])

Added in version: 0.1.0

functionali.argmap(functions: Iterable[Callable], args: Iterable) Generator

Maps the same argument(s) to multiple functions.

>>> inc = lambda x:x+1
>>> dec = lambda x:x-1
>>> list(argmap([inc, dec],[1]))
    [2,0]

you can even map multiple arguments

>>> add = lambda a,b: a+b
>>> sub = lambda a,b:  a-b
>>> list(argmap([add, sub], [2, 1])) # two arguments
    [3, 1]

Added in version: 0.1.0

functionali.argzip(sequence: Iterable[Callable], *args: Any) Generator

Similar to zip, but instead of zipping iterables, It zips an argument(s) with all the values of the iterable. for example.

>>> list(argzip([1,2,3,4], "number"))
[(1, 'number'), (2, 'number'), (3, 'number'), (4, 'number')]
>>> list(argzip([1,2,3,4], "number", "int"))
[(1, 'number', 'int'), (2, 'number', 'int'), (3, 'number', 'int'), (4, 'number', 'int')]

Added in version: 0.1.0

functionali.unzip(sequence: Iterable) Tuple[Any]

Opposite of zip. Unzip is shallow.

>>> unzip([[1,'a'], [2,'b'], [3,'c']])
((1, 2, 3), ('a', 'b', 'c'))
>>> unzip([ [1,'a','A'], [2, 'b','B'], [3,'c','C'] ])
((1, 2, 3), ('a', 'b', 'c'), ('A', 'B', 'C'))

shallow nature of unzip.

>>> unzip([ [[1,'num'],['a','str']], [[2,'num'],['b','str']] ])
(([1, 'num'], [2, 'num']), (['a', 'str'], ['b', 'str']))

Added in version: 0.1.0

functionali.interleave(*seqs: Iterable) Tuple

Similar to clojure’s interleave. returns a flat sequence with the contents of iterables interleaved.

>>> interleave([1,2,3],["a","b","c"])
(1, 'a', 2, 'b', 3, 'c')
>>> interleave([1,2,3],["int","int","int"], ["a","b","c"],["str","str","str" ])
(1, 'int', 'a', 'str', 2, 'int', 'b', 'str', 3, 'int', 'c', 'str')

Added in version: 0.1.0

functionali.flatten(sequence: Iterable) Tuple

Returns the contents of a nested sequence as a flat sequence. Flatten is recursive.

>>> flatten([1,2,[3,[4],5],6,7])
(1, 2, 3, 4, 5, 6, 7)

Added in version: 0.1.0

functionali.insert(element: Any, iterable: Iterable, *, key: Callable = <function <lambda>>) Tuple

Inserts element right before the first element in the iterable that is greater than element

>>> insert(3, [1,2,4,2])
(1,2,3,4,2)
>>> insert((2, "b"), {1:"a", 3:"c"})
((1, "a"), (2, "b"), (3, "c"))

Using the key Parameter

>>> Person = namedtuple("Person", ("name", "age"))
>>> person1 = Person("John", 18)
>>> person2 = Person("Abe", 50)
>>> person3 = Person("Cassy", 25)
>>> insert(person3, (person1, person2), key=lambda p:p.age)
    (person1, person3, person2)
>>> insert(person3, (person1, person2), key=lambda p:p.name)
    (person3, person1, person2)

Added in version: 0.1.0