Author: | Christoforus Surjoputro <[email protected]> |
---|---|
Date: | 2018-12-31 |
Version: | 0.0.1 |
License: | MIT License |
Table of content
cpyfunctional is python package to help you code python in functional programming paradigm. Series of article by Eric Elliot will tell you why you should code using functional programming.
This module work on 3.4+. Fully tested on python 3.5.2.
pip install cpyfunctional
- Import cpyfunctional to your project:
import cpyfunctional
. - Choose action you want in
cpyfunctional
package.
compose is a function to execute any callable one by one chaining from last callable to first. This function accept any callable and execute it from last callable to the first and pass every result to next callable until the last.
def inc(number: int) -> int:
return number + 1
def square(number: int) -> int:
return number ** 2
cpyfunctional.compose(inc, square)(3) # 10
cpyfunctional.compose(square, inc)(3) # 16
As you can see, it execute callable from last to first. You can also use lambda instead of creating function.
cpyfunctional.compose(lambda number: number + 1, lambda number: number ** 2)(3) # 10
This function has same functionality to compose but execute callable from first to last.
def inc(number: int) -> int:
return number + 1
def square(number: int) -> int:
return number ** 2
cpyfunctional.pipe(inc, square)(3) # 16
cpyfunctional.pipe(square, inc)(3) # 10
func_curry is a function to add parameter to callable that called by compose or pipe. This function accept a callable that accept parameter and push previous value to related callable and execute it.
def inc(number: int) -> int:
return number + 1
def multiple(multiplier: int, prev_number: int) -> int:
return prev_number * multiplier
cpyfunctional.compose(inc, func_curry(multiple)(6))(3) # 19
This example show that now callable are able to accept any parameter not only from previous result.
Just create an issue when you encounter any problem or contact me personally.
I'm not an expert in functional programming, so any input about FP like any function and/or naming and/or incorrect implementation will be very helpful.