Comparing version 4.0.0 to 5.0.0
{ | ||
"name": "gamla", | ||
"version": "4.0.0", | ||
"version": "5.0.0", | ||
"description": "functional programing for javascript", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -0,2 +1,6 @@ | ||
import { head, second } from "./array.js"; | ||
import { filter } from "./filter.js"; | ||
import { isPromise } from "./promise.js"; | ||
import { pipe } from "./composition.js"; | ||
@@ -20,1 +24,11 @@ export const ifElse = | ||
export const when = (predicate, fTrue) => ifElse(predicate, fTrue, (x) => x); | ||
export const cond = | ||
(predicatesAndResolvers) => | ||
(...x) => | ||
pipe( | ||
filter(pipe(head, (predicate) => predicate(...x))), | ||
head, | ||
second, | ||
(f) => f(...x), | ||
)(predicatesAndResolvers); |
@@ -1,2 +0,2 @@ | ||
import { ifElse, unless, when } from "./conditional.js"; | ||
import { cond, ifElse, unless, when } from "./conditional.js"; | ||
@@ -32,1 +32,10 @@ import { wrapPromise } from "./promise.js"; | ||
}); | ||
test("cond", () => { | ||
const testFunction = cond([ | ||
[(x) => x > 3, (x) => x + 1], | ||
[(x) => x < 3, (x) => x - 1], | ||
]); | ||
expect(testFunction(2)).toStrictEqual(1); | ||
expect(testFunction(4)).toStrictEqual(5); | ||
}); |
import { isPromise } from "./promise.js"; | ||
const reduceHelper = (reducer) => (s, xs, firstIndex) => | ||
firstIndex === xs.length | ||
? s | ||
: isPromise(s) | ||
? s.then((s) => | ||
reduceHelper(reducer)(reducer(s, xs[firstIndex]), xs, firstIndex + 1), | ||
) | ||
: reduceHelper(reducer)(reducer(s, xs[firstIndex]), xs, firstIndex + 1); | ||
const reduceHelper = (reducer) => (s, xs, firstIndex) => { | ||
for (let i = firstIndex; i < xs.length; i++) { | ||
if (isPromise(s)) return s.then((s) => reduceHelper(reducer)(s, xs, i)); | ||
s = reducer(s, xs[i]); | ||
} | ||
return s; | ||
}; | ||
@@ -12,0 +11,0 @@ export const reduce = (reducer, initial) => (xs) => |
@@ -37,1 +37,10 @@ import { max, min, reduce } from "./reduce.js"; | ||
}); | ||
test("max call stack is not a limit on array size", () => { | ||
const bigArray = []; | ||
const size = 1000000; | ||
for (let i = 0; i < size; i++) { | ||
bigArray.push(i); | ||
} | ||
expect(max((x) => x)(bigArray)).toBe(size - 1); | ||
}); |
40303
1411