clojure - Sequentially calling a function with elements from a vector -
suppose have function f
accepts 2 arguments x & y. have vector x composed of elements x1, x2, ... xn.
how can write function g
, g(x, y) calls f(xi, y) x?
further specification: g
return vector, each element stores result of f(xi, y). understanding should considering map or mapv.
you can use map
implement it.
i'm using lambda expression here define function takes 1 argument.
(defn f [x y ] (...) ) // apply f x y every element of xs // in order need function takes 1 argument x_i , takes second argument somehere else - second argument of g // that's lambda \x -> f x y - in short form. (defn g [xs y] (map (fn [x] (f x y)) xs))
for example
(def x [1 2 3 4]) (defn f [x y] (* x y)) (g x 3) ;=> (3 6 9 12)
Comments
Post a Comment