Sequence traversing functions

first(iterable)

Returns the first item in an iterable or None if iterable is empty.

rest(iterable)

Returns an iterator of all but the first element in the iterable.

ffirst(iterable)

same as first(first(iterable)) expects a nested iterable, returns None if iterable is empty

second(iterable)

Returns the second item in iterable, or None if length is less than 2

third(iterable)

Returns the third item in iterable, or None if length is less than 3

fourth(iterable)

Returns the fourth item in iterable, or None if length is less than 4

fifth(iterable)

Returns the fifth item in iterable, or None if length is less than 5

last(iterable)

returns the last element in the iterable.

butlast(iterable)

returns an iterable of all but the last element in the iterable

take(n, iterable)

Returns the first n number of elements in iterable.

drop(n, iterable)

Returns All the Elements after the first n number of elements in iterable.

take_while(predicate, iterable)

Constructs a iterable list by taking elements from iterable while predicate is true, Stop taking after the first element falsifies the predicate.

drop_while(predicate, iterable)

Drops elements from iterable while predicate is true, And returns a tuple of the remaining elements in iterable.

split_with(predicate, iterable)

Equivalent to (take_while(predicate, iterable), drop_while(predicate, iterable))

iter_(iterable)

Returns appropriate iterator for the given iterable.

reversed_(iterable)

Returns appropriate reversed iterator for the given iterable.

count(iterable)

counts the number of elements in the iterable, works with map objects, filter objets, and iterators.

count_(iterable)

returns a tuple of the number of elements in the iterable and the iterable itself.

functionali.first(iterable: Iterable[Any]) Optional[Any]

Returns the first item in an iterable or None if iterable is empty. If iterable is a dict, returns a tuple of the First key-value pair

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

Added in version: 0.1.0

functionali.rest(iterable: Iterable) Iterator

Returns an iterator of all but the first element in the iterable. If iterable is empty it returns an empty iterator.

>>> list(rest([1,2,3,4,5]))
[2, 3, 4, 5]
>>> tuple(rest({1:"a", 2:"b", 3:"c"}))
((2,"b"), (3, "c"))
>>> tuple(rest([]))
()

Added in version: 0.1.0

functionali.ffirst(iterable: Iterable[Any]) Optional[Any]

same as first(first(iterable)) expects a nested iterable, returns None if iterable is empty

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

Added in version: 0.1.0

functionali.second(iterable: Iterable[Any]) Optional[Any]

Returns the second item in iterable, or None if length is less than 2

>>> second([1,2,3,4,5])
2

Added in version: 0.1.0

functionali.third(iterable: Iterable[Any]) Optional[Any]

Returns the third item in iterable, or None if length is less than 3

>>> third([1,2,3,4,5])
3

Added in version: 0.1.0

functionali.fourth(iterable: Iterable[Any]) Optional[Any]

Returns the fourth item in iterable, or None if length is less than 4

>>> fourth([1,2,3,4,5])
4

Added in version: 0.1.0

functionali.fifth(iterable: Iterable[Any]) Optional[Any]

Returns the fifth item in iterable, or None if length is less than 5

>>> fifth([1,2,3,4,5])
5

Added in version: 0.1.0

functionali.last(iterable: Iterable[Any]) Optional[Any]

returns the last element in the iterable.

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

Added in version: 0.1.0

functionali.butlast(iterable: Iterable[Any]) Optional[Tuple[Any]]

returns an iterable of all but the last element in the iterable

>>> butlast([1, 2, 3])
(1, 2)

Added in version: 0.1.0

functionali.take(n: int, iterable: Iterable) Tuple

Returns the first n number of elements in iterable. Returns an empty tuple if iterable is empty

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

Added in version: 0.1.0

functionali.drop(n: int, iterable: Iterable) Tuple

Returns All the Elements after the first n number of elements in iterable. Returns an empty tuple if iterable is empty

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

Added in version: 0.1.0

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

Constructs a iterable list by taking elements from iterable while predicate is true, Stop taking after the first element falsifies the predicate.

>>> take_while(is_even, [2,4,6,7,8,9,10])
(2,4,6) # Notice that it does not include 8 and 10
>>> def is_even_dict(d):
        #checks if the key of dict d is even
        return d[0]%2==0
>>> take_while(is_even_dict, {2:"a", 4:"b",5:"c"})
    ((2, "a"), (4, "b"))

Added in version: 0.1.0

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

Drops elements from iterable while predicate is true, And returns a tuple of the remaining elements in iterable.

>>> drop_while(is_even, [2,4,6,7,8,9,10])
(7,8,9, 10)
>>> def is_even_dict(d):
        #checks if the key of dict d is even
        return d[0]%2==0
>>> drop_while(is_even_dict, {2:"a", 4:"b",5:"c"})
    ((5, "c"),)

Added in version: 0.1.0

functionali.split_with(predicate: Callable, iterable: Iterable) Tuple[Tuple, Tuple]

Equivalent to (take_while(predicate, iterable), drop_while(predicate, iterable))

>>> split_with(is_even, [2, 4, 6, 7, 8, 9, 10])
((2, 4, 6), (7, 8, 9, 10))

Added in version: 0.1.0

functionali.iter_(iterable: Iterable) Iterator

Returns appropriate iterator for the given iterable. This is mainly created because python’s iter returns an iterable of keys instead of keys and values for dict.

>>> tuple(iter_({1: "a", 2: "b", 3: "c"}))
((1, "a"),(2, "b"), (3, "c"))

Added in version: 0.1.0

functionali.reversed_(iterable: Iterable) Iterator

Returns appropriate reversed iterator for the given iterable. This is mainly created because python’s reversed returns an iterable of keys instead of keys and values for dict.

>>> tuple(reversed_({1: "a", 2: "b", 3: "c"}))
((3, 'c'), (2, 'b'), (1, 'a'))

Added in version: 0.1.0

functionali.count(iterable: Iterable) int

counts the number of elements in the iterable, works with map objects, filter objets, and iterators. count will consume iterators, use count_ if you want access to the iterators. Added in version: 0.1.2

>>> count(iter([1,2,3]))
3
functionali.count_(iterable: Iterable) Tuple[int, Iterable]

returns a tuple of the number of elements in the iterable and the iterable itself. This can be used if you wish to find the length of iterators and want to consume the iterators later on. Added in version: 0.1.2 >>> count(iter([1,2,3])) (3,[1,2,3])