Package: haskpy
HaskPy - Haskell types and functions in PythonHask is the category of types and functions in Haskell. This package provides
classes and functions inspired by Hask.Documentation is available atjluttine.github.io/haskpy.OverviewFeaturesTypeclasses:Functor,Applicative,Monad,Semigroup,Monoid,Commutative,Foldable,Contravariant,Profunctor,Cartesian,CocartesianTODO:Traversable,Bifunctor,Monoidal,Ord,Show,ReadTypes and type constructors:Identity,Maybe,Either,List,Function,Compose,LinkedListTODO:Constant,Validation,Dictionary,State,Reader,Writer,IOMonad transformers:MaybeT,IdentityTTODO:StateT,ReaderT,WriterT,ListTSimple monoids:Sum,All,Any,String,EndoTODO:ProductProfunctor optics:adapter,lens,prismTODO:traversal,grate,affine,setterOperators for common tasks:**for function composition or functorial
mapping,@for applicative application,%for monadic binding and>>for applicative/monadic sequencing.Property-based testing of typeclass lawsHaskPy has implemented typeclass laws as property-based tests. Thus, one can
easily test that an implementation satisfies all the laws it should. Just add
something like this to your test module and run with pytest:fromhaskpy.utilsimportmake_test_classfrommystuffimportMyClassTestMyClass=make_test_class(MyClass)This will automatically verify thatMyClasssatisfies all the typeclass laws
of those typeclasses that it inherits. It makes use of greatHypothesis
package.ExamplesFunctorsA minimal example of functorial mapping:>>>fromhaskpyimportmap,List>>>map(lambdax:x**2,List(1,2,3,4,5))List(1,4,9,16,25)Lift over two layers of functorial structure:>>>fromhaskpyimportmap,List,Just,Nothing>>>map(map(lambdax:x**2))(List(Just(1),Nothing,Just(3),Just(4),Nothing))List(Just(1),Nothing,Just(9),Just(16),Nothing)Note thathaskpy.mapworks for all Functor instances. That is, you don't need
to use a different function to lift over different functors. You can even create
function that performs some operation to values contained in any two-layer
functorial structure. In the following example,squaresquares the values
inside a two-layer functor:>>>square=map(map(lambdax:x**2))>>>square(List(Just(1),Nothing,Just(3)))List(Just(1),Nothing,Just(9))>>>square(List(List(1,2,3),List(4,5)))List(List(1,4,9),List(16,25))Even functions are functors if they have been decorated withfunction:>>>@function...deff(x):...returnList(x,2*x,3*x)>>>square(f)(3)List(9,36,81)FunctionsHaskPy provides typeFunctionfor functions so that they have, for instance,FunctorandMonadimplementations. These functions are curried and they
should only take a fixed number of arguments (no optional arguments and at least
one argument). Decoratorfunctionjust converts a normal Python function into
a curriedFunctionobject:>>>fromhaskpyimportcurry>>>@function...defconcat(x,y,z):...returnx+y+z>>>concat("a")("b")("c")"abc">>>concat("a")("b","c")"abc"Note that it doesn't convert the function into nested one-argument functions (as
currying strictly speaking should do) but it gives more flexibility by accepting
any number of arguments (even no arguments at all). Keyword arguments aren't
supported because of consistency.Almost all functions in HaskPy have been decorated withfunction.CopyrightCopyright (C) 2019-2021 Jaakko LuttinenHaskPy is licensed under the MIT License. See LICENSE file for a text of the
license or visithttp://opensource.org/licenses/MIT.