Skip to content

Commit 6eb23ee

Browse files
Add below to Prism.purs (#81)
* Add `below` to Prism.purs Ported from Haskell's `lens` library: https://hackage.haskell.org/package/lens-4.16/docs/src/Control-Lens-Prism.html#below * Force Ci to build via whitespace insertion * Drop whitespace insertion * Import APrism' * Export below * Import Traversable and traverse Co-authored-by: JordanMartinez <[email protected]> Co-authored-by: JordanMartinez <[email protected]>
1 parent a9a3b73 commit 6eb23ee

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/Data/Lens/Prism.purs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@
6565
-- | Solid Color.white # preview solidFocus <#> review solidFocus
6666
-- | == Solid Color.white
6767
-- | ```
68-
6968
module Data.Lens.Prism
7069
( prism', prism
7170
, only, nearly
7271
, review
7372
, is, isn't, matching
7473
, clonePrism, withPrism
74+
, below
7575
, module ExportTypes
7676
) where
7777

@@ -81,11 +81,12 @@ import Control.MonadPlus (guard)
8181
import Data.Either (Either(..), either)
8282
import Data.HeytingAlgebra (tt, ff)
8383
import Data.Lens.Types (Prism, Prism', APrism, APrism', Review, Review') as ExportTypes
84-
import Data.Lens.Types (Prism, Prism', APrism, Market(..), Review, Tagged(..))
84+
import Data.Lens.Types (Prism, Prism', APrism, APrism', Market(..), Review, Tagged(..))
8585
import Data.Maybe (Maybe, maybe)
8686
import Data.Newtype (under)
8787
import Data.Profunctor (dimap, rmap)
8888
import Data.Profunctor.Choice (right)
89+
import Data.Traversable (class Traversable, traverse)
8990

9091
-- | Create a `Prism` from a constructor and a matcher function that
9192
-- | produces an `Either`:
@@ -173,3 +174,24 @@ is l = either (const ff) (const tt) <<< matching l
173174
--| Ask if `preview prism` would produce a `Nothing`.
174175
isn't :: forall s t a b r. HeytingAlgebra r => APrism s t a b -> s -> r
175176
isn't l = not <<< is l
177+
178+
-- Ported from Haskell: https://hackage.haskell.org/package/lens-4.16/docs/src/Control-Lens-Prism.html#below
179+
-- | `lift` a `Prism` through a `Traversable` functor, giving a `Prism` that matches
180+
-- | only if all the elements of the container match the `Prism`.
181+
-- |
182+
-- | ``` purescript
183+
-- | >>> [Left 1, Right "foo", Left 4, Right "woot"]^..below _Right
184+
-- | []
185+
-- | ```
186+
-- |
187+
-- | ``` purescript
188+
-- | >>> [Right "hail hydra!", Right "foo", Right "blah", Right "woot"]^..below _Right
189+
-- | [["hail hydra!","foo","blah","woot"]]
190+
-- | ```
191+
below :: forall f s a. Traversable f => APrism' s a -> Prism' (f s) (f a)
192+
below k =
193+
withPrism k $ \bt seta ->
194+
prism (map bt) $ \s ->
195+
case traverse seta s of
196+
Left _ -> Left s
197+
Right t -> Right t

0 commit comments

Comments
 (0)