Skip to main content

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


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 hashes)

Uploaded Source

Supported by

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