Skip to content

Commit

Permalink
Merge pull request #10 from purescript/star
Browse files Browse the repository at this point in the history
Add Star
  • Loading branch information
paf31 committed Sep 6, 2015
2 parents d2dac84 + 314a0b5 commit 862bde8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/Data/Profunctor/Star.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## Module Data.Profunctor.Star

#### `Star`

``` purescript
newtype Star f a b
= Star (a -> f b)
```

`Star` turns a `Functor` into a `Profunctor`.

##### Instances
``` purescript
instance profunctorStar :: (Functor f) => Profunctor (Star f)
instance strongStar :: (Functor f) => Strong (Star f)
instance choiceStar :: (Applicative f) => Choice (Star f)
```

#### `runStar`

``` purescript
runStar :: forall f a b. Star f a b -> a -> f b
```

Unwrap a value of type `Star f a b`.


27 changes: 27 additions & 0 deletions src/Data/Profunctor/Star.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Data.Profunctor.Star where

import Prelude

import Data.Tuple
import Data.Either
import Data.Profunctor
import Data.Profunctor.Strong
import Data.Profunctor.Choice

-- | `Star` turns a `Functor` into a `Profunctor`.
newtype Star f a b = Star (a -> f b)

-- | Unwrap a value of type `Star f a b`.
runStar :: forall f a b. Star f a b -> a -> f b
runStar (Star f) = f

instance profunctorStar :: (Functor f) => Profunctor (Star f) where
dimap f g (Star ft) = Star (f >>> ft >>> map g)

instance strongStar :: (Functor f) => Strong (Star f) where
first (Star f) = Star \(Tuple s x) -> map (`Tuple` x) (f s)
second (Star f) = Star \(Tuple x s) -> map (Tuple x) (f s)

instance choiceStar :: (Applicative f) => Choice (Star f) where
left (Star f) = Star $ either (map Left <<< f) (pure <<< Right)
right (Star f) = Star $ either (pure <<< Left) (map Right <<< f)

0 comments on commit 862bde8

Please sign in to comment.