···110110 (a -> b)
111111 -> t a
112112 -> t b
113113-fmapT =
114114- error "todo: fmapT"
113113+fmapT f =
114114+ getIdentity . traverse (Identity . f)
115115116116-- | Let's refactor out the call to @traverse@ as an argument to @fmapT@.
117117over ::
···119119 -> (a -> b)
120120 -> s
121121 -> t
122122-over =
123123- error "todo: over"
122122+over t f =
123123+ getIdentity . t (Identity . f)
124124125125-- | Here is @fmapT@ again, passing @traverse@ to @over@.
126126fmapTAgain ::
···129129 -> t a
130130 -> t b
131131fmapTAgain =
132132- error "todo: fmapTAgain"
132132+ over traverse
133133134134-- | Let's create a type-alias for this type of function.
135135type Set s t a b =
···142142sets ::
143143 ((a -> b) -> s -> t)
144144 -> Set s t a b
145145-sets =
146146- error "todo: sets"
145145+sets t f =
146146+ Identity . t (getIdentity . f)
147147148148mapped ::
149149 Functor f =>
150150 Set (f a) (f b) a b
151151mapped =
152152- error "todo: mapped"
152152+ sets fmap
153153154154set ::
155155 Set s t a b
156156 -> s
157157 -> b
158158 -> t
159159-set =
160160- error "todo: set"
159159+set t s b =
160160+ over t (const b) s
161161162162----
163163···169169 (a -> b)
170170 -> t a
171171 -> b
172172-foldMapT =
173173- error "todo: foldMapT"
172172+foldMapT f =
173173+ getConst . traverse (Const . f)
174174175175-- | Let's refactor out the call to @traverse@ as an argument to @foldMapT@.
176176foldMapOf ::
···178178 -> (a -> r)
179179 -> s
180180 -> r
181181-foldMapOf =
182182- error "todo: foldMapOf"
181181+foldMapOf t f =
182182+ getConst . t (Const . f)
183183184184-- | Here is @foldMapT@ again, passing @traverse@ to @foldMapOf@.
185185foldMapTAgain ::
···188188 -> t a
189189 -> b
190190foldMapTAgain =
191191- error "todo: foldMapTAgain"
191191+ foldMapOf traverse
192192193193-- | Let's create a type-alias for this type of function.
194194type Fold s t a b =
···205205 -> (a -> Const b a)
206206 -> s
207207 -> Const t s
208208-folds =
209209- error "todo: folds"
208208+folds t f =
209209+ Const . t (getConst . f)
210210211211folded ::
212212 Foldable f =>
213213 Fold (f a) (f a) a a
214214folded =
215215- error "todo: folded"
215215+ folds foldMap
216216217217----
218218···226226 Get a s a
227227 -> s
228228 -> a
229229-get =
230230- error "todo: get"
229229+get t =
230230+ getConst . t Const
231231232232----
233233···242242-- | Traverse both sides of a pair.
243243both ::
244244 Traversal (a, a) (b, b) a b
245245-both =
246246- error "todo: both"
245245+both f (a, b) =
246246+ (,) <$> f a <*> f b
247247248248-- | Traverse the left side of @Either@.
249249traverseLeft ::
250250 Traversal (Either a x) (Either b x) a b
251251-traverseLeft =
252252- error "todo: traverseLeft"
251251+traverseLeft f (Left a) =
252252+ Left <$> f a
253253+traverseLeft _ (Right x) =
254254+ pure (Right x)
253255254256-- | Traverse the right side of @Either@.
255257traverseRight ::
256258 Traversal (Either x a) (Either x b) a b
257257-traverseRight =
258258- error "todo: traverseRight"
259259+traverseRight _ (Left x) =
260260+ pure (Left x)
261261+traverseRight f (Right a) =
262262+ Right <$> f a
259263260264type Traversal' a b =
261265 Traversal a a b b
···286290_Left ::
287291 Prism (Either a x) (Either b x) a b
288292_Left =
289289- error "todo: _Left"
293293+ dimap (either Right (Left . Right)) (either pure (fmap Left)) . right
290294291295_Right ::
292296 Prism (Either x a) (Either x b) a b
293297_Right =
294294- error "todo: _Right"
298298+ dimap (either (Left . Left) Right) (either pure (fmap Right)) . right
295299296300prism ::
297301 (b -> t)
298302 -> (s -> Either t a)
299303 -> Prism s t a b
300300-prism =
301301- error "todo: prism"
304304+prism to fr =
305305+ dimap fr (either pure (fmap to)) . right
302306303307_Just ::
304308 Prism (Maybe a) (Maybe b) a b
305309_Just =
306306- error "todo: _Just"
310310+ prism
311311+ Just
312312+ (maybe (Left Nothing) Right)
307313308314_Nothing ::
309315 Prism (Maybe a) (Maybe a) () ()
310316_Nothing =
311311- error "todo: _Nothing"
317317+ prism
318318+ (\() -> Nothing)
319319+ (maybe (Right ()) (Left . Just))
312320313321setP ::
314322 Prism s t a b
315323 -> s
316324 -> Either t a
317317-setP =
318318- error "todo: setP"
325325+setP p =
326326+ either Right Left . p Left
319327320328getP ::
321329 Prism s t a b
322330 -> b
323331 -> t
324324-getP =
325325- error "todo: getP"
332332+getP p =
333333+ getIdentity . getTagged . p . Tagged . Identity
326334327335type Prism' a b =
328336 Prism a a b b
···345353 -> (a -> b)
346354 -> s
347355 -> t
348348-modify =
349349- error "todo: modify"
356356+modify r f =
357357+ getIdentity . r (Identity . f)
350358351359-- | An alias for @modify@.
352360(%~) ::
···375383 -> b
376384 -> s
377385 -> t
378378-(.~) =
379379- error "todo: (.~)"
386386+(.~) l =
387387+ modify l . const
380388381389infixl 5 .~
382390···396404 -> (a -> f b)
397405 -> s
398406 -> f t
399399-fmodify =
400400- error "todo: fmodify"
407407+fmodify l =
408408+ l
401409402410-- |
403411--
···412420 -> f b
413421 -> s
414422 -> f t
415415-(|=) =
416416- error "todo: (|=)"
423423+(|=) l =
424424+ fmodify l . const
417425418426infixl 5 |=
419427···423431-- (30,"abc")
424432fstL ::
425433 Lens (a, x) (b, x) a b
426426-fstL =
427427- error "todo: fstL"
434434+fstL p (x, y) =
435435+ fmap (\x' -> (x', y)) (p x)
428436429437-- |
430438--
···432440-- (13,"abcdef")
433441sndL ::
434442 Lens (x, a) (x, b) a b
435435-sndL =
436436- error "todo: sndL"
443443+sndL p (x, y) =
444444+ fmap (\y' -> (x, y')) (p y)
437445438446-- |
439447--
···458466 Ord k =>
459467 k
460468 -> Lens (Map k v) (Map k v) (Maybe v) (Maybe v)
461461-mapL =
462462- error "todo: mapL"
469469+mapL k p m =
470470+ let z = Map.lookup k m
471471+ in fmap (\y -> case y of
472472+ Just v -> Map.insert k v m
473473+ Nothing -> case z of
474474+ Just _ -> Map.delete k m
475475+ Nothing -> m) (p z)
463476464477-- |
465478--
···484497 Ord k =>
485498 k
486499 -> Lens (Set.Set k) (Set.Set k) Bool Bool
487487-setL =
488488- error "todo: setL"
500500+setL k =
501501+ (\p s -> fmap (\b -> bool Set.delete Set.insert b k s) (p (Set.member k s)))
489502490503-- |
491504--
···498511 Lens s t a b
499512 -> Lens q r s t
500513 -> Lens q r a b
501501-compose =
502502- error "todo: compose"
514514+compose r1 r2 =
515515+ r2 . r1
503516504517-- | An alias for @compose@.
505518(|.) ::
···521534identity ::
522535 Lens a b a b
523536identity =
524524- error "todo: identity"
537537+ id
525538526539-- |
527540--
···534547 Lens s t a b
535548 -> Lens q r c d
536549 -> Lens (s, q) (t, r) (a, c) (b, d)
537537-product =
538538- error "todo: product"
550550+product r1 r2 p (a, c) =
551551+ getAlongsideRight (r2 (\b2 -> AlongsideRight (
552552+ getAlongsideLeft (r1 (\b1 -> AlongsideLeft (
553553+ p (b1,b2))) a))) c)
539554540555-- | An alias for @product@.
541556(***) ::
···564579 Lens s t a b
565580 -> Lens q r a b
566581 -> Lens (Either s q) (Either t r) a b
567567-choice =
568568- error "todo: choice"
582582+choice r1 r2 =
583583+584584+ (\p e -> case e of
585585+ Left a -> fmap Left (r1 p a)
586586+ Right b -> fmap Right (r2 p b))
569587570588-- | An alias for @choice@.
571589(|||) ::
···649667 Person
650668 -> String
651669getSuburb =
652652- error "todo: getSuburb"
670670+ get (suburbL |. addressL)
653671654672-- |
655673--
···663681 -> String
664682 -> Person
665683setStreet =
666666- error "todo: setStreet"
684684+ set (streetL |. addressL)
667685668686-- |
669687--
···676694 (Person, Locality)
677695 -> (Int, String)
678696getAgeAndCountry =
679679- error "todo: getAgeAndCountry"
697697+ get (ageL *** countryL)
680698681699-- |
682700--
···688706setCityAndLocality ::
689707 (Person, Address) -> (String, Locality) -> (Person, Address)
690708setCityAndLocality =
691691- error "todo: setCityAndLocality"
709709+ set (cityL |. localityL |. addressL *** localityL)
692710693711-- |
694712--
···701719 Either Address Locality
702720 -> String
703721getSuburbOrCity =
704704- error "todo: getSuburbOrCity"
722722+ get (suburbL ||| cityL)
705723706724-- |
707725--
···715733 -> String
716734 -> Either Person Locality
717735setStreetOrState =
718718- error "todo: setStreetOrState"
736736+ set (streetL |. addressL ||| stateL)
719737720738-- |
721739--
···728746 Person
729747 -> Person
730748modifyCityUppercase =
731731- error "todo: modifyCityUppercase"
749749+ cityL |. localityL |. addressL %~ map toUpper
732750733751-- |
734752--
···741759 IntAnd [a]
742760 -> IntAnd Bool
743761modifyIntAndLengthEven =
744744- error "todo: modifyIntAndLengthEven"
762762+ intAndL %~ even . length
745763746764----
747765···751769-- Locality "ABC" "DEF" "GHI"
752770traverseLocality ::
753771 Traversal' Locality String
754754-traverseLocality =
755755- error "todo: traverseLocality"
772772+traverseLocality f (Locality c t y) =
773773+ Locality <$> f c <*> f t <*> f y
756774757775-- |
758776--
···764782intOrIntP ::
765783 Prism' (IntOr a) Int
766784intOrIntP =
767767- error "todo: intOrIntP"
785785+ prism
786786+ IntOrIs
787787+ (\i -> case i of
788788+ IntOrIs n -> Right n
789789+ IntOrIsNot a -> Left (IntOrIsNot a))
768790769791intOrP ::
770792 Prism (IntOr a) (IntOr b) a b
771793intOrP =
772772- error "todo: intOrP"
794794+ prism
795795+ IntOrIsNot
796796+ (\i -> case i of
797797+ IntOrIs n -> Left (IntOrIs n)
798798+ IntOrIsNot a -> Right a)
773799774800-- |
775801--
···785811 IntOr [a]
786812 -> IntOr Bool
787813intOrLengthEven =
788788- error "todo: intOrLengthEven"
814814+ over intOrP (even . length)