-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(memoize): add doOnce(), update readme
- Loading branch information
1 parent
66dd3b1
commit 889e00d
Showing
4 changed files
with
23 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { Fn } from "@thi.ng/api"; | ||
import { MapLike } from "./api"; | ||
|
||
/** | ||
* Similar to {@link memoize1}, however optimized for side effects only, i.e. | ||
* functions which DO NOT return any result. | ||
* | ||
* @param fn | ||
* @param cache | ||
*/ | ||
export const doOnce = <T>(fn: Fn<T, void>, cache?: MapLike<T, boolean>) => { | ||
!cache && (cache = new Map()); | ||
return (x: T) => { | ||
if (!cache!.has(x)) { | ||
cache!.set(x, true); | ||
fn(x); | ||
} | ||
}; | ||
}; |
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,5 +1,6 @@ | ||
export * from "./api"; | ||
export * from "./defonce"; | ||
export * from "./do-once"; | ||
export * from "./memoize"; | ||
export * from "./memoize1"; | ||
export * from "./memoizej"; |
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