It’s probably fine to do a linear search with .indexOf for small vectors, but 
I’ll just say as a matter of style I prefer to use a couple of maps to hold 
previous and next items.  The maps are callable like functions, which I think 
reads very nicely.  Using your example data for ranks:

;; helper functions
(defn prev-map [coll] (zipmap (rest coll) coll))
(defn next-map [coll] (zipmap coll (rest coll)))

(def ranks [:private :corporal :sergeant :lieutenant :captain :major :colonel 
:general])

(def previous-rank (prev-map ranks))

(def next-rank (next-map ranks))

(next-rank :corporal)
;=> :sergeant

Perhaps, in your example with promotions you'd want the end items to stick 
rather than return nil for the overflow cases.  (In other situations, you might 
want to wrap around.)

(defn bounded-prev-map [coll]
  (assoc (zipmap (rest coll) coll) (first coll) (first coll)))

(defn bounded-next-map [coll]
  (let [lst (if (vector? coll) (peek coll) (last coll))]
    (assoc (zipmap coll (rest coll)) lst lst)))

Just a suggestion.


> On Feb 23, 2015, at 1:44 PM, Daniel Hinojosa <dh.evolutionn...@gmail.com> 
> wrote:
> This example works, but it still has the feel of a lot of duplication, are 
> there any refactorings that I can do to this to make it more concise?
> 
> https://github.com/dhinojosa/language-matrix/blob/master/clojure/protocols/protocols.clj

-- 
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/d/optout.

Reply via email to