Splats in CoffeeScript

Brought by Filip Michalowski @fmmm http://filmic.eu Based on "Programming in CoffeeScript" book by Mark Bates

by kanelbula

CoffeeScript

log = (value) ->
  $('body').append "#{value}<br/><br/>"

    
###
 Splat as last argument
### 
 
splatter = (etc...) ->
 "Length: #{etc.length}, Values: #{etc.join(', ')}"
 
log splatter()
log splatter("a", "b", "c")
 
 
 
###
 Splat as middle argument
### 
 
joinArgs = (first, middles..., last) ->
  parts = []

  if first?
    parts.push first.toUpperCase()

  for middle in middles
    parts.push middle.toLowerCase()

  if last?
    parts.push last.toUpperCase()

  parts.join('/')

log joinArgs("a")
log joinArgs("a", "b")
log joinArgs("a", "B", "C", "d")
  
  

###
 Passing deconstructed array in as an argument
### 
 
splatter = (etc...) ->
  "Length: #{etc.length}, Values: #{etc.join(', ')}"

a = ["a", "b", "c"]
log splatter(a)
log splatter(a...)