Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 一系列值和返回值中的函数测试值_Javascript_Clojure_Clojurescript - Fatal编程技术网

Javascript 一系列值和返回值中的函数测试值

Javascript 一系列值和返回值中的函数测试值,javascript,clojure,clojurescript,Javascript,Clojure,Clojurescript,我是clojure脚本的新手,我试图在clojurescript中包含以下函数 function getLengthAtPoint(pt){ var i = 0; while(i < p.getTotalLength()) { var xy = p.getPointAtLength(i); // console.log(i, xy); if(Math.round(xy.y) == pt[1] &&

我是clojure脚本的新手,我试图在clojurescript中包含以下函数

function getLengthAtPoint(pt){
        var i = 0;
    while(i < p.getTotalLength())
    {
        var xy = p.getPointAtLength(i);
      // console.log(i, xy);
        if(Math.round(xy.y) == pt[1] && Math.round(xy.x) == pt[0])
      {
            return i;
      }

      i++;
    }

    return 0;
 }
但我想我可能错了,因为:什么时候不会返回
I
will

更新


想出这个

(defn get-length-at-point
  [pt {p :path}]
  (loop [i 0]
    (let [point (.getPointAtLength p i)]
    (cond
       (and (= (.round js/Math (.-x point)) (.round js/Math (:x pt)))
       (= (.round js/Math (.-y point)) (.round js/Math (:y pt)))) i
       (>= i (.getTotalLength p)) 0
        :else (recur (inc i))
       ))))

如果有人知道一种更简单的方法,请分享

,这里并不完全清楚您想要计算什么,但这是您追求的一些方法:

(defn get-length-at-point
  [pt p]
  (->> (map #(.getPointAtLength p %) (range (.getTotalLength p)))
       (filter (fn [point] (and (= (.round js/Math (.-x point)) (.round js/Math (:x pt)))
                                (= (.round js/Math (.-y point)) (.round js/Math (:y pt))))))
       (count)))

惯用的Clojure代码通常避免
循环
,而
映射就可以了
doseq
仅针对副作用运行,不会向您返回值。大多数情况下,Clojure代码也不使用索引。如果你在文章中添加了更多的内容来说明你想做什么,以及你可以调用哪些函数,我们可能会提供更多帮助。

这里不完全清楚你想计算什么,但这是你想要实现的一些方法:

(defn get-length-at-point
  [pt p]
  (->> (map #(.getPointAtLength p %) (range (.getTotalLength p)))
       (filter (fn [point] (and (= (.round js/Math (.-x point)) (.round js/Math (:x pt)))
                                (= (.round js/Math (.-y point)) (.round js/Math (:y pt))))))
       (count)))

惯用的Clojure代码通常避免
循环
,而
映射就可以了
doseq
仅针对副作用运行,不会向您返回值。大多数情况下,Clojure代码也不使用索引。如果你在文章中添加了更多的内容来说明你想做什么,以及你可以调用哪些函数,我们可能会提供更多帮助。

你的Javascript引用了
p
pt
,虽然只有
pt
是一个参数。是的,我在clojure版本中添加了一些额外的内容。如上所述,Javascript
getLengthAtPoint
是否真的有效?由于函数体在未定义的变量
p
上运行,这似乎不太可能。您的Javascript引用了
p
pt
,尽管只有
pt
是一个参数。是的,我在clojure版本中添加了一些额外的内容Javascript
getLengthAtPoint
,如上所述,真的有用吗?由于函数体是在一个未定义的变量
p
上运行的,所以这似乎不太可能。对不起,我认为通过Javascript会很明显。p是一个
元素,我正在尝试获取给定点的长度(与
getPointAtLength
相反),对不起,我认为通过Javascript可以明显看出这一点。p是一个
元素,我试图得到给定点的长度(与
getPointAtLength
相反)