Skip to content

Instantly share code, notes, and snippets.

@coreyhaines coreyhaines/maybe.ts
Last active Jun 19, 2019

Embed
What would you like to do?
Simple Maybe<T> approximation in typescript
type Maybe<T> = T | undefined;
function maybeMap<T, U>(mv: Maybe<T>, f: (v:T) => U) : Maybe<U> {
if(mv !== undefined) {
return f(mv);
}else{
return undefined;
}
}
function maybeBind<T, U>(mv: Maybe<T>, f: (v:T) => Maybe<U>) : Maybe<U> {
if(mv !== undefined) {
return f(mv);
}else{
return undefined;
}
}
function withDefault<T>(mv : Maybe<T>, defaultValue : T) : T {
if(mv === undefined) {
return defaultValue;
}else{
return mv;
}
}
function maybeFromNullable<T>(v : T | null) : Maybe<T> {
if(v === null) {
return undefined;
}else{
return v;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.