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'
enum MaybeType {
Just = 'maybe-type__just',
Nothing = 'maybe-type__nothing',
}
interface Just<T> extends Readonly<{
type: typeof MaybeType.Just
value: T
enum MaybeType {
Just = 'maybe-type__just',
Nothing = 'maybe-type__nothing',
}
interface Just<T> {
type: typeof MaybeType.Just
value: T
}
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}`
}
}
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 safeHead = <T> (list: ReadonlyArray<T>): Maybe<T> =>
list.length === 0
? Nothing()
: Just(list[0])
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))
}
}
type UpperCaseHead = (list: ReadonlyArray<string>) => Maybe<string>
const upperCaseHead: UpperCaseHead = compose(
Maybe.map(toUpper),
safeHead,
) as UpperCaseHead
console.log(upperCaseHead([])) // { type: Maybe.Nothing } ie. Nothing
console.log(upperCaseHead([ 'rick', 'morty' ])) // { type: Maybe.Just, value: 'RICK' } ie. Just 'RICK'
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))
}
}
module Html where
import Control.Monad.Reader (Reader, ask, runReader)
import Data.List (intercalate)
import Prelude hiding (div)
type Html = String
type Email = String
div :: [Html] -> Html
main :: IO ()
main =
putStrLn "what is your email address?" >>
getLine >>= \email ->
putStrLn . show $ view email
view :: Email -> Html
view email =
div
[ page email