From dd13fa9da4841d77863446746e2a6d0ce1fc0edd Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Tue, 12 Feb 2019 15:49:31 +0000 Subject: [PATCH] feat(compose): add new functions - add constantly() - add delay() - add delayed() - add identity() --- packages/compose/src/constantly.ts | 4 ++++ packages/compose/src/delay.ts | 29 +++++++++++++++++++++++++++++ packages/compose/src/delayed.ts | 3 +++ packages/compose/src/identity.ts | 1 + packages/compose/src/index.ts | 4 ++++ 5 files changed, 41 insertions(+) create mode 100644 packages/compose/src/constantly.ts create mode 100644 packages/compose/src/delay.ts create mode 100644 packages/compose/src/delayed.ts create mode 100644 packages/compose/src/identity.ts diff --git a/packages/compose/src/constantly.ts b/packages/compose/src/constantly.ts new file mode 100644 index 0000000000..2f758a5fc7 --- /dev/null +++ b/packages/compose/src/constantly.ts @@ -0,0 +1,4 @@ +import { FnAny } from "@thi.ng/api"; + +export const constantly = + (x: T): FnAny => () => x; diff --git a/packages/compose/src/delay.ts b/packages/compose/src/delay.ts new file mode 100644 index 0000000000..7b7efc4064 --- /dev/null +++ b/packages/compose/src/delay.ts @@ -0,0 +1,29 @@ +import { IDeref } from "@thi.ng/api"; + +export const delay = + (body: () => T) => new Delay(body); + +export class Delay implements + IDeref { + + 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; + } +} diff --git a/packages/compose/src/delayed.ts b/packages/compose/src/delayed.ts new file mode 100644 index 0000000000..d250ef0f57 --- /dev/null +++ b/packages/compose/src/delayed.ts @@ -0,0 +1,3 @@ +export const delayed = + (x: T, t: number) => + new Promise((resolve) => setTimeout(() => resolve(x), t)); diff --git a/packages/compose/src/identity.ts b/packages/compose/src/identity.ts new file mode 100644 index 0000000000..74efbc8cf1 --- /dev/null +++ b/packages/compose/src/identity.ts @@ -0,0 +1 @@ +export const identity = (x: T) => x; diff --git a/packages/compose/src/index.ts b/packages/compose/src/index.ts index dfddd39435..f9c76d4c74 100644 --- a/packages/compose/src/index.ts +++ b/packages/compose/src/index.ts @@ -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";