-
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compose): add threadFirst/Last, rename compI => compL
- Loading branch information
1 parent
ec6084b
commit 0061b21
Showing
6 changed files
with
110 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from "./comp"; | ||
export * from "./juxt"; | ||
export * from "./partial"; | ||
export * from "./thread-first"; | ||
export * from "./thread-last"; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { FnAny } from "@thi.ng/api/api"; | ||
|
||
/** | ||
* Takes an `init` value and a number of functions and/or function | ||
* tuples, consisting of: `[fn, ...args]`. Executes each function | ||
* (or tuple) with the return value of the previous expression inserted | ||
* as first argument, using `init` for the first expression. | ||
* | ||
* ``` | ||
* const neg = (x) => -x; | ||
* const sub = (a, b) => a - b; | ||
* const div = (a, b) => a / b; | ||
* | ||
* threadFirst( | ||
* 5, | ||
* neg, // -5 | ||
* [sub, 20], // -5 - 20 = -25 | ||
* [div, 10] // -25 / 10 = -2.5 | ||
* ); | ||
* | ||
* // -2.5 | ||
* ``` | ||
* | ||
* @see threadLast | ||
* | ||
* @param init | ||
* @param fns | ||
*/ | ||
export function threadFirst(init: any, ...fns: (FnAny<any> | [FnAny<any>, ...any[]])[]) { | ||
return fns.reduce( | ||
(acc, expr) => | ||
typeof expr === "function" ? | ||
expr(acc) : | ||
expr[0](acc, ...expr.slice(1)) | ||
, | ||
init | ||
); | ||
} |
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,38 @@ | ||
import { FnAny } from "@thi.ng/api/api"; | ||
|
||
/** | ||
* Takes an `init` value and a number of functions and/or function | ||
* tuples, consisting of: `[fn, ...args]`. Executes each function | ||
* (or tuple) with the return value of the previous expression inserted | ||
* as last argument, using `init` for the first expression. | ||
* | ||
* ``` | ||
* const neg = (x) => -x; | ||
* const sub = (a, b) => a - b; | ||
* const div = (a, b) => a / b; | ||
* | ||
* threadLast( | ||
* 5, | ||
* neg, // -5 | ||
* [sub, 10], // 20 - (-5) = 15 | ||
* [div, 10] // 10 / 25 = 0.4 | ||
* ); | ||
* | ||
* // 0.4 | ||
* ``` | ||
* | ||
* @see threadFirst | ||
* | ||
* @param init | ||
* @param fns | ||
*/ | ||
export function threadLast(init: any, ...fns: (FnAny<any> | [FnAny<any>, ...any[]])[]) { | ||
return fns.reduce( | ||
(acc, expr) => | ||
typeof expr === "function" ? | ||
expr(acc) : | ||
expr[0](...expr.slice(1), acc) | ||
, | ||
init | ||
); | ||
} |