Skip to content

Instantly share code, notes, and snippets.

@dewey92
Last active June 27, 2017 05:49
Show Gist options
  • Select an option

  • Save dewey92/d8fec123d8806803731c0e6b25dc8ad8 to your computer and use it in GitHub Desktop.

Select an option

Save dewey92/d8fec123d8806803731c0e6b25dc8ad8 to your computer and use it in GitHub Desktop.
// filter :: (a → Bool) → [a] → [a]
const filter = fn => arr => arr.filter(fn)
// isEven :: Int → Bool
const isEven = num => num % 2 === 0
// oneToSix :: [Int]
const oneToSix = [1, 2, 3, 4, 5, 6]
// filterEvenNumbers :: [Int]
const filterEvenNumbers = filter(isEven)(oneToSix)
# [2, 4, 6]
// reduce :: (b → a → b) → b → [a] → b
const reduce = (fn, init) => arr => arr.reduce(fn, init)
// addStrLength :: Int → String → Int
const addStrLength = (acc, x) => acc + x.length
// init :: Int
const init = 0
// myName :: [String]
const myName = ['Jihad', 'Dzikri']
// computeStringLength :: Int
const computeStringLength = reduce(addStrLength, init)(myName)
# 11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment