Skip to content

Commit

Permalink
feat(compose): add new functions
Browse files Browse the repository at this point in the history
- add constantly()
- add delay()
- add delayed()
- add identity()
  • Loading branch information
postspectacular committed Feb 12, 2019
1 parent 4b0eec6 commit dd13fa9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/compose/src/constantly.ts
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;
29 changes: 29 additions & 0 deletions packages/compose/src/delay.ts
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;
}
}
3 changes: 3 additions & 0 deletions packages/compose/src/delayed.ts
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));
1 change: 1 addition & 0 deletions packages/compose/src/identity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const identity = <T>(x: T) => x;
4 changes: 4 additions & 0 deletions packages/compose/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export * from "./comp";
export * from "./constantly";
export * from "./delay";
export * from "./delayed";
export * from "./identity";
export * from "./juxt";
export * from "./partial";
export * from "./thread-first";
Expand Down

0 comments on commit dd13fa9

Please sign in to comment.