On Feb 19, 2:39 pm, Phil Hagelberg <p...@hagelb.org> wrote: > linh <nguyenlinh.m...@gmail.com> 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.
In this particular example, bar is a constant, but I suspect that linh may be interested in a more general solution. Of course the difference is slight: ;; foo as defined above (defn bar [k] [k (* k 2)]) (apply foo (bar 3)) ;; => 9 (foo (bar 3)) ;; => throws IllegalArgumentException -- Nathan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---