Skip to content

Instantly share code, notes, and snippets.

@Kirens
Created January 29, 2019 09:43
Show Gist options
  • Save Kirens/22ba0528a6d00e2d7a2d754b62707d69 to your computer and use it in GitHub Desktop.
Save Kirens/22ba0528a6d00e2d7a2d754b62707d69 to your computer and use it in GitHub Desktop.
Propositions in conditionals
// is :: (U String Constructor) ---> T --(Predicate for `object` being a `test`)--> Bool
const is = test => object =>
typeof test === 'string'
? typeof object === string // object is assured to be a string
: object instanceof test // Object can be anything but a string
const x = [1, 2, 3]
if ( is(Array)(x) ) {
// x is assured to be an array
x.push(5)
} else {
// x can be anything but an array
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment