clojure - How to build data structure using nested calls to `map` -
i want build sequence of maps (the data structure) using nested calls map (the function). here's i'm doing
(defn point [x y] {:x-coord x :y-coord y}) (defn grid [rows columns] (mapcat (fn [x] map (fn [y] (point x y)) (range columns)) (range rows)))
which compiles fine, doesn't work expect. expected call grid result in sequence of maps:
user=>(grid 2 2) ({:x-coord 0, :y-coord 0} {:x-coord 0, :y-coord 1} {:x-coord 1, :y-coord 0} {:x-coord 1, :y-coord 1})
but got sequence of ints:
user=> (grid 2 2) (0 1 0 1)
so question is, how can sequence want? and, why did particular sequence got?
you need call map
. in way wrote inner map
not being called. returning result of (range columns)
.
try this:
(defn grid [rows columns] (mapcat (fn [x] (map (fn [y] (point x y)) (range columns))) (range rows)))
you can make call point
that: #(point x %)
. easier read way wrote.
Comments
Post a Comment