65
65
-- | Solid Color.white # preview solidFocus <#> review solidFocus
66
66
-- | == Solid Color.white
67
67
-- | ```
68
-
69
68
module Data.Lens.Prism
70
69
( prism' , prism
71
70
, only , nearly
72
71
, review
73
72
, is , isn't , matching
74
73
, clonePrism , withPrism
74
+ , below
75
75
, module ExportTypes
76
76
) where
77
77
@@ -81,11 +81,12 @@ import Control.MonadPlus (guard)
81
81
import Data.Either (Either (..), either )
82
82
import Data.HeytingAlgebra (tt , ff )
83
83
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 (..))
85
85
import Data.Maybe (Maybe , maybe )
86
86
import Data.Newtype (under )
87
87
import Data.Profunctor (dimap , rmap )
88
88
import Data.Profunctor.Choice (right )
89
+ import Data.Traversable (class Traversable , traverse )
89
90
90
91
-- | Create a `Prism` from a constructor and a matcher function that
91
92
-- | produces an `Either`:
@@ -173,3 +174,24 @@ is l = either (const ff) (const tt) <<< matching l
173
174
-- | Ask if `preview prism` would produce a `Nothing`.
174
175
isn't :: forall s t a b r . HeytingAlgebra r => APrism s t a b -> s -> r
175
176
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