on master help sign in
Unfinished "attempt" functions.
commitc8da5d2c1febee16ec30cbe97acf495b57a51575 authorPaweł Nowak <pawel834@gmail.com> dateThu 30 Oct 2014, 16:38 (12 years ago) parent40ee1da
1 file changed+30−1
Control/Lens/SemiIso.hs+30−1
Control/Lens/SemiIso.hs+30−1
@@ -43,8 +43,16 @@ module Control.Lens.SemiIso (
4343 -- * Constructing semi-isos.
4444 semiIso,
4545
46− -- * Consuming semi-isos.
46+ -- * Transforming semi-isos.
4747 withSemiIso,
48+ attempt,
49+ attemptAp,
50+ attemptUn,
51+ attempt_,
52+ attemptAp_,
53+ attemptUn_,
54+
55+ -- * Consuming semi-isos.
4856 fromSemi,
4957 apply,
5058 unapply,
@@ -114,6 +122,27 @@ withSemiIso :: ASemiIso s t a b
114122withSemiIso ai k = case ai (Retail Right (Right . Identity)) of
115123 Retail sa bt -> k sa (rmap (runIdentity . sequenceA) bt)
116124
125+attempt :: ASemiIso s t a b -> SemiIso s (Either String t) (Either String a) b
126+attempt = attemptAp . attemptUn
127+
128+attemptAp :: ASemiIso s t a b -> SemiIso s t (Either String a) b
129+attemptAp = undefined
130+
131+attemptUn :: ASemiIso s t a b -> SemiIso s (Either String t) a b
132+attemptUn = undefined
133+
134+discard :: Either a b -> Maybe b
135+discard = either (const Nothing) Just
136+
137+attempt_ :: ASemiIso s t a b -> SemiIso s (Maybe t) (Maybe a) b
138+attempt_ ai = rmap (fmap discard) . attempt ai . lmap discard
139+
140+attemptAp_ :: ASemiIso s t a b -> SemiIso s t (Maybe a) b
141+attemptAp_ ai = attemptAp ai . lmap discard
142+
143+attemptUn_ :: ASemiIso s t a b -> SemiIso s (Maybe t) a b
144+attemptUn_ ai = rmap (fmap discard) . attemptUn ai
145+
117146-- | Applies the 'SemiIso'.
118147apply :: ASemiIso s t a b -> s -> Either String a
119148apply ai = withSemiIso ai $ \l _ -> l