Functional Python
For better computation composing in Python
Goals
- To bring the ability of composing computations in the functional way
- Make my life easier
No Goals
- Exact clone of Haskell
- Blazing fast / super efficient
Python is Already Amazing, Why Bother?
- Because I can
- Python may be amazing in some field, but sucks from the functional perspective
Python Sucks, Why Bother?
- Because I can
- Python is still used in my work place
Install
With pip:
pip install fppy
Control:
fpy.control.functor
Functor Given Functors f
, g
:
__fmap__ :: f a -> (a -> b) -> f b
__ntrans__ :: f a -> (f a ~> g b) -> g b
Operators:
-
|
=__fmap__
-
&
=__ntrans__
Functions:
-
fmap
=__fmap__
fpy.control.natural_transform
NTrans (Natrual Transform) Given Functors f
, g
:
__trans__ :: f a ~> g b
fpy.control.applicative
Applicative : Functor No new trait comparing to functor, liftA2
is defined using fmap
fpy.control.monad
Monad : Applicative Given Monad m
:
__bind__ :: m a -> (a -> m b) -> m b
Operators:
-
>>
=__bind__
Do Notation:
-
@do(Monad)
enables do notation in the decorated function, where the explicitreturn
statement will be treated asret
from the givenMonad
type, if noreturn
statement is given, the last element on the stack will be returned. -
name <- computation
binds the computation to the following block, calling the__bind__
method of the monad object returned fromcomputation
with the namename
. -
(name1, name2, ..., namen) <- computation
works in the similar way as the single name binding, this applys the binding function to the tuple contained within the monad object instead of calling the function directly. -
name1, name2, ..., namen <- computation
same as above
Data
fpy.data.maybe
Maybe : Monad Types:
Maybe[T]
Just[T] : Maybe[T]
Nothing[T] : Maybe[T]
Functions:
isJust :: Maybe[T] -> bool
isNothing :: Maybe[T] -> bool
fromJust :: Maybe[T] -> T
fromMaybe :: T -> Maybe[T] -> T
maybe :: S -> (T -> S) -> Maybe[T] -> S
mapMaybe :: (T -> Maybe[S]) -> List[T] -> List[S]
fpy.data.either
Either : Monad Types:
Either[T]
Left[T] : Either[T]
Right[T] : Either[T]
fpy.data.forgetful
Forgetful : Monad (Forgetful Functor) Types:
-
Under[T]
Under
similar to Haskell'sIdentity
monad
fpy.data.cont
Cont : Monad Types:
Cont[T, R]
Functions:
cont :: (A -> B) -> Cont[A, B]
runCont :: Cout[B, C] -> C
Functions:
Given functor f
:
forget: NTrans[F, B, Under, T] :: f b ~> Under[T]
fpy.data.function
Utility Functions id_ :: T -> T
const :: T -> A -> T
flip :: (B -> A -> T) -> A -> B -> T
fix :: (A -> A) -> A
on :: (B -> B -> T) -> (A -> B) -> A -> A -> T
Composable
fpy.composable.composable
Composable __compose__
Operators:
-
^
=__compose__
fpy.composable.transparent
Transparent -
__underlying__
Delegate an attribute access to an underlying object
fpy.composable.function
Function Types:
func : Composable
SignatureMismatchError
NotEnoughArgsError
fpy.composable.collections
Collections Types:
Seq : func
Map : func
Functions:
transN(n, f, it) := it[n] = f(it[n])
getN(n, it) := it[n]
setN(n, v, it) := it[n] = v
eqN(n, it, x) := it[n] == x
mapN(n, fn, lsts) := map(fn, zip(lst1, lst2 ... lstn))
of_(v1 ... vn) := _ in (v1 ... vn)
is_(t) := isinstance(_, t)
and_(a, b) := a(_) and b(_)
or_(a, b) := a(_) or b(_)
to(dst, src) := dst(src)
apply(fn) := fn(*a, **k)
fwd_ = Under.ret
Predefined Vars:
trans0
trans1
get0
get1
set0
set1
eq0
eq1
mp1
mp2
Parsec
fpy.parsec.parsec
Parsec Types:
parser[S, T] :: [S] -> Either [S] ([T] * [S])
Operators:
-
*
=parser.timeN
-
+
=parser.concat
-
|
=parser.choice
-
>>
=parser.parseR
-
<<
=parser.parseL
Functions:
one :: (S -> bool) -> parser[S, S]
neg :: (S -> bool) -> parser[S, S]
just_nothing :: parser[S, T]
pmaybe :: parser[S, T] -> parser[S, T]
many :: parser[S, T] -> parser[S, T]
many1 :: parser[S, T] -> parser[S, T]
ptrans :: parser[S, T] -> (T -> Y) -> parser[S, Y]
peek :: parser[S, T] -> parser[S, T]
skip :: parser[S, T] -> parser[S, T]
pseq :: [S] -> parser[S, T]
inv :: parser[S, T] -> parser[S, T]
Dependencies
License
GPL3+