Skip to main content

Functionali

functional programming tools for python. Putting the fun in functional programming 😉

codecov Documentation Status

Functional programming is a fundamentally different way of solving problems, and once It clicks, it's pure joy after that. A lot of ideas in this library have been taken from Clojure and Haskell, so the credit goes to those languages. If you find your favorite function missing, or find ways to improve this project, I'd love to hear it.

Documentation

wiki

Installation

pip install functionali 

Features

  • Polymorphic functions that operate on all sequences. Including dictionaries.
  • A bunch of commonly used predicates that enhance readability, especially when used with filters.

A note on how functionali deals with dictionaries.

As of now, all sequence traversing like first and rest functions treat dictionaries as a nested tuple of keys and values. Here's an example.

>>> d = {1:"a"}
>>> first(d)
((1, "a"),)

while this is convenient for traversing dicts, it makes certain functions like contains rather awkward, since you as the user, would have to explicity convert the tuple back to a dict before passing it in. (This might change in future)

functional programming is powerful

One of my favorite Functional implementations of an algorithm is insertion sort, here's how simple it

>>> from functionali import foldr, insert
>>> def insertion_sort(iterable):
...     return foldr(insert, iterable, [])

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

# even works with dictionaries
>>> insertion_sort({3:"c", 1: "a", 2: "b"})  
((1, 'a'), (2, 'b'), (3, 'c'))

# and strings
>>> insertion_sort("cbdasdf")
('a', 'b', 'c', 'd', 'd', 'f', 's')
>>> "".join(insertion_sort("cbdasdf"))
'abcddfs' # better?

functional programs are flexible

Let's say you wanted to find a number that was divisible by The following numbers 1,2,3,4,5,6,7,8,9,10 One way to solve it is a bunch of nested if Statements and we all know how brittle those can be.

Let's see the functional way.

>>> from functionali import is_divisible_by, all_predicates
>>> my_filter = all_predicates(*map(is_divisible_by,[1,2,3,4,5,6,7,8,9,10]))
>>> list(filter(my_filter,range(1,10000)))
[2520, 5040, 7560]

Now let's say the boss decides that He wants the numbers that are not Divisible by all these numbers

>>> from functionali import complement
>>> list(filter(complement(my_filter),range(1,10000)))
[... 2515, 2516, 2517, 2518, 2519, 2521, 2522, 2523, 2524...] # Snipped for brevity 
# note that 2520 is not present

The boss comes in again and he says that he Doesn't want numbers that are divisible by ALL these numbers but instead, ANY of these numbers [1,2,3,4,5,6,7,8,9,10]

>>> from functionali import some_predicates
>>> my_filter = some_predicates(*map(is_divisible_by,[1,2,3,4,5,6,7,8,9,10]))
>>> list(filter(complement(my_filter),range(1,10000)))
[]

The boss realizes that all numbers are divisible by 1 and he tells you to Remove 1

>>> my_filter = some_predicates(*map(is_divisible_by,[2,3,4,5,6,7,8,9,10]))   
>>> list(filter(complement(my_filter),range(1,10000)))
[1, 11, 13, 17, 19, 23, 29, 31...] # ,Snipped for brevity

A few more examples

Functionali provides functions to traverse sequences(Including dictionaries), Some of the most useful ones are first, rest, last,butlast, take, drop

from functionali import first, rest, last, butlast, take, drop

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

>>> first({1:"a", 2:"b"})
    (1, "a")

>>> last([1,2,3,4])
    4

>>> list(rest([1,2,3,4,5]))
    [2, 3, 4, 5]

>>> butlast([1,2,3]) # returns all elements except the last element
    (1,2)

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

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

There are functions that construct new sequences like cons, conj, concat, insert

from functionali import cons, conj, concat, insert
>>> cons(5, [1,2,3,4])
    deque([5, 1, 2, 3, 4]) # 5 is the 'head' of the new list.

# adds element to the iterable, at the appropriate end.
>>> conj([1,2,3,4],5) # similar to Clojure's conj
    [1, 2, 3, 4, 5]

    # Adds to the left of a deque.
>>> conj(deque([1,2]), 3,4)
    deque([4, 3, 1, 2])

# 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])

# Inserts 3 right before the first element
# in the iterable (here:4) that is greater than 3
>>> insert(3, [1,2,4,2])
    (1,2,3,4,2)

Functionali also comes with a number of useful predicates (if You can't find something you're looking for, make a pull request.) These can be combined in various ways. for example.

from functionali import is_even, is_prime, take_while
>>> list(filter(is_even,[1,2,3,4])
    [2,4]

>>> take_while(is_prime, [2,3,5,4,6,5])) # Constructs a list while is_prime is true.
    [2,3,5]

Metadata

Release files for functionali 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for functionali 0.3.0
File Size Uploaded
functionali-0.3.0.tar.gz 14.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for functionali 0.3.0
File Interpreter ABI Platform
functionali-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 30.3 kB

Release files / functionali-0.3.0.tar.gz

Download URL functionali-0.3.0.tar.gz
Size 14.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8c82b12590af2b1feba5ae2003efdb61a79a651ecf2c2f1f3b42491238e47267
BLAKE2b-256 checksum
How to use checksums
3fa152bb38d30d9835123f80ac02e2326462581afa8e997f2e94a32baf66a5b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.6 CPython/3.8.9 Windows/10

Release files / functionali-0.3.0-py3-none-any.whl

Download URL functionali-0.3.0-py3-none-any.whl
Size 16.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e48619fdc3dc2b411f3a6f9b14a4546f0ae60cd7132882aea1b3d42bbc6da491
BLAKE2b-256 checksum
How to use checksums
3f501323ed7dfdca19bb5a6dc5070492ee256612be0c978753a0e15196e9433e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.6 CPython/3.8.9 Windows/10

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page