Skip to content

Commit ac01da0

Browse files
authored
Merge pull request #58 from LiamGoodacre/feature/delimit
Add surroundMap and surround
2 parents 65bfd30 + 920805a commit ac01da0

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/Data/Foldable.purs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module Data.Foldable
77
, sequence_
88
, oneOf
99
, intercalate
10+
, surroundMap
11+
, surround
1012
, and
1113
, or
1214
, all
@@ -231,6 +233,47 @@ intercalate sep xs = (foldl go { init: true, acc: mempty } xs).acc
231233
go { init: true } x = { init: false, acc: x }
232234
go { acc: acc } x = { init: false, acc: acc <> sep <> x }
233235

236+
-- | `foldMap` but with each element surrounded by some fixed value.
237+
-- |
238+
-- | For example:
239+
-- |
240+
-- | ```purescript
241+
-- | > surroundMap "*" show []
242+
-- | = "*"
243+
-- |
244+
-- | > surroundMap "*" show [1]
245+
-- | = "*1*"
246+
-- |
247+
-- | > surroundMap "*" show [1, 2]
248+
-- | = "*1*2*"
249+
-- |
250+
-- | > surroundMap "*" show [1, 2, 3]
251+
-- | = "*1*2*3*"
252+
-- | ```
253+
surroundMap :: forall f a m. Foldable f => Semigroup m => m -> (a -> m) -> f a -> m
254+
surroundMap d t f = unwrap (foldMap joined f) d
255+
where joined a = Endo \m -> d <> t a <> m
256+
257+
-- | `fold` but with each element surrounded by some fixed value.
258+
-- |
259+
-- | For example:
260+
-- |
261+
-- | ```purescript
262+
-- | > surround "*" []
263+
-- | = "*"
264+
-- |
265+
-- | > surround "*" ["1"]
266+
-- | = "*1*"
267+
-- |
268+
-- | > surround "*" ["1", "2"]
269+
-- | = "*1*2*"
270+
-- |
271+
-- | > surround "*" ["1", "2", "3"]
272+
-- | = "*1*2*3*"
273+
-- | ```
274+
surround :: forall f m. Foldable f => Semigroup m => m -> f m -> m
275+
surround d = surroundMap d id
276+
234277
-- | The conjunction of all the values in a data structure. When specialized
235278
-- | to `Boolean`, this function will test whether all of the values in a data
236279
-- | structure are `true`.

test/Main.purs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Control.Monad.Eff.Console (CONSOLE, log)
88
import Data.Bifoldable (class Bifoldable, bifoldl, bifoldr, bifoldMap, bifoldrDefault, bifoldlDefault, bifoldMapDefaultR, bifoldMapDefaultL)
99
import Data.Bifunctor (class Bifunctor, bimap)
1010
import Data.Bitraversable (class Bitraversable, bisequenceDefault, bitraverse, bisequence, bitraverseDefault)
11-
import Data.Foldable (class Foldable, foldl, foldr, foldMap, foldrDefault, foldlDefault, foldMapDefaultR, foldMapDefaultL, minimumBy, minimum, maximumBy, maximum, find, findMap, length, null)
11+
import Data.Foldable (class Foldable, foldl, foldr, foldMap, foldrDefault, foldlDefault, foldMapDefaultR, foldMapDefaultL, minimumBy, minimum, maximumBy, maximum, find, findMap, length, null, surroundMap)
1212
import Data.Function (on)
1313
import Data.Int (toNumber)
1414
import Data.Maybe (Maybe(..))
@@ -117,6 +117,12 @@ main = do
117117
assert $ length [1] == 1
118118
assert $ length [1, 2] == 2
119119

120+
log "Test surroundMap"
121+
assert $ "*" == surroundMap "*" show ([] :: Array Int)
122+
assert $ "*1*" == surroundMap "*" show [1]
123+
assert $ "*1*2*" == surroundMap "*" show [1, 2]
124+
assert $ "*1*2*3*" == surroundMap "*" show [1, 2, 3]
125+
120126
log "All done!"
121127

122128

0 commit comments

Comments
 (0)