@@ -7,6 +7,7 @@ module Data.Foldable
77 , for_
88 , sequence_
99 , oneOf
10+ , oneOfMap
1011 , intercalate
1112 , surroundMap
1213 , surround
@@ -18,6 +19,8 @@ module Data.Foldable
1819 , product
1920 , elem
2021 , notElem
22+ , indexl
23+ , indexr
2124 , find
2225 , findMap
2326 , maximum
@@ -31,11 +34,9 @@ module Data.Foldable
3134import Prelude
3235
3336import Control.Plus (class Plus , alt , empty )
34-
3537import Data.Maybe (Maybe (..))
3638import Data.Maybe.First (First (..))
3739import Data.Maybe.Last (Last (..))
38- import Data.Monoid (class Monoid , mempty )
3940import Data.Monoid.Additive (Additive (..))
4041import Data.Monoid.Conj (Conj (..))
4142import Data.Monoid.Disj (Disj (..))
@@ -170,11 +171,11 @@ instance foldableMultiplicative :: Foldable Multiplicative where
170171
171172-- | Fold a data structure, accumulating values in some `Monoid`.
172173fold :: forall f m . Foldable f => Monoid m => f m -> m
173- fold = foldMap id
174+ fold = foldMap identity
174175
175- -- | Similar to 'foldl', but the result is encapsulated in a monad.
176+ -- | Similar to 'foldl', but the result is encapsulated in a monad.
176177-- |
177- -- | Note: this function is not generally stack-safe, e.g., for monads which
178+ -- | Note: this function is not generally stack-safe, e.g., for monads which
178179-- | build up thunks a la `Eff`.
179180foldM :: forall f m a b . Foldable f => Monad m => (a -> b -> m a ) -> a -> f b -> m a
180181foldM f a0 = foldl (\ma b -> ma >>= flip f b) (pure a0)
@@ -227,12 +228,16 @@ for_ = flip traverse_
227228-- | sequence_ [ trace "Hello, ", trace " world!" ]
228229-- | ```
229230sequence_ :: forall a f m . Applicative m => Foldable f => f (m a ) -> m Unit
230- sequence_ = traverse_ id
231+ sequence_ = traverse_ identity
231232
232233-- | Combines a collection of elements using the `Alt` operation.
233234oneOf :: forall f g a . Foldable f => Plus g => f (g a ) -> g a
234235oneOf = foldr alt empty
235236
237+ -- | Folds a structure into some `Plus`.
238+ oneOfMap :: forall f g a b . Foldable f => Plus g => (a -> g b ) -> f a -> g b
239+ oneOfMap f = foldr (alt <<< f) empty
240+
236241-- | Fold a data structure, accumulating values in some `Monoid`,
237242-- | combining adjacent elements using the specified separator.
238243intercalate :: forall f m . Foldable f => Monoid m => m -> f m -> m
@@ -280,19 +285,19 @@ surroundMap d t f = unwrap (foldMap joined f) d
280285-- | = "*1*2*3*"
281286-- | ```
282287surround :: forall f m . Foldable f => Semigroup m => m -> f m -> m
283- surround d = surroundMap d id
288+ surround d = surroundMap d identity
284289
285290-- | The conjunction of all the values in a data structure. When specialized
286291-- | to `Boolean`, this function will test whether all of the values in a data
287292-- | structure are `true`.
288293and :: forall a f . Foldable f => HeytingAlgebra a => f a -> a
289- and = all id
294+ and = all identity
290295
291296-- | The disjunction of all the values in a data structure. When specialized
292297-- | to `Boolean`, this function will test whether any of the values in a data
293298-- | structure is `true`.
294299or :: forall a f . Foldable f => HeytingAlgebra a => f a -> a
295- or = any id
300+ or = any identity
296301
297302-- | `all f` is the same as `and <<< map f`; map a function over the structure,
298303-- | and then get the conjunction of the results.
@@ -320,6 +325,30 @@ elem = any <<< (==)
320325notElem :: forall a f . Foldable f => Eq a => a -> f a -> Boolean
321326notElem x = not <<< elem x
322327
328+ -- | Try to get nth element from the left in a data structure
329+ indexl :: forall a f . Foldable f => Int -> f a -> Maybe a
330+ indexl idx = _.elem <<< foldl go { elem: Nothing , pos: 0 }
331+ where
332+ go cursor a =
333+ case cursor.elem of
334+ Just _ -> cursor
335+ _ ->
336+ if cursor.pos == idx
337+ then { elem: Just a, pos: cursor.pos }
338+ else { pos: cursor.pos + 1 , elem: cursor.elem }
339+
340+ -- | Try to get nth element from the right in a data structure
341+ indexr :: forall a f . Foldable f => Int -> f a -> Maybe a
342+ indexr idx = _.elem <<< foldr go { elem: Nothing , pos: 0 }
343+ where
344+ go a cursor =
345+ case cursor.elem of
346+ Just _ -> cursor
347+ _ ->
348+ if cursor.pos == idx
349+ then { elem: Just a, pos: cursor.pos }
350+ else { pos: cursor.pos + 1 , elem: cursor.elem }
351+
323352-- | Try to find an element in a data structure which satisfies a predicate.
324353find :: forall a f . Foldable f => (a -> Boolean ) -> f a -> Maybe a
325354find p = foldl go Nothing
0 commit comments