Skip to content

Commit

Permalink
Rename module and add a new test
Browse files Browse the repository at this point in the history
  • Loading branch information
paf31 committed Jan 16, 2017
1 parent 9cb64eb commit a5c44b1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Data.Lens.Prism.Partial
( unsafeViewPrism, (^?!)
module Data.Lens.Fold.Partial
( unsafeView, (^?!)
, unsafeIndexedFold, (^@?!)
)
where
Expand All @@ -14,23 +14,18 @@ import Data.Tuple(Tuple(..))
import Data.Newtype (unwrap)
import Partial.Unsafe (unsafeCrashWith)

unsafeViewPrism :: forall s t a b. Partial => s -> Fold (First a) s t a b -> a
unsafeViewPrism s l = fromMaybe' (crash "unsafeViewPrism: Empty fold") $ previewOn s l

infixl 8 unsafeViewPrism as ^?!
unsafeView :: forall s t a b. Partial => s -> Fold (First a) s t a b -> a
unsafeView s l = fromMaybe' (\_ -> unsafeCrashWith "unsafeView: Empty fold") $ previewOn s l

infixl 8 unsafeView as ^?!

unsafeIndexedFold
:: forall i s t a b. Partial
=> s
-> IndexedFold ((First (Tuple i a))) i s t a b
-> (Tuple i a)
unsafeIndexedFold s l = fromMaybe' (crash "unsafeIndexedFold: empty Fold")
-> IndexedFold (First (Tuple i a)) i s t a b
-> Tuple i a
unsafeIndexedFold s l = fromMaybe' (\_ -> unsafeCrashWith "unsafeIndexedFold: empty Fold")
$ unwrap
$ ifoldMapOf l (\i a -> First $ Just (Tuple i a)) s

infixl 8 unsafeIndexedFold as ^@?!


crash :: forall a. String -> Unit -> a
crash msg _ = unsafeCrashWith msg
10 changes: 3 additions & 7 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ import Data.Lens.Index (ix)
import Data.Lens.Setter (iover)
import Data.Lens.Lens (ilens, IndexedLens, cloneIndexedLens)
import Data.Lens.Fold ((^?))
import Data.Lens.Prism.Partial ((^?!))
import Data.Lens.Fold.Partial ((^?!), (^@?!))
import Data.Lens.Zoom (Traversal, Traversal', Lens, Lens', zoom)
import Data.Tuple (Tuple(..))
import Data.Maybe (Maybe(..))
import Data.Either (Either(..))
import Partial.Unsafe (unsafePartial)

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)
import Control.Monad.State (evalState, get)

import Partial.Unsafe (unsafePartial)

-- Traversing an array nested within a record
foo :: forall a b r. Lens { foo :: a | r } { foo :: b | r } a b
Expand All @@ -34,7 +32,6 @@ doc = { foo: Just { bar: [ "Hello", " ", "World" ]} }
bars :: forall a b. Traversal (Foo a) (Foo b) a b
bars = foo <<< _Just <<< bar <<< traversed


-- Get the index of a deeply nested record
type BarRec = { foo :: Array (Either { bar :: Array Int } String) }
data Bar = Bar BarRec
Expand All @@ -51,8 +48,6 @@ _0Justbar = _Bar <<< foo <<< ix 0
_1bars :: Traversal' Bar Int
_1bars = _0Justbar <<< _Left <<< bar <<< ix 1



-- Tests state using zoom
stateTest :: Tuple Int String
stateTest = evalState go (Tuple 4 ["Foo", "Bar"]) where
Expand All @@ -70,5 +65,6 @@ main = do
logShow $ view bars doc
logShow $ doc2 ^? _1bars
logShow $ unsafePartial $ doc2 ^?! _1bars
logShow $ unsafePartial $ Tuple 0 1 ^@?! i_2
logShow stateTest
logShow cloneTest

0 comments on commit a5c44b1

Please sign in to comment.