Clojure 将单个变量传递给函数 [英] Clojure Passing Individual Variables to Function

查看:24
本文介绍了Clojure 将单个变量传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一些包含节点列表的向量传递给 clojure 中的函数,如果我要输入变量,该函数可以工作,但我不确定如何一次从每个向量传递一个变量.

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)))

我需要使用机器人将 ItemPickUp 和 ItemDestination 中的变量传递到 shortestPath 中,而不是传递其中的一个变量,同时传递 :a1 和 :Mail ,反之亦然.

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.

我如何单独传递每个变量,以便第一次迭代的前两个变量是 :a1 和 :Storage 等等?

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?

谢谢.

推荐答案

在 Clojure 中,这通常使用 map - 它接受一个函数 f 和任意数量的集合,然后懒惰地生成一个 f 的序列code>(f (first coll1) (first coll2)...), (f (second coll1) (second coll2)...)...
所以它应该只是

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)

(其他一些函数式语言将单集合mapping 和多集合zipping 区别开来——我相信Haskell 在这里有影响.它需要这个,因为它的功能已经固定arities,所以你有 zipWith,zipWith3 等.用括号来表示参数的数量意味着 Lisps 不必处理这个.)

(Some other functional languages distinguish one-collection mapping from multi-collection zipping - 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆