Clojure将各个变量传递给函数 [英] Clojure Passing Individual Variables to Function
问题描述
(def ItemPickUp [:a1:Mail])
(def ItemDestinations [:Storage:a1])
(def Robot {[ItemPickUp] [ItemDestinations]})
(defn shortestPath [g start dest]
(let? (fn [[vertex _]](not = vertex dest))]
( - >最短路径g开始)
( - > )
first
(nth 2))))
(应用第一机器人)(应用第二机器人))
我需要使用机器人将ItemPickUp和ItemDestination中的一个变量传递给smallestPath,而不是传递其中的一个变量通过:a1
我如何单独传递每个变量,所以第一个迭代的前两个变量是:a1和:存储等等。
感谢。
通常使用 map
- 它需要一个函数 f
和任意数量的集合,并且懒惰地产生一个第一个coll1)(第一个coll2)...)
,(f(second coll1)(second coll2)...)
...
所以应该只是
(map(partial shortestPath G)ItemPickup ItemDestinations)
(其他一些功能语言区分一个集合 map
ping从多集合 zip
ping - 我相信Haskell在这里有影响力。它需要这个,因为它的函数有固定的字母,所以你有 zipWith
, zipWith3
等有parenteses指示数字的参数意味着Lisps不必处理。)
I am trying to pass some vectors containing a list of nodes in to a function in clojure the function works if i was to type the variables in but i am not sure how to pass a single variable from each vector in at a time.
(def ItemPickUp [:a1 :Mail])
(def ItemDestinations [:Storage :a1])
(def Robot {[ItemPickUp] [ItemDestinations]})
(defn shortestPath [g start dest]
(let [not-destination? (fn [[vertex _]] (not= vertex dest))]
(-> (shortest-paths g start)
(->> (drop-while not-destination?))
first
(nth 2))))
(apply shortestPath G (apply first Robot)((apply second Robot)))
I need to pass a variable from the ItemPickUp and ItemDestination into shortestPath using robot but instead of passing one of the variable in it passes both :a1 and :Mail and vice versa for the other one.
How do i go about passing each variable in individually so the first two variables for the first iteration is :a1 and :Storage and so on?
Thanks.
In Clojure this is normally done with map
- it takes a function f
and any number of collections and lazily produces a sequence of (f (first coll1) (first coll2)...)
, (f (second coll1) (second coll2)...)
...
So it should just be
(map (partial shortestPath G) ItemPickup ItemDestinations)
(Some other functional languages distinguish one-collection map
ping from multi-collection zip
ping - I believe Haskell is influential here. It needs this because its functions have fixed arities, so you have zipWith
,zipWith3
etc. Having the parenteses to indicate number of arguments means Lisps don't have to deal with that.)
这篇关于Clojure将各个变量传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!