The first solution looks good.  I had no idea that wrapping a defn
inside of let's would leave the function exposed.  Interesting.  Is it
considered idiomatic though?

On May 30, 2:16 pm, Erik Söhnel <eriksoeh...@googlemail.com> wrote:
> Hi,
>
> Not really mechanisms, but two Idioms will help you with your problem:
> Either wrap the defn in a let:
>
> (let [U (gen-matrix [1 2 2 -2 -1 -2 2 2 3] 3 3)
>       A (gen-matrix [1 2 2 2 1 2 2 2 3] 3 3)
>       D (gen-matrix [-1 -2 -2 2 1 2 2 2 3] 3 3)
>       S (gen-vector [3 4 5])]
>   (letfn [(prims-from [m] [(mult m U) (mult m A) (mult m D)])
>             (next-prims [l] (flatten (map prims-from l)))]
>     (defn primitives
>       "Generates pythagorean primitives.  Will filter them against
> pred if
>   given."
>       ([] (iterate next-prims [S]))
>       ([pred] (iterate #(filter pred (next-prims %)) [S])))))
>
> or use a default value for pred: (constantly true)
>
> (defn primitives
>   "Generates pythagorean primitives.  Will filter them against pred if
> given."
>   ([] (primitives (constantly true)))
>   ([pred]
>      (let [U (gen-matrix [1 2 2 -2 -1 -2 2 2 3] 3 3)
>            A (gen-matrix [1 2 2 2 1 2 2 2 3] 3 3)
>            D (gen-matrix [-1 -2 -2 2 1 2 2 2 3] 3 3)
>            S (gen-vector [3 4 5])]
>        (letfn [(prims-from [m] [(mult m U) (mult m A) (mult m D)])
>                (next-prims [l] (flatten (map prims-from l)))]))
>      (iterate #(filter pred (next-prims %)) [S])))
>
> The former will evaluate the values only once when the file is loaded
> and keep them around until your program dies and do not impose any
> runtime overhead when you call the function, because all variables are
> already initialized.
> The latter one uses a default argument to pred. (constantly true)
> returns a n-argument function wich returns true on every invokation.
>
> Erik

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to