-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sequence: add the terminal count$ operation
- Loading branch information
Dierk Koenig
committed
Dec 13, 2024
1 parent
2674d9b
commit fbbc91b
Showing
13 changed files
with
102 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
docs/src/kolibri/sequence/terminalOperations/count/count.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { reduce$ } from "../reduce/reduce.js"; | ||
|
||
export { count$ } | ||
|
||
/** | ||
* @typedef CountSequenceOperationType | ||
* @type { () => CountOperationType } | ||
*/ | ||
/** | ||
* Count the number of elements in a **finite** {@link Iterable}. | ||
* The **finite** constraint is indicated by the trailing **$** in the function name. | ||
* | ||
* _Note_: | ||
* When erroneously called on an **infinite** iterable, the function **does not return**. | ||
* So better be safe and first {@link TakeOperationType take} as many elements as you consider the allowed maximum count. | ||
* | ||
* | ||
* @typedef CountOperationType | ||
* @function | ||
* @pure | ||
* @haskell [a] -> Int | ||
* @param { Iterable } iterable - a finite iterable | ||
* @returns { Number } | ||
* | ||
* @example | ||
* const numbers = [1, 3, 0, 5]; | ||
* const count = count$(numbers); | ||
* | ||
* console.log(count); | ||
* // => Logs '4' | ||
* | ||
* const infinite = Sequence(0, forever, id); // this cannot be counted | ||
* assert.is(count$( take(10)(infinite) ), 10); // take with upper limit | ||
*/ | ||
|
||
/** | ||
* see {@link CountOperationType} | ||
* @type { CountOperationType } | ||
*/ | ||
const count$ = iterable => /** @type { Number } */ reduce$( (acc, _cur) => acc + 1, 0)(iterable); |
36 changes: 36 additions & 0 deletions
36
docs/src/kolibri/sequence/terminalOperations/count/countTest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { addToTestingTable, TESTS } from "../../util/testingTable.js"; | ||
import { TestSuite } from "../../../util/test.js"; | ||
import {nil, append, forever, Sequence, take} from "../../sequence.js"; | ||
import { createTestConfig } from "../../util/testUtil.js"; | ||
import { count$ } from "./count.js"; | ||
import {id} from "../../../stdlib.js"; | ||
|
||
const testSuite = TestSuite("Sequence: terminal operation count$"); | ||
|
||
addToTestingTable(testSuite)( | ||
createTestConfig({ | ||
name: "count$", | ||
iterable: () => nil, | ||
operation: () => count$, | ||
evalFn: expected => actual => expected === actual, | ||
expected: 0, | ||
excludedTests: [TESTS.TEST_CB_NOT_CALLED_AFTER_DONE], | ||
invariants: [ | ||
it => count$(it) * 2 === count$(append(it)(it)), | ||
] | ||
}) | ||
); | ||
|
||
testSuite.add("zero, one, many", assert => { | ||
assert.is(count$(nil), 0); | ||
assert.is(count$([42]), 1); | ||
assert.is(count$(Array.from({length: 1000})), 1000); | ||
}); | ||
|
||
testSuite.add("take and then count", assert => { | ||
const infinite = Sequence(0, forever, id); // this cannot be counted | ||
assert.is(count$( take(10)(infinite) ), 10); // take with upper limit | ||
assert.is(count$( take(10)( nil) ), 0); // upper limit does not get in the way if iterable is shorter | ||
}); | ||
|
||
testSuite.run(); |
1 change: 1 addition & 0 deletions
1
docs/src/kolibri/sequence/terminalOperations/terminalOperations.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
docs/src/kolibri/sequence/terminalOperations/terminalOperationsTestSuite.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters