0

Clojure Quilライブラリでアルゴリズムを理解することに深刻な問題があります。私は機能的なミドルウェアモードでthisのようなネットワーク/フロッキングパターンのいくつかの図を作ろうとしてきました。私の質問は、任意の点を線で他の点に接続するためにQuildで入れ子になった "forループ"を作成する方法です。その違いは、1-> 2 3-> 4 ...などに接続された1組の点と、1-> 2 1-> 3 1-> 4 2-> 3 2-> 4との間にある。等Clojure Quil:線を描いている間にネストされたループ

私のプログラムは、この質問の目的のために一般的に編集しようとしましたが、このように見えます。

(ns stack-question.core 
    (:require [quil.core :as q] 
      [quil.middleware :as m])) 

;how to draw the point 
(defn draw-points [point] 
    (q/point (:x point) (:y point))) 

;the main data structure 
(defn make-point [num] 
    {:name num 
    :x (rand 500.0) 
    :y (rand 500.0)})  

;draws connections between two objects 
(defn connections [p1 p2] 
     (q/stroke 200 200 100 150) 
     (q/line (:x p1) (:y p1) (:x p2) (:y p2))))) 

(defn setup [] 
    (q/frame-rate 30) 
    {:points (map make-points (take 1000 (range)))}) 

;this function isn't important to the question 
;but let's just say that the points move position each frame 
(defn update-state [state] 
    (update state :points move-group)) 

;this is the problem function here. 
;I'm trying to map over the collection, but with a fixed head 
;and do so for each element in the collection 
(defn make-conts [coll] 
    (loop [x coll] 
    (when (not (empty? (rest coll))) 
     (map (partial connections (first x)) (rest x)) 
     (recur (rest x))))) 


(defn draw-state [state] 
    (do 
    ;(q/frame-rate 1) 
    (q/background 0) 
    (doseq [x (map draw-point (:points state))] x) 
    (doseq [x (make-conts (:points state))] x))) 

(q/defsketch stack-question 
    :title "GL" 
    :size [500 500] 
    :setup setup 
    :update update-state 
    :draw draw-state 
    :middleware [m/fun-mode] 
    :renderer :opengl) 

これは完全に不透明ではないことを望みます。

答えて

0

あなたのコードには二つの問題があります。

は、最初は、あなたが無限ループにつながる、when文で(rest coll)代わりの(rest x)をチェックしているということです。

mapは遅延シーケンスを返します。つまり、connectionsへの呼び出しは行われません。これは、mapへの呼び出しをdoallでラップすることで修正できますが、戻り値ではなく副作用に興味があるので、代わりにdoseqを使用してください。 (余分loopも削除されました)関数のこのバージョンでは動作するはず

(defn make-conts [coll] 
    (when (not (empty? (rest coll))) 
     (doseq [endpoint (rest coll)] 
     (connections (first coll) endpoint)) 
     (recur (rest coll)))) 

代わりの組み合わせを自分で生成するコードを書く、あなたがclojure.math.combinatoricsからcombinationsを使用することができます。

(require '[clojure.math.combinatorics :as combi]) 

(defn make-conts [coll] 
    (doseq [[p1 p2] (combi/combinations coll 2)] 
    (connections p1 p2))) 
関連する問題