Skip to main content

Utilities for functional programming

Project description

Functional Programming Utilities

"Functional Programming" distinguishes between:

  1. Data
  2. Functions
  3. Actions

Working with data

Data should be treated as immutable. For example, if an element is added to a list, a new "list" object is created. The source remains unchanged. This corresponds to the behavior of strings in Python. The situation is different with the "Collection" objects in Python. The classes:

  • list
  • dict

can be changed "in place". "tuple" and "set" cannot, however. The module "collection.py" provides the functions:

  • append_element
  • append_collection
  • remove_element

which can be used to edit:

  • list
  • dict
  • tuple

in the same way. The source remains unchanged in each case. The result is always a new object of the same class as the source.

Working with functions and iterators

Functions - or "pure functions" are used to implement business rules. The result depends only on the input parameters of the function. The iterators, which are also frequently used in functional programming, lead to surprising effects.

Example:

def get_even_num(list_of_numbers):
return filter(lambda x: x % 2 == 0, list_of_numbers)

# Create iterator; return even numbers only
coll_even_num = get_even_num([1,2,3,4,5,6,7,8])
# get sum; sum is a "pure function"
print(sum(coll_even_num))
print(sum(coll_even_num))

The output is:

20
0

The result is quickly explained: the function "sum" used up the iterator "coll_even_num" with the first call. With the second call, this iterator no longer returns any values ​​- the result is "0". If you run the example shown above with an object of the class "list", a different result is returned:

def get_even_num_as_list(list_of_numbers):
return list(filter(lambda x: x % 2 == 0, list_of_numbers))

# Create iterator; return even numbers only
coll_even_num = get_even_num([1,2,3,4,5,6,7,8])
# get sum; sum is a "pure function"
print(sum(coll_even_num))
print(sum(coll_even_num))

The output is:

20
20

An important feature of Python is generic algorithms. These (like the "sum" function) can work with different input data such as:

  • tuple
  • list
  • iterator

The "iterator.py" module provides the "decorators":

  • restartable_t
  • restartable_m

These can be used on functions that return "iterators". The "decorator" is used to package the "iterator" in a class that returns a new "iterator" every time the function __iter__() is called. "restartable_t" and "restartable_m" use different methods to get a new "iterator":

  • "restartable_t" calls the function that created the original "iterator" again.
  • "restartable_m" uses the "itertools.tee" function internally.

"restartable_t" and "restartable_m" cannot, of course, perform miracles - they merely compensate for the unexpected behavior of the "iterator". "restartable_t" does this at the expense of runtime - "restartable_m" at the expense of storage space.

Example:

from klfuncutil.iterator import restartable_t

@restartable_t
def get_even_num(list_of_numbers):
return filter(lambda x: x % 2 == 0, list_of_numbers)

# Create iterator; return even numbers only
coll_even_num = get_even_num([1,2,3,4,5,6,7,8])
# getsum; sum is a "pure function"
print(sum(coll_even_num))
print(sum(coll_even_num))

The output is:

 20
 20

Repro

https://github.com/nuuk42/klfuncutil.git

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

klfuncutil-1.0.1.tar.gz (21.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

klfuncutil-1.0.1-py2.py3-none-any.whl (17.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file klfuncutil-1.0.1.tar.gz.

File metadata

  • Download URL: klfuncutil-1.0.1.tar.gz
  • Upload date:
  • Size: 21.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for klfuncutil-1.0.1.tar.gz
Algorithm Hash digest
SHA256 e41793c50c945969572e7efa50830fc78e210d9215ad04d348c9a0839a88a7a8
MD5 6a7318fdb3a7f64f3532a3cd26ed1864
BLAKE2b-256 98d77c6b657dc4090b6d05320b97ede73f0e8dfa04afb8e6b38f732d76e62b3b

See more details on using hashes here.

File details

Details for the file klfuncutil-1.0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: klfuncutil-1.0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for klfuncutil-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9c63fda41a003afcf809eeaeb08088ef3d707d493e49e813a4212110cef185ba
MD5 c3c7167930331a75be14b3db1003f338
BLAKE2b-256 7453d41c190b51b74ca4a19805dc1a67f2a161823db1bf15cc545e887cfccf36

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page