Skip to content

Instantly share code, notes, and snippets.

View kylecorbelli's full-sized avatar

Kyle Corbelli kylecorbelli

  • Uber
  • SF Bay Area, CA
View GitHub Profile
import { curry } from 'ramda'
function maybeMap<A, B> (f: (val: A) => B, m: Maybe<A>): Maybe<B> {
switch (m.type) {
case MaybeType.Nothing:
return Nothing()
case MaybeType.Just:
return Just(f(m.value))
}
}
const safeHead = <T> (list: ReadonlyArray<T>): Maybe<T> =>
list.length === 0
? Nothing()
: Just(list[0])
import { compose, toUpper } from 'ramda'
function unsafeHead<T> (list: ReadonlyArray<T>): T {
return list[0]
}
type UpperCaseHead = (list: ReadonlyArray<string>) => string
const upperCaseHead: UpperCaseHead = compose(
toUpper,
unsafeHead,
const greet = (maybeName: Maybe<string>): string => {
switch (maybeName.type) {
case MaybeType.Nothing:
return 'Pleased to meet you!'
case MaybeType.Just:
return `Good to see you again ${maybeName.value}`
}
}
enum MaybeType {
Just = 'maybe-type__just',
Nothing = 'maybe-type__nothing',
}
interface Just<T> {
type: typeof MaybeType.Just
value: T
}
import { curry } from 'ramda'
enum MaybeType {
Just = 'maybe-type__just',
Nothing = 'maybe-type__nothing',
}
interface Just<T> extends Readonly<{
type: typeof MaybeType.Just
value: T