git.iko.soy/semi-iso-optics
on master help sign in
Do not export unidirectional folds.

Added simple bifolds.

commit71aa321931155bbfc4d915d5c395396020f5be82 authorPaweł Nowak <pawel834@gmail.com> dateSun 23 Nov 2014, 13:37 (12 years ago) parent1799ff1
1 file changed+50−19
Control/Lens/SemiIso.hs+50−19
Control/Lens/SemiIso.hs+50−19
@@ -79,19 +79,16 @@ module Control.Lens.SemiIso (
7979 attemptAp_,
8080 attemptUn_,
8181
82− -- * Folds.
83− foldlM1,
84− foldrM1,
85− unfoldlM,
86− unfoldlM1,
87− unfoldrM,
88− unfoldrM1,
89−
9082 -- * Bidirectional folds.
9183 bifoldr,
9284 bifoldr1,
9385 bifoldl,
94− bifoldl1
86+ bifoldl1,
87+
88+ bifoldr_,
89+ bifoldr1_,
90+ bifoldl_,
91+ bifoldl1_
9592 ) where
9693
9794import Control.Arrow
@@ -300,13 +297,47 @@ unfoldlM1 f a0 = go a0 []
300297 Just (new_a, b) -> go new_a (b : bs)
301298 Nothing -> return (a : bs)
302299
300+-- | Constructs a bidirectional fold.
301+--
302+-- \-> Right unfolds using the (->) part of the given semi-iso, until it fails.
303+--
304+-- \<- Right folds using the (<-) part of the given semi-iso.
305+bifoldr :: ASemiIso' a (b, a) -> SemiIso' a (a, [b])
306+bifoldr = bifoldr_ . attemptAp_
307+
308+-- | Constructs a bidirectional fold.
309+--
310+-- \-> Right unfolds using the (->) part of the given semi-iso, until it fails.
311+-- It should produce a non-empty list.
312+--
313+-- \<- Right folds a non-empty list using the (<-) part of the given semi-iso.
314+bifoldr1 :: ASemiIso' a (a, a) -> SemiIso' a [a]
315+bifoldr1 = bifoldr1_ . attemptAp_
316+
317+-- | Constructs a bidirectional fold.
318+--
319+-- \-> Left unfolds using the (->) part of the given semi-iso, until it fails.
320+--
321+-- \<- Left folds using the (<-) part of the given semi-iso.
322+bifoldl :: ASemiIso' a (a, b) -> SemiIso' a (a, [b])
323+bifoldl = bifoldl_ . attemptAp_
324+
325+-- | Constructs a bidirectional fold.
326+--
327+-- \-> Left unfolds using the (->) part of the given semi-iso, until it fails.
328+-- It should produce a non-empty list.
329+--
330+-- \<- Left folds a non-empty list using the (<-) part of the given semi-iso.
331+bifoldl1 :: ASemiIso' a (a, a) -> SemiIso' a [a]
332+bifoldl1 = bifoldl1_ . attemptAp_
333+
303334-- | Constructs a bidirectional fold.
304335--
305336-- \-> Right unfolds using the (->) part of the given semi-iso.
306337--
307338-- \<- Right folds using the (<-) part of the given semi-iso.
308−bifoldr :: ASemiIso a a (Maybe (b, a)) (b, a) -> SemiIso' a (a, [b])
309−bifoldr ai = semiIso (uf ai) (f ai)
339+bifoldr_ :: ASemiIso a a (Maybe (b, a)) (b, a) -> SemiIso' a (a, [b])
340+bifoldr_ ai = semiIso (uf ai) (f ai)
310341 where
311342 f = uncurry . foldrM . curry . unapply
312343 uf = unfoldrM . apply
@@ -314,11 +345,11 @@ bifoldr ai = semiIso (uf ai) (f ai)
314345-- | Constructs a bidirectional fold.
315346--
316347-- \-> Right unfolds using the (->) part of the given semi-iso. It should
317−-- produce an non-empty list.
348+-- produce a non-empty list.
318349--
319350-- \<- Right folds a non-empty list using the (<-) part of the given semi-iso.
320−bifoldr1 :: ASemiIso a a (Maybe (a, a)) (a, a) -> SemiIso' a [a]
321−bifoldr1 ai = semiIso (uf ai) (f ai)
351+bifoldr1_ :: ASemiIso a a (Maybe (a, a)) (a, a) -> SemiIso' a [a]
352+bifoldr1_ ai = semiIso (uf ai) (f ai)
322353 where
323354 f = foldrM1 . curry . unapply
324355 uf = unfoldrM1 . apply
@@ -328,8 +359,8 @@ bifoldr1 ai = semiIso (uf ai) (f ai)
328359-- \-> Left unfolds using the (->) part of the given semi-iso.
329360--
330361-- \<- Left folds using the (<-) part of the given semi-iso.
331−bifoldl :: ASemiIso a a (Maybe (a, b)) (a, b) -> SemiIso' a (a, [b])
332−bifoldl ai = semiIso (uf ai) (f ai)
362+bifoldl_ :: ASemiIso a a (Maybe (a, b)) (a, b) -> SemiIso' a (a, [b])
363+bifoldl_ ai = semiIso (uf ai) (f ai)
333364 where
334365 f = uncurry . foldlM . curry . unapply
335366 uf = unfoldlM . apply
@@ -337,11 +368,11 @@ bifoldl ai = semiIso (uf ai) (f ai)
337368-- | Constructs a bidirectional fold.
338369--
339370-- \-> Left unfolds using the (->) part of the given semi-iso. It should
340−-- produce an non-empty list.
371+-- produce a non-empty list.
341372--
342373-- \<- Left folds a non-empty list using the (<-) part of the given semi-iso.
343−bifoldl1 :: ASemiIso a a (Maybe (a, a)) (a, a) -> SemiIso' a [a]
344−bifoldl1 ai = semiIso (uf ai) (f ai)
374+bifoldl1_ :: ASemiIso a a (Maybe (a, a)) (a, a) -> SemiIso' a [a]
375+bifoldl1_ ai = semiIso (uf ai) (f ai)
345376 where
346377 f = foldlM1 . curry . unapply
347378 uf = unfoldlM1 . apply