Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpressey/850d0aee22a66bcd8e55182f7416936f to your computer and use it in GitHub Desktop.
Save cpressey/850d0aee22a66bcd8e55182f7416936f to your computer and use it in GitHub Desktop.
Calling an anonymous function directly in various languages

Just a little survey of what code to create an anonymous function and immediately call it looks like in a handful of high-level languages. All of these expressions should evaluate to 10.

Language Code
Python (lambda x: x+1)(9)
Ruby (lambda {|x| x+1}).call(9)
Lua (function(x) return x+1 end)(9)
Erlang (fun(X) -> X+1 end)(9)
Haskell (\x -> x+1)(9)
Scheme ((lambda (x) (+ x 1)) 9)
Perl (sub { @_[0]+1 })->(9)
Julia (x -> x+1)(9)
ES5 JavaScript (function(x) { return x+1 })(9)
ES6 JavaScript (x => x+1)(9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment