2025-04-13 16:29:51 +00:00
|
|
|
import {NumberT} from './NumberT.js'
|
2025-04-02 11:22:53 -07:00
|
|
|
|
refactor: change onType to match and take only one pattern and result (#22)
Pursuant to #12. Besides changing the name of onType to match, and only allowing one pattern and result in `match()`,
this PR also arranges that in place of an onType with lots of alternating PATTERN, VALUE, PATTERN, VALUE arguments, one now exports an _array_ of `match(PATTERN, VALUE)` items.
Doesn't quite fully resolve #12, because there is still the question of whether `match(...)` can be left out for a behavior that literally matches anything (current behavior), or whether `match(Passthru, behavior)` should be required for such cases.
Reviewed-on: https://code.studioinfinity.org/StudioInfinity/nanomath/pulls/22
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Co-committed-by: Glen Whitney <glen@studioinfinity.org>
2025-04-22 05:01:21 +00:00
|
|
|
import {match} from '#core/helpers.js'
|
2025-04-08 22:42:53 +00:00
|
|
|
import {Returns} from '#core/Type.js'
|
|
|
|
|
refactor: change onType to match and take only one pattern and result (#22)
Pursuant to #12. Besides changing the name of onType to match, and only allowing one pattern and result in `match()`,
this PR also arranges that in place of an onType with lots of alternating PATTERN, VALUE, PATTERN, VALUE arguments, one now exports an _array_ of `match(PATTERN, VALUE)` items.
Doesn't quite fully resolve #12, because there is still the question of whether `match(...)` can be left out for a behavior that literally matches anything (current behavior), or whether `match(Passthru, behavior)` should be required for such cases.
Reviewed-on: https://code.studioinfinity.org/StudioInfinity/nanomath/pulls/22
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Co-committed-by: Glen Whitney <glen@studioinfinity.org>
2025-04-22 05:01:21 +00:00
|
|
|
export const plain = f => match(
|
2025-04-13 16:29:51 +00:00
|
|
|
Array(f.length).fill(NumberT), Returns(NumberT, f))
|
|
|
|
|
2025-04-16 04:23:48 +00:00
|
|
|
// Takes a behavior returning boolean, and returns a factory
|
|
|
|
// that returns that behavior if the boolean type is present,
|
|
|
|
// and otherwise wraps the behavior to return 1 or 0.
|
|
|
|
export const boolnum = behavior => math => {
|
|
|
|
const {BooleanT} = math.types
|
|
|
|
if (BooleanT) return Returns(BooleanT, behavior)
|
|
|
|
return Returns(NumberT, (...args) => behavior(...args) ? 1 : 0)
|
|
|
|
}
|