On Tuesday, August 16, 2016 at 2:02:38 PM UTC-5, [email protected] wrote:
>
> Hello fellow Clojurists,
>
> I have just started learning Clojure is a side project in a Java bootcamp
> at The Iron Yard. I am tackling a project that I think will require a
> dynamically built vector of hashmaps.
>
> I am reading "Clojure for the Brave and True" and doing lots of research
> on the web, but Google will only answer questions that i know to ask...
>
> I have found atoms <http://clojure.org/reference/atoms> and agents
> <http://clojure.org/reference/agents> and am leaning towards using an
> atom for my specific problem.
>
> To expand on my problem. The challenge was originally to develop an app to
> manage an Animal Shelter in Java. I finished that well ahead of the class
> so I was challenged with reimplementing it in Clojure. In Java the core
> data element is a class called Animal, that is used as the basis of an
> ArrayList. My thought is to use a hashmap:
>
> {:name animals-name :species animals-species :breed animals-breed
> :description notes}
>
>
Sounds good. Could also use a defrecord to create it: (defrecord Animal
[name species breed description]). There are pros and cons.
And build a vector of these hashmaps.
>
Sounds good. You might end up finding it more useful for this case to use a
map, keyed by animal name for lookups.
>
> Given that animals are constantly coming into the shelter as rescues and
> being adopted out of the shelter, the current list of shelter residents
> needs to be updated with each new rescue or adoption.
>
> My questions are:
>
> 1. Atom, Agent or something else?
>
> Atom. Agents are best suited for simulations and a few other specialized
cases, they aren't really used that often in Clojure. Atoms are the most
common way to store state.
>
> 1. Am I thinking about my data structure in a Clojure-compatible way.
>
>
Yes.
> I will happily RTFM - please point me to the most helpful FM and I will RT.
>
You might find the first couple chapters of Clojure Applied to give you a
bit more depth on this stuff. [I am a co-author, so biased. :)]
I prefer to minimize the state-related things and keep them as separate
from the immutable data and pure functions as possible. So just the data
parts are something like:
;; depending on context, I may not actually bother with making this explicit
(def empty-animals {})
;; again, depending on context I might actually do this with a map rather
than broken out values
(defn add-animal
[animals name species breed description]
(assoc animals name {:name name :species species :breed breed
:description description}))
(defn remove-animal
[animals name]
(dissoc animals name))
Those functions are then pure functions that take immutable data and return
immutable data. You can feel really confident that they do exactly what you
say and they are easy to test and assemble for various purposes. You might
even be confident enough that you didn't need unit tests around this as
it's a thin wrapper around basic core functions.
Your state then holds an instance of the animals map. That might be in a
var defined by def or it might just be created and passed around, again
depends how things are arranged. But the atom parts for creating and
updating could look like:
;; create
(def animals-state (atom (empty-animals)))
;; update - re-use our pure function invoked on the value held by the atom,
to produce a new immutable value that is the next atom value
(swap! animals-state add-animal name species breed description)
;; remove
(swap! animals-state remove-animal name)
>
> Chris
>
>
--
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/d/optout.