Lens.hs: Complete the easier part of this Lens exercise

Signed-off-by: Sanchayan Maity <maitysanchayan@gmail.com>
This commit is contained in:
Sanchayan Maity 2019-08-27 20:37:20 +05:30
parent b0f7bf2c06
commit 9c83801470

View file

@ -98,7 +98,7 @@ import Prelude hiding (product)
-- --
-- class (Foldable t, Functor t) => Traversable t where -- class (Foldable t, Functor t) => Traversable t where
-- traverse :: -- traverse ::
-- Applicative f => -- Applicative f =>
-- (a -> f b) -- (a -> f b)
-- -> t a -- -> t a
-- -> f (t b) -- -> f (t b)
@ -111,17 +111,15 @@ fmapT ::
(a -> b) (a -> b)
-> t a -> t a
-> t b -> t b
fmapT = fmapT f = getIdentity . traverse (Identity . f)
error "todo: fmapT"
-- | Let's refactor out the call to @traverse@ as an argument to @fmapT@. -- | Let's refactor out the call to @traverse@ as an argument to @fmapT@.
over :: over ::
((a -> Identity b) -> s -> Identity t) ((a -> Identity b) -> s -> Identity t)
-> (a -> b) -> (a -> b)
-> s -> s
-> t -> t
over = over t f = getIdentity . t (Identity . f)
error "todo: over"
-- | Here is @fmapT@ again, passing @traverse@ to @over@. -- | Here is @fmapT@ again, passing @traverse@ to @over@.
fmapTAgain :: fmapTAgain ::
@ -129,8 +127,7 @@ fmapTAgain ::
(a -> b) (a -> b)
-> t a -> t a
-> t b -> t b
fmapTAgain = fmapTAgain = over traverse
error "todo: fmapTAgain"
-- | Let's create a type-alias for this type of function. -- | Let's create a type-alias for this type of function.
type Set s t a b = type Set s t a b =
@ -142,23 +139,20 @@ type Set s t a b =
-- unwrapping. -- unwrapping.
sets :: sets ::
((a -> b) -> s -> t) ((a -> b) -> s -> t)
-> Set s t a b -> Set s t a b
sets = sets f g = Identity . f (getIdentity . g)
error "todo: sets"
mapped :: mapped ::
Functor f => Functor f =>
Set (f a) (f b) a b Set (f a) (f b) a b
mapped = mapped f g = Identity (getIdentity . f <$> g)
error "todo: mapped"
set :: set ::
Set s t a b Set s t a b
-> s -> s
-> b -> b
-> t -> t
set = set f s b = over f (const b) s
error "todo: set"
---- ----
@ -170,8 +164,7 @@ foldMapT ::
(a -> b) (a -> b)
-> t a -> t a
-> b -> b
foldMapT = foldMapT f = getConst . traverse (Const . f)
error "todo: foldMapT"
-- | Let's refactor out the call to @traverse@ as an argument to @foldMapT@. -- | Let's refactor out the call to @traverse@ as an argument to @foldMapT@.
foldMapOf :: foldMapOf ::
@ -179,8 +172,7 @@ foldMapOf ::
-> (a -> r) -> (a -> r)
-> s -> s
-> r -> r
foldMapOf = foldMapOf t f = getConst . t (Const . f)
error "todo: foldMapOf"
-- | Here is @foldMapT@ again, passing @traverse@ to @foldMapOf@. -- | Here is @foldMapT@ again, passing @traverse@ to @foldMapOf@.
foldMapTAgain :: foldMapTAgain ::
@ -188,8 +180,7 @@ foldMapTAgain ::
(a -> b) (a -> b)
-> t a -> t a
-> b -> b
foldMapTAgain = foldMapTAgain = foldMapOf traverse
error "todo: foldMapTAgain"
-- | Let's create a type-alias for this type of function. -- | Let's create a type-alias for this type of function.
type Fold s t a b = type Fold s t a b =
@ -206,14 +197,12 @@ folds ::
-> (a -> Const b a) -> (a -> Const b a)
-> s -> s
-> Const t s -> Const t s
folds = folds f g s = let t = (f (getConst . g) s) in Const t
error "todo: folds"
folded :: folded ::
Foldable f => Foldable f =>
Fold (f a) (f a) a a Fold (f a) (f a) a a
folded = folded = folds foldMap
error "todo: folded"
---- ----
@ -227,8 +216,7 @@ get ::
Get a s a Get a s a
-> s -> s
-> a -> a
get = get f = getConst . f Const
error "todo: get"
---- ----
@ -243,20 +231,20 @@ type Traversal s t a b =
-- | Traverse both sides of a pair. -- | Traverse both sides of a pair.
both :: both ::
Traversal (a, a) (b, b) a b Traversal (a, a) (b, b) a b
both = -- both :: (a -> f b) -> (a , a) -> f (b , b)
error "todo: both" both f (a, b) = (,) <$> f a <*> f b
-- | Traverse the left side of @Either@. -- | Traverse the left side of @Either@.
traverseLeft :: traverseLeft ::
Traversal (Either a x) (Either b x) a b Traversal (Either a x) (Either b x) a b
traverseLeft = traverseLeft f (Left a) = Left <$> f a
error "todo: traverseLeft" traverseLeft _ (Right x) = pure $ Right x
-- | Traverse the right side of @Either@. -- | Traverse the right side of @Either@.
traverseRight :: traverseRight ::
Traversal (Either x a) (Either x b) a b Traversal (Either x a) (Either x b) a b
traverseRight = traverseRight _ (Left x) = pure $ Left x
error "todo: traverseRight" traverseRight f (Right b) = Right <$> f b
type Traversal' a b = type Traversal' a b =
Traversal a a b b Traversal a a b b
@ -286,11 +274,10 @@ type Prism s t a b =
_Left :: _Left ::
Prism (Either a x) (Either b x) a b Prism (Either a x) (Either b x) a b
_Left = _Left = _
error "todo: _Left"
_Right :: _Right ::
Prism (Either x a) (Either x b) a b Prism (Either x a) (Either x b) a b
_Right = _Right =
error "todo: _Right" error "todo: _Right"
@ -396,7 +383,7 @@ fmodify ::
Lens s t a b Lens s t a b
-> (a -> f b) -> (a -> f b)
-> s -> s
-> f t -> f t
fmodify _ _ _ = fmodify _ _ _ =
error "todo: fmodify" error "todo: fmodify"
@ -700,7 +687,7 @@ setCityAndLocality ::
(Person, Address) -> (String, Locality) -> (Person, Address) (Person, Address) -> (String, Locality) -> (Person, Address)
setCityAndLocality = setCityAndLocality =
error "todo: setCityAndLocality" error "todo: setCityAndLocality"
-- | -- |
-- --
-- >>> getSuburbOrCity (Left maryAddress) -- >>> getSuburbOrCity (Left maryAddress)