diff --git a/typescript/Maybe.ts b/typescript/Maybe.ts index 619c2ad..e48a3d8 100644 --- a/typescript/Maybe.ts +++ b/typescript/Maybe.ts @@ -40,3 +40,11 @@ export function mustExist(m: T | undefined): T { return m; } } + +export function defaultTo(d: T, m: Maybe): T { + if (isJust(m)) { + return m[SymbolJust]; + } else { + return d; + } +} diff --git a/typescript/Parse.ts b/typescript/Parse.ts index 551e2a2..15d80f5 100644 --- a/typescript/Parse.ts +++ b/typescript/Parse.ts @@ -1,4 +1,4 @@ -import { isJust, isNothing, Just, just, Maybe, mustExist, Nothing, nothing, SymbolJust } from './Maybe.js'; +import { defaultTo, isJust, isNothing, Just, just, Maybe, mustExist, Nothing, nothing, SymbolJust } from './Maybe.js'; import { map, primStringToList, split } from './Util.js'; export type Token = { @@ -155,10 +155,11 @@ export function parseNat(a: Maybe, cs: ReadonlyArray): Result