Skip to content

Array.fromFormula with docs and tests #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Core__Array.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ function findMap(arr, f) {
};
}

function fromFormula(state, generator) {
var result = [];
var state$1 = state;
var $$break = false;
while(!$$break) {
var match = Curry._1(generator, state$1);
if (match !== undefined) {
result.push(match[0]);
state$1 = match[1];
} else {
$$break = true;
}
};
return result;
}

export {
make ,
fromInitializer ,
Expand All @@ -155,5 +171,6 @@ export {
shuffle ,
shuffleInPlace ,
findMap ,
fromFormula ,
}
/* No side effect */
16 changes: 16 additions & 0 deletions src/Core__Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,19 @@ let findMap = (arr, f) => {
}

@send external at: (array<'a>, int) => option<'a> = "at"

let fromFormula = (state, generator) => {
let result = []
let state = ref(state)
let break = ref(false)
while !break.contents {
switch generator(state.contents) {
| None => break := true
| Some((item, nextState)) => {
result->push(item)
state := nextState
}
}
}
result
}
16 changes: 16 additions & 0 deletions src/Core__Array.resi
Original file line number Diff line number Diff line change
Expand Up @@ -955,3 +955,19 @@ let findMap: (array<'a>, 'a => option<'b>) => option<'b>
*/
@send
external at: (array<'a>, int) => option<'a> = "at"

/**
`fromFormula(state, f)` creates an array that contains the elements generated by a computation. The function `f` is first passed `state` and then is repeatedly called to build the array. When `f` returns a tuple of `Some(item, state)`, `item` is pushed onto the result array, and `state` is fed into the next computation. When `f` returns `None`, the computation is complete and the new array is returned.

## Examples

```rescript
let f = i => i<100 ? Some(i, i*2) : None
Array.fromFormula(1, f) // [1, 2, 4, 8, 16, 32, 64]
Array.fromFormula(100, f) // []

let g = i => i >= 0.0 ? Some(i, i -. 0.5) : None
Array.fromFormula(3.0, g) // [3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0]
```
*/
let fromFormula: ('state, 'state => option<('a, 'state)>) => array<'a>
64 changes: 64 additions & 0 deletions test/ArrayTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,71 @@ Test.run([

})), eq, undefined);

function generator(i) {
if (i < 100) {
return [
i,
(i << 1)
];
}

}

Test.run([
[
"ArrayTests.res",
104,
20,
33
],
"fromFormula"
], Core__Array.fromFormula(1, generator), eq, [
1,
2,
4,
8,
16,
32,
64
]);

Test.run([
[
"ArrayTests.res",
105,
20,
33
],
"fromFormula"
], Core__Array.fromFormula(12, generator), eq, [
12,
24,
48,
96
]);

Test.run([
[
"ArrayTests.res",
106,
20,
33
],
"fromFormula"
], Core__Array.fromFormula(99, generator), eq, [99]);

Test.run([
[
"ArrayTests.res",
107,
20,
33
],
"fromFormula"
], Core__Array.fromFormula(100, generator), eq, []);

export {
eq ,
generator ,
}
/* Not a pure module */
7 changes: 7 additions & 0 deletions test/ArrayTests.res
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,10 @@ Test.run(
eq,
None,
)

let generator = i => i < 100 ? Some(i, i * 2) : None

Test.run(__POS_OF__("fromFormula"), Array.fromFormula(1, generator), eq, [1, 2, 4, 8, 16, 32, 64])
Test.run(__POS_OF__("fromFormula"), Array.fromFormula(12, generator), eq, [12, 24, 48, 96])
Test.run(__POS_OF__("fromFormula"), Array.fromFormula(99, generator), eq, [99])
Test.run(__POS_OF__("fromFormula"), Array.fromFormula(100, generator), eq, [])
3 changes: 3 additions & 0 deletions test/TestSuite.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ var Concurrently = PromiseTest.Concurrently;

var panicTest = ErrorTests.panicTest;

var generator = ArrayTests.generator;

var eq = IntTests.eq;

var $$catch = IntTests.$$catch;
Expand All @@ -41,6 +43,7 @@ export {
Catching ,
Concurrently ,
panicTest ,
generator ,
eq ,
$$catch ,
}
Expand Down