Fork me on GitHub
#beginners
<
2020-09-06
>
gorniak11:09:27

I have vector [33 "A" 2 "B" "C" "H"] and I would like it to look [[33 "A"] [2 "B"] [1 "C"] [1 "H"]] Any tips ?

Drew Verlee12:09:33

Is the input missing a 1 on purpose? Assuming it isn't. (partition 2 [33 "A" 2 "B" "1" "C" "1" "H"]) would do the trick. Otherwise your input isn't uniform and I would use reduce or loop. Likely loop because you have to deal with 2 values at a time @gorniakowski

Drew Verlee12:09:07

I guess if that's the only instance of that input you could use partition and then just update the last value.

valtteri13:09:40

Maybe something like this?

user> (def v [33 "A" 2 "B" "C" "H"])
#'user/v
user> (for [s (filter string? v)
            :let [prev (get v (dec (.indexOf v s )))]]
        [(if (int? prev) prev 1) s])
([33 "A"] [2 "B"] [1 "C"] [1 "H"])

Karo18:09:25

Dear all, how can I calculate period in months given start and end dates in Clojure? Thanks

phronmophobic18:09:43

there are a few clojure library options, but the built in java time libraries work pretty well. check out https://docs.oracle.com/javase/8/docs/api/java/time/Period.html

thanks2 3
phronmophobic18:09:34

(let [start-date (java.time.LocalDate/of 2018 2 1)
      end-date (java.time.LocalDate/of 2020 5 10)]
  (java.time.Period/between start-date end-date))

👍 9