0

I'd like to create a getnext fn that looks for a element in a coll and when match, return the next element. Also, it should return the first element if the last one is passed as argument.

(def coll ["a" "b" "c" "d"])

(defn get-next [coll item] ...)

(get-next coll "a") ;;=> "b"
(get-next coll "b") ;;=> "c"
(get-next coll "c") ;;=> "d"
(get-next coll "d") ;;=> "a" ; back to the beginning

Thanks!

4 Answers 4

7

How about this:

  1. Append first item at the end of the sequence (lazily),

  2. Drop non-items,

  3. Return what's left (nil if item not found).

Or in code:

    (defn get-next [coll item]
      (->> (concat coll [(first coll)])
           (drop-while (partial not= item))
           second))
7
  • Can you please explain each step a little bit more? Trying to learn clojure I am. Apr 12, 2018 at 13:35
  • Not sure, I gave explanation upfront. Only tricky bit is the threading macro (->>), otherwise the code does exactly what bullet points advertise: extends the collection by adding first element at the end, drops elements that don't match the search item, then returns next item after a match. Matched item would be first, we want one after that, so second. Look up the functions I referenced on ClojureDocs, there are additional comments and examples there. Apr 12, 2018 at 18:21
  • Ok, got it, it's (defn get-next [coll item] (second (drop-while (partial not= item) (concat coll [(first coll)])))) expanded. Apr 13, 2018 at 10:24
  • How is the appending in 1. "lazy"? Apr 13, 2018 at 10:55
  • To preserve laziness, maybe a small modification using cycle? (defn get-next [coll item] (->> (cycle coll) (take (inc (count coll))) (drop-while (partial not= item)) second))
    – Henrik
    Apr 13, 2018 at 17:33
4

There are certainly purer lisp approaches than this one but, hey, as long as we got .indexOf, we might as well use it. The key to simplicity is that, plus cycle, so we don't have to check for the last item.

(defn get-next [coll item]
  (nth (cycle coll) (inc (.indexOf coll item))))

Some test runs:

   (get-next ["A" "B" "C" "D"] "B")

=> "C"
   (get-next ["A" "B" "C" "D"] "D")

=> "A"
   (get-next ["A" "B" "C" "D"] "E")

=> "A"

Whoops! Well, we didn't specify what we wanted to do if the item wasn't in the collection. Idiomatically, we would return nil, so we need a new get-next:

(defn get-next-2 [coll item]
  (let [i (.indexOf coll item)]
       (if (= -1 i) nil (nth (cycle coll) (inc i)))))

And now we catch the not-there case:

   (get-next-2 ["A" "B" "C" "D"] "Q")

=> nil   
2

I would convert coll to map and use it for lookups:

(def doll (zipmap coll (rest (cycle coll))))

(doll "a") => "b"
(doll "b") => "c"
(doll "d") => "a"
3
  • You would loose the coll order though
    – leontalbot
    Apr 12, 2018 at 14:27
  • Does the coll order matter in this function?
    – Henrik
    Apr 13, 2018 at 17:47
  • And you never really lose anything in Clojure since you still have the original. =) Apr 15, 2018 at 15:16
1

This is a good job for drop-while:

(defn get-next
  [coll item]
  (let [remainder (drop-while #(not= % item) coll)]
    (when (empty? remainder)
      (throw (IllegalArgumentException. (str "Item not found: " item))))
    (if (< 1 (count remainder))
      (nth remainder 1)
      (first coll))))

(dotest
  (let [coll [1 2 3 4]]
    (is= 2 (get-next coll 1))
    (is= 3 (get-next coll 2))
    (is= 4 (get-next coll 3))
    (is= 1 (get-next coll 4))
    (throws? (get-next coll 5))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.