使用 ubergraph 的最短路径 clojure 的递归函数
Recursive function for shortest path clojure using ubergraph
我目前正在使用 clojure 开发一个路线规划程序。我正在使用 ubergraph 创建我的数据结构,然后使用内置在最短路径算法中的 ubergraph。我让它处理传入的 2 个参数,也让它处理传入的 3 个参数。
但是,不是每次我想传入更多参数时都编写一个新函数,有没有办法编写一个可以接受任意数量参数的递归版本?
(defn journey [start end]
(alg/pprint-path (alg/shortest-path all-edges {:start-node start, :end-node end, :cost-attr :weight})))
(journey :main-office :r131)
(defn fullpath [start delivery end]
(journey start delivery)
(journey delivery end))
(fullpath :main-office :r131 :main-office)
(fullpath :main-office :r119 :main-office)
以上是我目前使用的代码,运行良好。
是否可以编写一个函数来接受下面的参数,并且仍然打印出所采用的路径。
(fullpath :main-office :r113 :r115 :main-office)
非常感谢任何帮助。
以下应该有效
(defn fullpath [& stops]
(map (fn [a b] (journey a b)) stops (rest stops) )
哪个
(fullpath :a :b :c ..)
收集
的结果
(journey :a :b)
(journey :b :c)
...
变成了collection。由于您的 return 旅程价值似乎为零,并且您只对打印它的副作用感兴趣,因此您可能想要放入一个 doall,即
(defn fullpath [& stops]
(doall (map (fn [a b] (journey a b)) stops (rest stops) ))
我目前正在使用 clojure 开发一个路线规划程序。我正在使用 ubergraph 创建我的数据结构,然后使用内置在最短路径算法中的 ubergraph。我让它处理传入的 2 个参数,也让它处理传入的 3 个参数。
但是,不是每次我想传入更多参数时都编写一个新函数,有没有办法编写一个可以接受任意数量参数的递归版本?
(defn journey [start end]
(alg/pprint-path (alg/shortest-path all-edges {:start-node start, :end-node end, :cost-attr :weight})))
(journey :main-office :r131)
(defn fullpath [start delivery end]
(journey start delivery)
(journey delivery end))
(fullpath :main-office :r131 :main-office)
(fullpath :main-office :r119 :main-office)
以上是我目前使用的代码,运行良好。
是否可以编写一个函数来接受下面的参数,并且仍然打印出所采用的路径。
(fullpath :main-office :r113 :r115 :main-office)
非常感谢任何帮助。
以下应该有效
(defn fullpath [& stops]
(map (fn [a b] (journey a b)) stops (rest stops) )
哪个
(fullpath :a :b :c ..)
收集
的结果(journey :a :b)
(journey :b :c)
...
变成了collection。由于您的 return 旅程价值似乎为零,并且您只对打印它的副作用感兴趣,因此您可能想要放入一个 doall,即
(defn fullpath [& stops]
(doall (map (fn [a b] (journey a b)) stops (rest stops) ))