From 0458b226c7f6f9e5ae219f0364b8142e2a2ab2b5 Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Thu, 19 Dec 2019 14:17:55 +0200 Subject: [PATCH] typescript(option): refactor: created some internal helpers to clean some of the code Signed-off-by: prescientmoon --- typescript/option/src/helpers.ts | 45 ++++++----------------- typescript/option/src/internalHelperts.ts | 2 + 2 files changed, 13 insertions(+), 34 deletions(-) create mode 100644 typescript/option/src/internalHelperts.ts diff --git a/typescript/option/src/helpers.ts b/typescript/option/src/helpers.ts index 9967f8a..3d68a9c 100644 --- a/typescript/option/src/helpers.ts +++ b/typescript/option/src/helpers.ts @@ -1,5 +1,6 @@ import { Option, Some, None } from './types' import { Binder, Folder, Mapper, Predicate, BackFolder } from './internalTypes' +import { always, identity } from './internalHelperts' import Internals, { SomeClass } from './internals' export const isSome = (option: Option) => @@ -9,8 +10,8 @@ export const isNothing = (option: Option) => export const match = ( option: Option, - caseSome: (v: T) => U, - caseNone: () => U + caseSome: Mapper, + caseNone: Mapper ) => { if (isSome(option)) { return caseSome((option as SomeClass)[Internals.someValue]) @@ -23,32 +24,24 @@ export const bind = ( binder: Binder, option: Option ): Option => { - return match(option, binder, () => None) + return match(option, binder, always(None)) } export const map = ( mapper: Mapper, option: Option ): Option => { - return match( - option, - v => Some(mapper(v)), - () => None - ) + return match(option, v => Some(mapper(v)), always(None)) } export const count = (option: Option) => Number(isSome(option)) export const exists = (predicate: Predicate, option: Option) => { - return match(option, predicate, () => false) + return match(option, predicate, always(false)) } export const filter = (predicate: Predicate, option: Option) => { - return match( - option, - v => (predicate(v) ? Some(v) : None), - () => None - ) + return match(option, v => (predicate(v) ? Some(v) : None), always(None)) } export const fold = ( @@ -56,11 +49,7 @@ export const fold = ( initial: U, option: Option ) => { - match( - option, - v => folder(initial, v), - () => initial - ) + match(option, v => folder(initial, v), always(initial)) } export const foldback = ( @@ -68,11 +57,7 @@ export const foldback = ( option: Option, initial: U ) => { - return match( - option, - v => folder(v, initial), - () => initial - ) + return match(option, v => folder(v, initial), always(initial)) } export const forall = (predicate: Predicate, option: Option) => { @@ -94,17 +79,9 @@ export const iter = (mapper: Mapper, option: Option) => { } export const toArray = (option: Option) => { - return match( - option, - v => [v], - () => [] - ) + return match(option, v => [v], always([])) } export const toNullable = (option: Option) => { - return match( - option, - v => v, - () => null - ) + return match(option, identity, always(null)) } diff --git a/typescript/option/src/internalHelperts.ts b/typescript/option/src/internalHelperts.ts new file mode 100644 index 0000000..693472b --- /dev/null +++ b/typescript/option/src/internalHelperts.ts @@ -0,0 +1,2 @@ +export const always = (v: T) => () => v +export const identity = (v: T) => v