0

I am having trouble iterating over the function arguments passed in to the following function.

(defn iterateDates
  [& dates]
  (let [ukFormatter (java.time.format.DateTimeFormatter/ofPattern "dd-MM-yyyy")]
    (for [i [dates]]
      (java.time.LocalDate/parse i ukFormatter))))

(iterateDates "09-10-2019" "10-10-2019" "11-10-2019")

This however when called, returns the following error:

Error printing return value (ClassCastException) at clojure.core/getOldestDate$iter$fn$fn (core.clj:96).
clojure.lang.ArraySeq cannot be cast to java.lang.CharSequence

I am not sure on how to iterate over the arguments passed in and take each element as a separate value which can then be passed to another function.

My goal ultimately with the code will be to compare a list of dates and find the oldest date in there. This code just tries to parse each argument as a date.

3
  • 2
    This code won't run. You have a bracket mistatch at (for [i [dates)]. Please post the actual code. Also you have the body of the for wrapped in two pairs of parenthesis, which will cause the return of parse to be called as a function. Is that your intent? Also, please post the full stack trace. On first brush, that error doesn't seem to stem from this code. Commented Oct 9, 2019 at 12:14
  • I have been running this in REPL and that's where I got the error, running it as normal does not produce an error but neither an output. Commented Oct 9, 2019 at 12:32
  • 3
    I think you just mean (for [i dates]. I don't know how that would cause that error, but you're passing the entire dates sequence to parse at once with how you have it now. Commented Oct 9, 2019 at 12:35

2 Answers 2

3
(defn iterateDates
  [& dates]
  (let [ukFormatter (java.time.format.DateTimeFormatter/ofPattern "dd-MM-yyyy")]
    (for [i dates]
      (java.time.LocalDate/parse i ukFormatter))))

(iterateDates "09-10-2019" "10-10-2019" "11-10-2019")

This version should work.

You wrote (for [i [dates]] in your original code, and it worked as you unintentionally specified:

  1. It used a vector [] of dates for iteration, where dates is already a sequence.
  2. The first element of this vector is the dates, which is an AraySeq.
  3. java.time.LocalDate/parse tried to parse ArraySeq as a CharSequence and failed.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your answer and the explanations. I realise now the issue and the code indeed worked.
0

Thought I'd update the answers as I have found another way to achieve this without having to iterate the args:

(defn iterateDates
  [& dates]
  (let [ukFormatter (java.time.format.DateTimeFormatter/ofPattern "dd-MM-yyyy")]
    (map #(java.time.LocalDate/parse % ukFormatter) dates)))

The map function will apply the date formatting to every element of dates and return it as a new list.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.