Skip to content

Commit 843ffe9

Browse files
Merge pull request #133 from sanctuary-js/davidchambers/intercalate
derive ‘intercalate’ from ‘concat’, ‘empty’, and ‘reduce’
2 parents f5896b4 + a267b2f commit 843ffe9

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

Diff for: index.js

+42
Original file line numberDiff line numberDiff line change
@@ -2036,6 +2036,47 @@
20362036
return any (function(y) { return equals (x, y); }, foldable);
20372037
}
20382038

2039+
//# intercalate :: (Monoid m, Foldable f) => (m, f m) -> m
2040+
//.
2041+
//. Concatenates the elements of the given structure, separating each pair
2042+
//. of adjacent elements with the given separator.
2043+
//.
2044+
//. This function is derived from [`concat`](#concat), [`empty`](#empty),
2045+
//. and [`reduce`](#reduce).
2046+
//.
2047+
//. ```javascript
2048+
//. > intercalate (', ', [])
2049+
//. ''
2050+
//.
2051+
//. > intercalate (', ', ['foo', 'bar', 'baz'])
2052+
//. 'foo, bar, baz'
2053+
//.
2054+
//. > intercalate (', ', Nil)
2055+
//. ''
2056+
//.
2057+
//. > intercalate (', ', Cons ('foo', Cons ('bar', Cons ('baz', Nil))))
2058+
//. 'foo, bar, baz'
2059+
//.
2060+
//. > intercalate ([0, 0, 0], [])
2061+
//. []
2062+
//.
2063+
//. > intercalate ([0, 0, 0], [[1], [2, 3], [4, 5, 6], [7, 8], [9]])
2064+
//. [1, 0, 0, 0, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0, 7, 8, 0, 0, 0, 9]
2065+
//. ```
2066+
function intercalate(separator, foldable) {
2067+
var result = reduce (
2068+
function(acc, x) {
2069+
return {
2070+
empty: false,
2071+
value: concat (acc.value, acc.empty ? x : concat (separator, x))
2072+
};
2073+
},
2074+
{empty: true, value: empty (separator.constructor)},
2075+
foldable
2076+
);
2077+
return result.value;
2078+
}
2079+
20392080
//# foldMap :: (Monoid m, Foldable f) => (TypeRep m, a -> m, f a) -> m
20402081
//.
20412082
//. Deconstructs a foldable by mapping every element to a monoid and
@@ -2326,6 +2367,7 @@
23262367
any: any,
23272368
none: none,
23282369
elem: elem,
2370+
intercalate: intercalate,
23292371
foldMap: foldMap,
23302372
reverse: reverse,
23312373
sort: sort,

Diff for: test/index.js

+18
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,24 @@ test ('elem', function() {
12961296
eq (Z.elem (0, Nothing), false);
12971297
});
12981298

1299+
test ('intercalate', function() {
1300+
eq (Z.intercalate.length, 2);
1301+
eq (Z.intercalate.name, 'intercalate');
1302+
1303+
eq (Z.intercalate (', ', []), '');
1304+
eq (Z.intercalate (', ', ['foo']), 'foo');
1305+
eq (Z.intercalate (', ', ['foo', 'bar']), 'foo, bar');
1306+
eq (Z.intercalate (', ', ['foo', 'bar', 'baz']), 'foo, bar, baz');
1307+
eq (Z.intercalate ([0, 0, 0], []), []);
1308+
eq (Z.intercalate ([0, 0, 0], [[1]]), [1]);
1309+
eq (Z.intercalate ([0, 0, 0], [[1], [2]]), [1, 0, 0, 0, 2]);
1310+
eq (Z.intercalate ([0, 0, 0], [[1], [2], [3]]), [1, 0, 0, 0, 2, 0, 0, 0, 3]);
1311+
eq (Z.intercalate ('.', Nil), '');
1312+
eq (Z.intercalate ('.', Cons ('x', Nil)), 'x');
1313+
eq (Z.intercalate ('.', Cons ('x', Cons ('y', Nil))), 'x.y');
1314+
eq (Z.intercalate ('.', Cons ('x', Cons ('y', Cons ('z', Nil)))), 'x.y.z');
1315+
});
1316+
12991317
test ('foldMap', function() {
13001318
eq (Z.foldMap.length, 3);
13011319
eq (Z.foldMap.name, 'foldMap');

0 commit comments

Comments
 (0)