Several comments:
1. camelCase is not idiomatic for Clojure. Prefer lowercase-dashed-style.
2. use :keywords for map keys and, more generally, for any enumeration-type
values (coming from a closed set of options). If you want JSON output,
transform these into camelCased strings only at the "checkout desk".
3. make-student-factory is what you'd call a *curried* function. You'd have
an easier time with it if you declared it as a regular two-argument
function and then used either clojure.core/partial or a literal #(make-student
teacher-name %). That would allow you to use the function either directly
as a student factory, or as a factory of student factories. Note that your
approach to use a closure *is *idiomatic, I'm just advising a more
idiomatic way to get it.
On Saturday, March 9, 2013 11:49:26 PM UTC+1, Craig Ching 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 [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.