Your code looks fine to me. One alternative for make-students would have been

(defn make-students
  [teacherName n]
  (map #(make-student teacherName (make-student-name %) 0) (range n)))

In case you're not familiar with it, the #() notation creates an
anonymous function in the same way as (fn [_]), but a bit shorter; the
arguments to the function are %, %2, %3, etc.; the first argument may
also be %1. The arity of the anonymous function is determined by the
arguments that appear in its expression. This is thus still creating a
closure, but it is using it immediately.

On 9 March 2013 23:49, Craig Ching <craigch...@gmail.com> wrote:
> Hi all,
>
> I wrote some code to generate some example data for a web ui that I'm
> working on and I was wondering if I could get some advice about it to ensure
> I'm on the right track in my clojure learning.  Basically its a very simple
> program that generates a number of JSON files that contain information, I've
> modeled the problem as a teacher-student relationship where each teacher is
> a file that contains data about a number of students.  The inputs to the
> main function (gen) takes the number of teachers and the number of students
> per teacher and generates sample data that can be consumed by the web ui.
> What I'm looking for is:
>
> 1. Am I doing things the "clojure way", is my program idiomatic?  If not,
> advice is appreciated.
> 2. Specifically the main function in the program (make-student-factory) I've
> modeled as a closure and I feel like there is probably a better way to do
> it, what are your thoughts on that?
>
> Here is the code:
>
> (ns teachers.core
>   (:require [clj-json.core :as json]))
>
> (defn- gen-items-json
>   [students]
>   (json/generate-string {"timestamp" 5000
>                          "items" students
>                          "identifier" "id"
>                          "label" "id"
>                          "rc" 200
>                          "msg" "Data retrieved successfully."}))
>
> (defn- make-student
>   [teacherName
>    studentName
>    age] {"id" (str teacherName "!" studentName)
>            "TeacherName" teacherName
>            "StudentName" studentName
>            "age" age})
>
> (defn- make-name [prefix n] (str prefix n))
> (defn- make-teacher-name [n] (make-name "TEACHER" n))
> (defn- make-student-name [n] (make-name "STUDENT" n))
>
> (defn- make-student-factory
>   "Returns a function allowing you to create a number
>    of student definitions for the given teacher."
>   [teacherName]
>   (fn [n]
>     (make-student teacherName (make-student-name n) 0)))
>
> (defn- make-students
>   [teacherName n]
>   (let [fac (make-student-factory teacherName)]
>     (map fac (range n))))
>
> (defn- spit-json
>   [teacherName n]
>   (println "Creating " n " students for teacher: " teacherName)
>   (spit (str teacherName ".json")
>         (gen-items-json (make-students teacherName n))))
>
> (defn gen
>   [numTeachers numStudents]
>   (doseq [teacherName (map make-teacher-name (range numTeachers))]
>     (spit-json teacherName numStudents)))
>
> Thanks for any comments!
>
> Cheers,
> Craig
>
> --
> --
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

-- 
-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to