linh <[email protected]> writes:
> # ruby code
> def foo(x, y)
> x + y
> end
>
> def bar
> [1, 2]
> end
>
> foo(*bar) # this is fine, the result will be 3
> foo(bar) # this is not ok, will raise exception
>
> bar returns an array of size 2, but foo expects 2 parameters not an
> array.
In Clojure, this would look something like:
(defn foo [x, y]
(+ x y))
(def bar [1, 2])
(apply foo bar) ;; becomes (foo 1 2), or 3
(foo bar) ;; errors out
Pretty close to Ruby, actually. The apply function calls the first
argument (a function) with the rest of the arguments to apply becoming
the argument list to the first function. In Clojure you see functions
being passed as arguments to other functions a lot; this is the biggest
difference from the Ruby version.
-Phil
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---