-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add constantly() - add delay() - add delayed() - add identity()
- Loading branch information
1 parent
4b0eec6
commit dd13fa9
Showing
5 changed files
with
41 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { FnAny } from "@thi.ng/api"; | ||
|
||
export const constantly = | ||
<T>(x: T): FnAny<T> => () => 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { IDeref } from "@thi.ng/api"; | ||
|
||
export const delay = | ||
<T>(body: () => T) => new Delay<T>(body); | ||
|
||
export class Delay<T> implements | ||
IDeref<T> { | ||
|
||
value: T; | ||
protected body: () => T; | ||
protected realized: boolean; | ||
|
||
constructor(body: () => T) { | ||
this.body = body; | ||
this.realized = false; | ||
} | ||
|
||
deref() { | ||
if (!this.realized) { | ||
this.value = this.body(); | ||
this.realized = true; | ||
} | ||
return this.value; | ||
} | ||
|
||
isRealized() { | ||
return this.realized; | ||
} | ||
} |
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,3 @@ | ||
export const delayed = | ||
<T>(x: T, t: number) => | ||
new Promise((resolve) => setTimeout(() => resolve(x), t)); |
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 @@ | ||
export const identity = <T>(x: T) => 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