Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You want to use <code>for</code> in a nested fashion:</p> <pre><code>(for [i (range 1 (inc 4))] (for [j (range 1 (inc 3))] (+ (* 10 i) j))) ;; '((11 12 13) (21 22 23) (31 32 33) (41 42 43)) </code></pre> <p><strong>EDIT:</strong> expanded with an example implementation</p> <p>For example:</p> <pre><code>(defn build-seq [f lower upper step] (for [i (range lower (+ upper step) step)] (f i))) (build-seq #(* % %) 2 6 2) ;; '(4 16 36) (defn build-table [f [ilower iupper] [jlower jupper]] (for [i (range ilower (inc iupper))] (for [j (range jlower (inc jupper))] (f i j)))) (build-table #(+ (* 10 %) %2) [1 4] [1 3]) ;; '((11 12 13) (21 22 23) (31 32 33) (41 42 43)) </code></pre> <p>Your three input/output samples do not display a consistent signature for one variable and two ; furthermore, the <code>step</code> argument seems to be optional. I'm skeptical about the existence of a nice API that would retain the samples' syntax, but I can try something different (even if I do believe the simple embedded <code>for</code> forms are a better solution):</p> <pre><code>(defn flexible-range [{:keys [lower upper step] :or {lower 0}}] (let [[upper step] (cond (and upper step) [(+ upper step) step] step (if (pos? step) [Double/POSITIVE_INFINITY step] [Double/NEGATIVE-INFINITY step]) upper (if (&lt; lower upper) [(inc upper) 1] [(dec upper) -1]) :else [Double/POSITIVE_INFINITY 1])] (range lower upper step))) (defn build-table ([f [&amp; params]] (for [i (flexible-range params)] (f i))) ([f [&amp; iparams] [&amp; jparams]] (for [i (flexible-range iparams)] (for [j (flexible-range jparams)] (f i j))))) (build-table #(* % %) [:lower 2 :upper 6 :step 2]) ;; '(4 16 36) (build-table #(+ (* 10 %) %2) [:lower 1 :upper 4] [:lower 1 :upper 3]) ;; '((11 12 13) (21 22 23) (31 32 33) (41 42 43)) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload