Skip to content

Instantly share code, notes, and snippets.

@nickfargo
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save nickfargo/11133171 to your computer and use it in GitHub Desktop.

Select an option

Save nickfargo/11133171 to your computer and use it in GitHub Desktop.

Transformers

mapping

“Remove stickers”

mapping = ( map ) -> ( reductor ) -> ( result, input ) ->
  reductor result, map input

filtering

“Discard rotten”

filtering = ( predicate ) -> ( reductor ) -> ( result, input ) ->
  if predicate input then reductor result, input else result

flattening

“Aggregate slices”

flattening = ( flatten ) -> ( reductor ) -> ( result, input ) ->
  result.reduce reductor, flatten input

Reducers

reducer

Returns a logical collection object of collection whose reduce method is a transformation of collection’s reduce.

reducer = ( collection, transformer ) ->
  reduce: ( reductor, seed ) ->
    collection.reduce transformer( reductor ), seed

reducer.map = ( fn, collection ) ->
  reducer collection, mapping fn

reducer.filter = ( fn, collection ) ->
  reducer collection, filtering fn

reducer.flatten = ( fn, collection ) ->
  reducer collection, flattening fn

module.exports = reducer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment