Skip to content

Commit 394a77b

Browse files
authored
Merge pull request paf31#2 from paf31/phil/closed
Closed instance
2 parents 251925c + 5c9757c commit 394a77b

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

generated-docs/Control/Fold.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ and produces output of type `b`.
3131
##### Instances
3232
``` purescript
3333
Profunctor Fold
34+
Closed Fold
3435
Functor (Fold a)
3536
Apply (Fold a)
3637
Applicative (Fold a)
@@ -87,6 +88,14 @@ Run a `Fold` by providing a `Traversable` container of inputs, and
8788
generating an output for each input. This is analogous to the `scanl` function from
8889
`Data.Traversable`.
8990

91+
#### `distributed`
92+
93+
``` purescript
94+
distributed :: forall f a b. Distributive f => Fold a b -> Fold (f a) (f b)
95+
```
96+
97+
Fold over entire collections of inputs, producing a collection of outputs.
98+
9099
#### `mconcat`
91100

92101
``` purescript

src/Control/Fold.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module Control.Fold
3939
, minimum
4040
, elem
4141
, notElem
42+
, distributed
4243
) where
4344

4445
import Prelude
@@ -48,11 +49,14 @@ import Control.Alt ((<|>))
4849
import Control.Apply (lift2)
4950
import Control.Comonad (class Comonad, extract)
5051
import Control.Extend (class Extend)
52+
import Data.Distributive (class Distributive, cotraverse)
5153
import Data.Foldable (class Foldable)
54+
import Data.Function (applyFlipped)
5255
import Data.HeytingAlgebra (ff, tt)
5356
import Data.Maybe (Maybe(..))
5457
import Data.Monoid (class Monoid, mempty)
5558
import Data.Profunctor (class Profunctor, dimap, lmap)
59+
import Data.Profunctor.Closed (class Closed, closed)
5660
import Data.Traversable (class Traversable)
5761

5862
-- | A left fold, which takes zero or more values of type `a` as input
@@ -89,6 +93,10 @@ foldl fold xs = extract (Foldable.foldl (\(Fold o) -> o.step) fold xs)
8993
scanl :: forall f a b. Traversable f => Fold a b -> f a -> f b
9094
scanl fold xs = map extract (Traversable.scanl (\(Fold o) -> o.step) fold xs)
9195

96+
-- | Fold over entire collections of inputs, producing a collection of outputs.
97+
distributed :: forall f a b. Distributive f => Fold a b -> Fold (f a) (f b)
98+
distributed = dimap applyFlipped (flip cotraverse id) <<< closed
99+
92100
-- | `Fold` values in some `Monoid`.
93101
mconcat :: forall m. Monoid m => Fold m m
94102
mconcat = unfoldFold_ mempty append
@@ -163,6 +171,9 @@ instance profunctorFold :: Profunctor Fold where
163171
step = f >>> o.step >>> dimap f g
164172
finish = o.finish >>> g
165173

174+
instance closedFold :: Closed Fold where
175+
closed f = unfoldFold (const f) (lift2 (flip stepFold)) (extract <<< _)
176+
166177
instance functorFold :: Functor (Fold a) where
167178
map f (Fold o) = Fold { step, finish }
168179
where

0 commit comments

Comments
 (0)