Polymorphism and cases - Synook/funcpy GitHub Wiki
An important part of functional languages is the ability for functions to be polymorphic. A common example of this is a functor, which can be mapped over with fmap
. How fmap works for each functor is very different, however: for example, it should be the case that (fmap (* 2) (Just 3))
returns (Just 6)
, while (fmap (* 2) [1,2])
returns [2,4]
.
This is enabled in funcpy through the implementation of cases, which are a poor form of pattern matching. In essence, cases allow function aliases to "override" previous definitions of that function, but only in certain cases. For example,
fmap f v -> (case (type v "Maybe") (<something>))
would redefine fmap
to be <something>
, but only if the first argument of case
evaluated to true
. Otherwise, the original definition of fmap
would be evaluated instead. This allows for fmap
to be defined piecemeal for any applicable type.