Skip to content

Commit 1deede0

Browse files
committed
Add intercalate
1 parent 65b7708 commit 1deede0

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343

4444
for_ :: forall a b f m. (Applicative m, Foldable f) => f a -> (a -> m b) -> m Unit
4545

46+
intercalate :: forall f m. (Foldable f, Monoid m) => m -> f m -> m
47+
4648
lookup :: forall a b f. (Eq a, Foldable f) => a -> f (Tuple a b) -> Maybe b
4749

4850
mconcat :: forall f m. (Foldable f, Monoid m) => f m -> m

src/Data/Foldable.purs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ sequence_ = traverse_ id
7070
mconcat :: forall f m. (Foldable f, Monoid m) => f m -> m
7171
mconcat = foldl (<>) mempty
7272

73+
intercalate :: forall f m. (Foldable f, Monoid m) => m -> f m -> m
74+
intercalate sep xs = (foldr go { init: true, acc: mempty } xs).acc
75+
where
76+
go x { init = init } | init = { init: false, acc: x }
77+
go x { acc = acc } = { init: false, acc: x <> sep <> acc }
78+
7379
and :: forall f. (Foldable f) => f Boolean -> Boolean
7480
and = foldl (&&) true
7581

@@ -98,7 +104,7 @@ find :: forall a f. (Foldable f) => (a -> Boolean) -> f a -> Maybe a
98104
find p f = case foldMap (\x -> if p x then [x] else []) f of
99105
(x:_) -> Just x
100106
[] -> Nothing
101-
107+
102108
lookup :: forall a b f. (Eq a, Foldable f) => a -> f (Tuple a b) -> Maybe b
103109
lookup a f = runFirst $ foldMap (\(Tuple a' b) -> First (if a == a' then Just b else Nothing)) f
104110

0 commit comments

Comments
 (0)