A simple library which implements higher order functions in python using functional chaining.
Project description
HOFU
A simple, fast and lightweight HOF (higher order functions) library for python.
This library allows you to use HOFs in a simple way, comparable to JavaScript its HOF and the Rust iterator.
Installation
pip install hofu
Example
from hofu import Iterator
Iterator(range(10)) \
.map(lambda x: x ** 2) \
.filter(lambda x: x % 2 == 0 and x != 0) \
.map(lambda x: "Next even power is " + str(x)) \
.for_each(print)
# +- equivalent to sum(range(10))
total = Iterator(range(10)).reduce(lambda x, y: x + y)
print("Sum of 1-10:", total)
Iterator(range(10)) \
.rev() \
.skip(5) \
.take(2) \
.map(lambda x: "Next number: " + str(x)) \
.for_each(print)
people = [
{"name": "John", "age": 30},
{"name": "Jane", "age": 25},
{"name": "Bob", "age": 40},
{"name": "Alice", "age": 35},
]
average_age = Iterator(people).reduce(lambda x, y: x + y["age"], 0) / len(people)
print("Average age:", average_age)
data = Iterator(range(10)).filter(lambda x: x % 2 == 0).collect()
print("Even numbers:", list(data))
even_formatted = Iterator(range(10)).map(lambda x: f"{x}={'even' if x % 2 == 0 else 'odd'}").collect()
print("Even/odd:", list(even_formatted))
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
hofu-0.0.1.tar.gz
(3.9 kB
view details)
File details
Details for the file hofu-0.0.1.tar.gz
.
File metadata
- Download URL: hofu-0.0.1.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36f94e511dd47cbce73cf65d45878a0255760238d0b6cda0d99f21e287aa2590 |
|
MD5 | c513d9c65d062af27d2f244544c52305 |
|
BLAKE2b-256 | 792597f504b56531f6480b16cf98d0ab8062b67d24f3228d80b1137d8fc8ea2d |