Skip to content

Instantly share code, notes, and snippets.

@metalim
Last active August 29, 2015 14:23
Show Gist options
  • Save metalim/3eaa4940c7acaa095af4 to your computer and use it in GitHub Desktop.
Save metalim/3eaa4940c7acaa095af4 to your computer and use it in GitHub Desktop.
class P
  constructor: ( @x, @y )->
p = new P 1, 2
args = [ 1, 2 ]
a = 1
b = 2

###call function immediately

do ( a, b )->123
# equivalent to
(( a, b )->123)(a, b)

# works with complex variables or constants as well
do ( a = args[0], b = 5 )->123
# eq
(( a, b )->123)(args[0], 5)

###apply arguments

a.b args...
# eq
a.b.apply a,args

# especially useful with object creation
a = new A args...
# alternative is either manually unrolling array
a = new A args.a, args.b
# or writting long piece of complicated code
a = do ( A, args, ctor=-> )->
  ctor:: = A::
  child = new ctor
  result = A.apply child, args
  if Object(result) is result then result else child
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment