- haskpy.types.either.Either
Either¶
- class Either(match)[source]¶
-
- __annotations__ = {}¶
- __eq__(other)[source]¶
Mark method non-existing
This is a workaround for Python forcefully creating some methods. One cannot create objects that don’t have
__eq__,__ge__,__gt__and many other methods. They are there and it’s not possible to delete them. With this wrapper you can override those methods so that they won’t show up in__dir__listing and if accessed in any way,AttributeErroris raised. Note that it just hides the methods, one can still access them asobject.__getattribute__(obj, "__eq__").
- __hash__ = None¶
- __lshift__(x)¶
Sequence with
<<similarly as with<*and<<in Haskell
- __matmul__(x)¶
Application operand
@applies similarly as<*>in Haskellf @ xtranslates tof.apply_to(x),x.apply(f)andapply(f, x).Why
@operator?It’s not typically used as often as some other more common operators so less risk for confusion.
The operator is not a commutative as isn’t
applyeither.If we see matrix as some structure, then matrix multiplication takes both left and right operand inside this structure and gives a result also inside this structure, similarly as
applydoes. So it’s an operator for two operands having a similar structure.The operator evaluates the contained function(s) at the contained value(s). Thus,
f“at”xmakes perfect sense.
- __mod__(f)¶
Use
%as bind operator similarly as>>=in HaskellThat is,
x % fis equivalent tobind(x, f)andx.bind(f).Why
%operator?It’s not very often used so less risk for confusion.
It’s not commutative as isn’t bind either.
It is similar to bind in a sense that the result has the same unit as the left operand while the right operand has different unit.
The symbol works visually as a line “binds” two circles and on the other hand two circles tell about two similar structures on both sides but those structures are just on different “level”.
- __ne__(other)¶
Inequality comparison:
Eq a => a -> a -> boolCan be used as
!=operator.The default implementation uses
__eq__.
- __rpow__(f)¶
Lifting operator
**lifts similarly as<$>in Haskellf ** xtranslates tox.map(f)andmap(f, x).Why
**operator?It’s not typically used as often as multiplication or addition so less risk of confusion.
It’s not commutative operator as isn’t lifting either.
The two operands have very different roles. They are not at the same “level”.
The right operand is “higher”, that is, it’s inside a structure and the left operand is kind of “raised to the power” of the second operand, where the “power” is the functorial structure.
The same operand is also used for function composition because function composition is just mapping. Visually the symbol can be seen as chaining two stars similarly as function composition chains two functions.
- __rshift__(x)¶
Sequence with
>>similarly as with*>and>>in Haskell
- apply(f)¶
m a -> m (a -> b) -> m b
self :: m a
f :: m (a -> b)
Default implementation is based on
bindandmap. In order to usebind, let’s write its type as follows:bind :: m (a -> b) -> ((a -> b) -> m b) -> m b
Let’s also use a simple helper function:
h = g -> map g self :: (a -> b) -> m b
Now:
bind f h :: m b
- apply_first(x)¶
Combine two actions, keeping only the result of the first
Apply f => f a -> f b -> f a
- apply_second(x)¶
Combine two actions, keeping only the result of the second
Apply f => f a -> f b -> f b
- bind(f)[source]¶
m a -> (a -> m b) -> m b
Default implementation is based on
joinandmap:self :: m a
f :: a -> m b
map f :: m a -> m (m b)
join :: m (m b) -> m b
- flap(x)¶
Functor f => f (a -> b) -> a - > f b
- join()¶
m (m a) -> m a
Default implementation is based on
bind:self :: m (m a)
identity :: m a -> m a
bind :: m (m a) -> (m a -> m a) -> m a
- map(f)[source]¶
m a -> (a -> b) -> m b
Default implementation is based on
bindandpure. This implementation needs to be provided because the default implementation ofapplyusesmapthus creating a circular dependency between the defaultmapdefined inApplicative.
- replace(x)¶
Haskell ($>) operator