Code Monkey home page Code Monkey logo

fpbox's Introduction

FPbox

A toolkit for functional programming in Python

Disclaimer

I wrote this tool when I was first learning about Python and FP. It doesn't provide much that Python doesn't already have.

Install

pip install fpbox

Examples

from fpbox import *
from operator import sub

# The Array collection enforces that the whole sequence be the same type. Immutable.
xs = Array([1, 2, 3])

# head/tail/init/last functions
one = head(xs)
two_three = tail(xs)
three = last(xs)
one_two = init(xs)

# Function composition, right to left
fn_composition = compose(last, reverse)(xs)

# Use strings like normal sequences with the "chars" helper function
reversed_string = reverse(chars("Racecar"))

# Really pushing the strings-as-sequences idea with string comprehension
string_comprehension = Array([Char(x) for x in 'hello' if not x == 'h'])

# The Stream class takes a sequence and returns a generator, and also
# gives a handful of lazy FP-related methods. Just some syntactic sugar.
lazy_xs_plus_one = Stream(xs).map(lambda x: x + 1)

# Calculating the amount of numbers between each number in the Array
distance_between = reverse_binmap(sub, xs)

curried_function = curry(lambda x, y: x + y)  # Curries a function
curried_result = curried_function(1)(2)  # Result is 3!


# Here's a quicksort implementation that shows how the tools
# FPBox offers can be used for great good
def quicksort(xs):
    if len(xs) > 1:
        pivot = head(xs)
        lesser, greater = map(quicksort, partition(lambda x: x < pivot, tail(xs)))
        return lesser + [pivot] + greater
    else:
        return xs


one_to_five_ascending = quicksort([5, 4, 3, 2, 1])

Reference

https://fpbox.readthedocs.io/en/latest/

fpbox's People

Contributors

an3223 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.