Forbidden Functional Programming in Python.
Project description
forbiddenfp
Summary
Functional-Programming (FP) in a forbidden way:
You can turn arbitrary function into postfix notation in favor of function chaining.
And the library provides many useful patches for builtin/itertools functions.
Install
Compatible with Python 3.7+
pip install forbiddenfp
Examples
# objects are already patched at import time
import forbiddenfp
"abc".print().len() # print out "abc", then return 3
"abc".then(lambda s: s * 2).filter(lambda s: s == "b").join() # "bb"
# A more complex one (examples/word_count.py)
("./lorem_ipsum.txt"
.with_open(lambda path, f: f.read().also(print(f"Reading {path}")))
.then(lambda s: s.split(" "))
.counter()
.print())
See more ./examples
.
Why Functional Programming
Separate out control structs (which are functions provided by library) from business logic (which are lambda functions supplied to fill the control structs).
So we can have a clearer scope, and separate concerns, when we want to change on either side of things.
if ...
andif ... else ...
are modeled by Maybe/Either monads.while ...
andfor ... in ...
are generator/iterators. Additionally, stop-early behavior istakewhile
of the sequence.
Philosophically, think more in def transform(old_state) -> new_state
, rather than state = modify(state)
.
What About Auto-completion?
This library is almost an embedded Domain Specific Language (DSL) in Python.
It is understandable that users aren't familiar with what is available without auto-completion features in IDEs.
So we leverage the stub file to provide type hints for the builtin object
type, which IntelliJ/PyCharm/VsCode all support.
In order to find where to stub, open your IDE and Python project/file, type object
to get a class, CTRL/CMD + Click and use
"Find Definition" feature to get to the stub file of your IDE plugin.
Then copy/paste extra definitions of object
class from typing/builtins.pyi
.
Note that for IntelliJ/Pycharm, once you CTRL + Click on object
, you would need to click on the little icon below
to get to the actual stub file. Append content there.
Warning
This library patches builtin object
(and hence ALL classes),
with hacks around CPython API (provided by forbiddenfruit),
so consider this NSFW (Not Safe For Work).
Known Issues
None
doesn't work well with chained keyword arguments.
import forbiddenfp
None.apply(print) # works
None.apply(func=print) # doesn't work
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
Hashes for forbiddenfp-0.9.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 099506084d0eed15771a63cfba0cac3b3e5cc9f48ab7daf53d694ad6b5c15354 |
|
MD5 | ce885585959225e9cb3e40db04ca5241 |
|
BLAKE2b-256 | c0fbee47bb9479fb8fc02fbc3afb6e17ef194245b30fcec4337334dae479d4f9 |