Skip to content

Instantly share code, notes, and snippets.

@forforf
Created July 22, 2011 16:19
Show Gist options
  • Save forforf/1099771 to your computer and use it in GitHub Desktop.
Save forforf/1099771 to your computer and use it in GitHub Desktop.
Comprehension Patterns for Coffeescript
#map equivalent
x = for i in ['a', 'b', 'c']
i + i
#=> ['aa', 'bb', 'cc']
#for each equivalent
for i in ['a', 'b', 'c']
#do side effect
null
#inject/collect equivalent example
for own key, val of [1, 2, 3, 4]
memo = (memo || 0) + val
#inject/collect equivalent general form
for own, key, val of someObjOrArray
memo = (memo || initVal) #set some intial value
memo = fn(memo, key, val) #operate on previous memo to get current memo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment