1
1
mirror of https://github.com/thma/LtuPatternFactory.git synced 2024-12-02 08:33:20 +03:00
This commit is contained in:
thma 2018-11-12 20:47:25 +01:00
commit 2badd11048

View File

@ -22,7 +22,7 @@ I think this kind of exposition could be helpful if you are either:
- [Lambda the ultimate pattern factory](#lambda-the-ultimate-pattern-factory)
- [The Patternopedia](#the-patternopedia)
- [Strategy -> Functor](#strategy---functor)
- [Singleton -> Pointed -> Applicative](#singleton---pointed---applicative)
- [Singleton -> Applicative](#singleton---applicative)
- [Pipeline -> Monad](#pipeline---monad)
- [NullObject -> Maybe Monad](#nullobject---maybe-monad)
- [Composite -> SemiGroup -> Monoid](#composite---semigroup---monoid)
@ -68,7 +68,7 @@ strategyDouble n = 2*n
-- now we define a context that applies a function of type Num a => a -> a to a list of a's:
context :: Num a => (a -> a) -> [a] -> [a]
context f l = map f l
-- using point-free notation this can be written as:
-- using pointfree style this can be written as:
context = map
```
The `context` function uses higher order `map` function (`map :: (a -> b) -> [a] -> [b]`) to apply the strategies to lists of numbers:
@ -93,7 +93,7 @@ Although it would be fair to say that the type class `Functor` captures the esse
[Full Sourcecode for this section](https://github.com/thma/LtuPatternFactory/blob/master/src/Strategy.hs)
## Singleton -> Pointed -> Applicative
## Singleton -> Applicative
> "The singleton pattern is a software design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system."
> (quoted from https://en.wikipedia.org/wiki/Singleton_pattern)
@ -112,18 +112,6 @@ Via the `let`-Binding we can thread the singleton through arbitrary code in the
Type classes provide several tools to make this kind of threading more convenient or even to avoid explicit threading of instances.
### Using Pointed to create singletons
> "Given a Functor, the Pointed class represents the additional ability to put a value into a “default context.” Often, this corresponds to creating a container with exactly one element, but it is more general than that."
> (quoted from the Typeclassopedia)
```haskell
class Functor f => Pointed f where
pure :: a -> f a
```
### Using Applicative Functor for threading of singletons
The following code defines a simple expression evaluator:
@ -148,7 +136,7 @@ eval (Mul p q) env = eval p env * eval q env
Note how the explicit `env`parameter is threaded through the recursive eval calls. This is needed to have the
environment avalailable for variable lookup at any recursive call depth.
If we now bind `env` to a value as in the following snippet it is used as an imutable singleton within the recursive evaluation of `eval exp env`.
If we now bind `env` to a value as in the following snippet it is used as an immutable singleton within the recursive evaluation of `eval exp env`.
```haskell
main = do