No project description provided
Project description
ag.funutils
Fun functional utilities for python. This library provides a chain
method and several operators that you can chain together to create pipelines of functional transformations, like with Lodash's chain or Clojure's threading macros.
The provided functions are "sugary". They do NOT provide performance optimizations, error handling, or guarantees of statelessness.
Each operator will take a transformation and return a function that applies that transformation to an iterable. Because most operators are wrappers around built-in functions like of the same name, they return iterables and the results of most chains will need to be converted to a list to be immediately useful. The documentation for associated standard library functions should largely be applicable to the provided operators.
Examples
Also see the tests.
import ag.funutils as fun
add_one = fun.map(lambda x: x + "1")
upper = fun.map(str.upper)
fun.chain(
["a", "b", "c", "d"],
add_one,
upper,
list
) # => ["A1", "B1", "C1", "D1"]
big_transform = [add_one, upper]
fun.chain(
["a", "b", "c", "d"],
*big_transform,
list
) # => ["A1", "B1", "C1", "D1"])
fun.chain(
["a", "b", "c", "d"],
fun.tap(print), # => "[a, b, c, d]"
*big_transform,
fun.tap(print), # => "[A1, B1, C1, D1]"
fun.sort(reverse=True),
list
) # => ["D1", "C1", "B1", "A1"]
fun.chain(
["a", "b", "c", "d"],
*big_transform,
fun.reduce(lambda acc, x: acc + x)
) # => "A1B1C1D1"
Reference
ag.funutils.chain(data, *transforms)
:- Provides
data
as an argument to the firsttransform
, then the result of eachtransform
to the following one. Eachtransform
should be a function that takes a single, iterable argument and returns an iterable. The exception is thereduce
operator, which can return a single value if it is the last operator in the chain.
- Provides
ag.funutils.map(transform)
:- returns a function which takes an iterable and applies
transform
to each item of the iterable
- returns a function which takes an iterable and applies
ag.funutils.filter(condition)
:- returns a function which takes an iterable and returns an iterable with only items matching the condition
ag.funutils.sort(key=None, reverse=False)
:- returns a function which takes an iterable and return an iterable sorted according to the
key
function. Compares items directly by default.
- returns a function which takes an iterable and return an iterable sorted according to the
ag.funutils.reduce(transform, initial=None)
:- returns a function which takes an iterable and reduces it to a single value. If no
initial
value is provided, the first item is used as the initial value.
- returns a function which takes an iterable and reduces it to a single value. If no
ag.funutils.tap(fn)
:- returns a function which takes a single argument, passes it to
fn
, and returns it. Intended for debugging purposes, in particular:ag.funutils.tap(print)
.
- returns a function which takes a single argument, passes it to
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ag.funutils-1.0.tar.gz
.
File metadata
- Download URL: ag.funutils-1.0.tar.gz
- Upload date:
- Size: 2.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.5.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ffc55a5d36808b427be284d9f7c765bf78c461c88d153fc8e9f6a444e5739a4 |
|
MD5 | f04fbc1d1043a13b91f96acece5ac723 |
|
BLAKE2b-256 | 6d2ea01687d04cf0cdbb7b7f9bbf1ee73c0b5ba44071a47612265c06ea82e63a |
File details
Details for the file ag.funutils-1.0-py3-none-any.whl
.
File metadata
- Download URL: ag.funutils-1.0-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.5.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f00ff27b1f8b5bc1e8b95d4c6abf916abbfe663c7b6bd2095b754f2b8e5b0542 |
|
MD5 | bb3eab57629e01a1f3dabe68a4b7c12b |
|
BLAKE2b-256 | 6970166f45de12098f47f5af8be461620880f634c907ea39d095f20b729b5570 |