Skip to content

Commit f206747

Browse files
committed
feat: 🎸 scan and scanN now work like map
scap applies jth init value and jth callback to jth output BREAKING CHANGE: 🧨 scan and scanN changed to behavior consistent with map
1 parent ef834cb commit f206747

2 files changed

Lines changed: 55 additions & 58 deletions

File tree

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ exports.scan = (...initStates) => (...reducers) => exports.map(...reducers.map(
284284
))
285285

286286
// simplified scan dropping the seed
287-
const scanS = (...args) => exports.scan(...args, undefined)
287+
const scanS = (...args) => exports.scan()(...args)
288288

289289

290290
/**

test/scan.test.js

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const test = require('./config')
2-
const { map, scan, scanS } = require('..')
2+
const { update, map, scan, scanS } = require('..')
33

44
test('scan over single callback output', t => {
55
const reducer = (acc, x) => acc + x
@@ -13,65 +13,62 @@ test('scan over single callback preserves outputs from other callbacks', t => {
1313
t.plan(2)
1414
scan(10)(reducer)(cpsFun)(t.cis(10+42),t.cis(5))
1515
})
16-
// test('scan ignores undefined/null args', t => {
17-
// const r = (acc, x) => acc + x
18-
// const cpsFun = (c1,c2) => {c1(42)}
19-
// const cpsFun1 = (c1,c2) => {c2(42)}
20-
// const cpsFun2 = (c1,c2) => {c1(10+11);c2(11)}
21-
// t.plan(5)
22-
// map(null, notCalled)(cpsFun)(t.cis(42))
23-
// scan(10)(null, r)(cpsFun)(t.cis(42))
24-
// scan(undefined, r, 10)(cpsFun)(t.cis(42))
25-
// scan(undefined, r, 10)(cpsFun1)(t.cis(10+42))
26-
// scan(undefined, r, 10)(cpsFun2)(t.cis(10+11))
27-
// })
28-
// test('scan over single repeated callback output', t => {
29-
// let called = false
30-
// const reducer = (acc, x) => acc + x
31-
// const cpsFun = cb => { cb(2); cb(8) }
32-
// const newCps = scan(reducer, 10)(cpsFun)
33-
// t.plan(2)
34-
// newCps(res => {
35-
// t.cis(called ? 10+2+8 : 10+2)(res)
36-
// called = true
37-
// })
38-
// })
39-
// test('scan over outputs from 2 callbacks', t => {
40-
// const r = (acc, x) => acc + x
41-
// const cpsFun = (cb1, cb2) => {cb1(2); cb2(3)}
42-
// const newCps = scan(r, r, 10)(cpsFun)
43-
// t.plan(2)
44-
// let count = 0
45-
// newCps(res => t.cis(count++ === 0 ? 10+2 : 10+2+3)(res))
46-
// })
47-
// test('scan with multiple functions applies each to the same seed', t=>{
48-
// const r1=(acc, x) => acc + x, r2=(acc, x) => acc * x
49-
// const cpsFun = (cb1, cb2) => {cb1(2); cb2(3)}
50-
// const cpsFun1 = (cb1, cb2) => {cb2(2); cb1(3)}
51-
// const newCps = scan(r1, r2, 10)(cpsFun)
52-
// const newCps1 = scan(r1, r2, 10)(cpsFun1)
53-
// t.plan(4)
54-
// let count = 0
55-
// newCps(res => t.cis(count++ === 0 ? 10+2 : (10+2)*3)(res))
56-
// count = 0
57-
// newCps1(res => t.cis(count++ === 0 ? 10*2 : (10*2)+3)(res))
58-
// })
16+
test('scan ignores undefined/null args', t => {
17+
const r = (acc, x) => acc + x
18+
const cpsFun = (c1,c2) => {c1(42)}
19+
const cpsFun1 = (c1,c2) => {c2(42)}
20+
const cpsFun2 = (c1,c2) => {c1(2);c2(11)}
21+
t.plan(5)
22+
scan(10)(null, r)(cpsFun)(t.cis(42))
23+
scan(10)(undefined, r)(cpsFun)(t.cis(42))
24+
scan(null,10)(undefined, r)(cpsFun1)(null,t.cis(10+42))
25+
scan(null,10)(undefined, r)(cpsFun2)(t.cis(2),t.cis(10+11))
26+
})
27+
test('scan over single repeated callback output', t => {
28+
let called = false
29+
const reducer = (acc, x) => acc + x
30+
const cpsFun = cb => { cb(2); cb(8) }
31+
const newCps = scan(10)(reducer)(cpsFun)
32+
t.plan(2)
33+
newCps(res => {
34+
t.cis(called ? 10+2+8 : 10+2)(res)
35+
called = true
36+
})
37+
})
38+
test('scan over outputs from 2 callbacks', t => {
39+
const r = (acc, x) => acc + x
40+
const cpsFun = (cb1, cb2) => {cb1(2); cb2(3)}
41+
const newCps = scan(10,11)(r, r)(cpsFun)
42+
t.plan(2)
43+
newCps(t.cis(10+2), t.cis(11+3))
44+
})
45+
test('scan with multiple functions applies each to the same seed', t=>{
46+
const r1=(acc, x) => acc + x, r2=(acc, x) => acc * x
47+
const cpsFun = (cb1, cb2) => {cb1(2); cb2(3)}
48+
const cpsFun1 = (cb1, cb2) => {cb2(2); cb1(3)}
49+
const newCps = scan(10,10)(r1, r2)(cpsFun)
50+
const newCps1 = scan(10,10)(r1, r2)(cpsFun1)
51+
t.plan(4)
52+
let count = 0
53+
newCps(t.cis(10+2),t.cis(10*3))
54+
count = 0
55+
newCps1(t.cis(10+3),t.cis(10*2))
56+
})
5957
// test('scan throws with fewer than 2 args', t=>{
6058
// t.throws(_=>scan())
6159
// t.throws(_=>scan(1))
6260
// })
6361

64-
// test('scanN implies seed=undefined', t => {
65-
// const reducer = (acc=10, x) => acc + x
66-
// const cpsFun = cb => cb(42)
67-
// t.plan(1)
68-
// scanS(reducer)(cpsFun)(t.cis(52))
69-
// })
70-
// test('scanN works with multiple args with seed=undefined implied', t => {
71-
// const r1=(acc=0, x) => acc + x, r2=(acc=1, x) => acc * x
72-
// const cpsFun = (c1,c2) => {c1(10);c2(20)}
73-
// t.plan(2)
74-
// let count = 0
75-
// scanS(r1,r2)(cpsFun)(res => t.cis(count++ === 0 ? 0+10 : (0+10)*20)(res))
76-
// })
62+
test('scanS implies seed=undefined', t => {
63+
const reducer = (acc=10, x) => acc + x
64+
const cpsFun = cb => cb(42)
65+
t.plan(1)
66+
scanS(reducer)(cpsFun)(t.cis(10+42))
67+
})
68+
test('scanS works with multiple args with seed=undefined implied', t => {
69+
const r1=(acc=0, x) => acc + x, r2=(acc=1, x) => acc * x
70+
const cpsFun = (c1,c2) => {c1(10);c2(20)}
71+
t.plan(2)
72+
scanS(r1,r2)(cpsFun)(t.cis(0+10), t.cis(1*20))
73+
})
7774

0 commit comments

Comments
 (0)