Fork me on GitHub

Is #(nth % 1) exactly the same as first?


this will be equivalent of second and unlike second it will fail if collection is empty consider this #(nth % 0 nil)


I see, thanks for the correction!


"will fail" means something like an out of bound exception?


yes, IndexOutOfBoundsException should be thrown

thanks2 1
Edward Ciafardini13:04:13

Hello. I'm having an issue using slurp. I was following this article to get some experience using it: When I run the command:

(slurp "")
The result looks like this: 9vTƌE,QZ5,V.&?7WIrrP36틈]H<,ASulbdx[L!ż\tzI:$\r<?:r#[email protected]ד=\tfcZw.f\nNn3{O{\b2R|%{\r)Z4C!tH\\ά5!m£\n%wMN%{ĆX^kCNqd_.J#87S<[tSϦof#:}3a#B\rTci\r%oޅL!=k<_%?\f*'aƊm#wn2zEEzg]$բ|dCUWqٙܟ4D5߱'http://fI.GY \nV;[email protected]&ȱ}Im2Y'8r\fjt7#[email protected]^ (this is only a portion of the output) I tried adding a param for encoding: (slurp "" :encoding "UTF-8") It's still giving me the cryptic response.


I think you're getting a gzipped response


you may not be able to fix that with slurp


slurp is a quick and dirty tool, maybe too quick and dirty for this task - you can either use your browser to save that page to disk, then open it from disk with slurp, or use something like as a real http client


or I guess you could theoretically use the gzip stream stuff built into Java to decode the slurp output probably

Johan Basson14:04:59

Good day. Where can I get a tutorial or guide on how to use the repl?

Dimitar Uzunov14:04:13

its interactive so it is easier to demonstrate with a video than a guide


Hi team, how can I idiomatically replace two maps in this data with other maps that has same keys but different values

[{"key1" "value1"} {"key2" "value2"} {"key3" "value3"} {"key4" "value4"}]
want to update first with {"key1" "value100"} and second {"key1" "value200"} order of maps can be changed so update based on index is not best choice.


(into [] (map #(replace-map %))
      [{"key1" "value1"} {"key2" "value2"} {"key3" "value3"} {"key4" "value4"}])
good case for using transducer you need to implement replace-map function which should take a single map from collection and based on condition either leave it intact or change based on your logic

thanks2 1
👍 1

just curious


On other languages I had to do a challenge where I had to calculate bowling scores and keep track on spare and strikes . If I have a collection like this : ( 6 4 0 0 10 4 3 0 0 0 0 0 ) How in clojure do I calculate the total score


(reduce + scores)


hmm, that simple. but on a strike the next two scores need to be doubled


or maybe you need to calculate the bonuses over the top of that - still doable with reduce


yes, I have


I think you need to track rolls per frame to distinguish strikes and spares though


(take 10 (map vector (range) (partition-all 3 1 '( 6 4 0 0 10 4 3 0 0 0 0 0 )))) gets you the 10 frames with frame number and later scores that can affect the score. But impossible to calculate without knowing which 10's are strikes and which are spares


easy a 10 is a strike


and when two seperate numbers are together 10 it is a strike so in my made up it the 6 and 4


so the frames are frame 1 : 6 4 frame 2 : 0 0 frame 3: 10 frame 4: 4 5 frame 5 till 10 : 0 0


oh. making this a variable length encoding adds lots of complexity and quite possibly an impossible ambiguity


I know. it was a exercism challenge I tried to do in ruby and haskell


Maybe, I do not see totally where you are heading to @U3ES97LAC


whatever you did in haskell will most likely work almost identically here


I could not solve this one in both 😞

Eric Ihli19:04:02

Is there something like take-through (like take-while but including the next after the predicate return value changes) in Clojure? I see it was but I can't find it in the docs now. If there's not, then is there a commonly accepted library of "extended utilities" that has it?


Maybe take-upto from fits the bill

🙏 1

dev=> (take-while even? [2 4 6 8 10 11 12 13])
(2 4 6 8 10)
dev=> (into [] (halt-when (complement even?) conj!) [2 4 6 8 10 11 12 13])
[2 4 6 8 10 11]

🤙 1

halt-when is a bit strange to use because in the 2-arity version it needs to know about the internals of the transduction, since the accumulator here is a transient.


@U050ECB92 ^ this seems odd to me — am I using this correctly?


Debatable whether this is a bug


There is a jira for it with more details


Ah, OK. Will look it up. Thanks!


there is a bug IMHO, but it's in into not halt-when


Gotcha. Yeah, was just reading that over and digesting it. Is into the only transducer that uses a transient? (I’m kinda surprised if that’s the case)


it's not the transient that is the issue


it's the fracturing of the completion arity


it adds metadata and calls persistent! after the call to transduce


rather than within the completion arity


Ah… yeah, that’s a bit subtle…


lmk if I can clarify that in the JIRA


No, that makes perfect sense now you’ve explained it.

Alexander Kaiser20:04:07

Recommended way for id-based lookups? I want to express the following:

(def input
  {:artists [#:artist{:id 0,
                      :founding-date 2000}],
   :albums [#:album{:id 0,
                    :artist 0,
                    :release-date 2002}]})

(def output
  [#:album{:id 0,
           :artist 0,
           :release-date 2002,
           :artist-years-active-at-release 2}]
One possible solution:
(defn group-by-version [{:keys [artists albums]}]
  (let [artists-by-id (group-by :artist/id artists)]
        ;; before using first like this i should (spec) check that :artist/id is distinct in artist or use something like only from the tupelo library. Omitted for readability.
        (first (get artists-by-id (album :album/artist)))
        ((fn[artist] (- (:album/release-date album)
                        (:artist/founding-date artist))))
        (assoc album :album/artist-years-active-at-release)
(group-by-version input)
However this feels unnecessary verbose and I especially would like to replace the group-by part. Is it better to use spectre or meander for these kinds of operations? Should I structure the data in a different way? Should I transform the data before I do those operations? E.g. with lilactown's new autonormal? I am missing something very obvious here?

Alexander Kaiser21:04:02

Thank you! This helps a lot


If I pass a map with a single key-value pair to a function, how can I destructure the name of the key as a string into a symbol?


@ashnur Not sure I’d recommend it but:

dev=> (defn foo [[& [[k v]]]] (str "key=" k ", val=" v))
dev=> (foo {:a 1})
"key=:a, val=1"
dev=> (foo {:a 1 :b 2})
"key=:a, val=1"


it's more like (foo {"this_is_backend_string_for_multiple_things" [list of related stuff]})


I just need it more often to find those things and only once to read it backwards, out of the map


You mean {v "this_is_backend_string_for_multiple_things"} kind of destructuring?


no, what you wrote is what I need, I just acknowledge that it's weird


probably with practice and experience I would structure my data differently so that it's easy to read it in both directions


And then there’s

dev=> (defn foo [{v "foo" w "bar"}] (str "v=" v ", w=" w))
dev=> (foo {"foo" 42 "bar" :thirteen})
"v=42, w=:thirteen"
just in case anyone wants to destructure on string keys and rename 🙂


btw, this, what just happened, is almost impossible to do in any other language that's commonly used


if I get some nested object and want to read it both ways, I better store it both ways


if you know what the string key is, you can also use :strs (analogous to :keys)


I found that in the docs, but there is a list of these and only a number of them are present at a time, not all of them, so it makes more sense to iterate over the maps and read the keys than to filter the maps out based on what keys I can find in nested maps


definitely will use it elsewhere though


Generally, if you provide an input and desired output example you’ll get to the answer you’re looking for much quickly


dunno, I got the perfect answer in 17 minutes, that's beyond any reasonable expectation imho 😄

😄 1
❤️ 1

anyone wants quicker solutions, probably have some other issues too 🙂

Alexander Kaiser21:04:37

@roelof not elegant but maybe a starting point:

    [spare-counter 0
     strike-counter 0
     scores '(6 4  0 0 10 4 3 0 0 0 0 0 0 8 9)]

  (let [round-result
         (= 10 (first scores))
            (= 10 (second scores))
            (<= 10 (reduce + (take 2 scores)))

        (or (and (= round-result :spare-on-first)
                 (< (count scores) 1)
            (< (count scores) 2)
      {:spare-counter spare-counter
       :strike-counter strike-counter}
      (recur ({:spare-on-first (inc spare-counter)
               :spare-on-second (inc spare-counter)
               :strike spare-counter
               nil spare-counter}
             ({:spare-on-first strike-counter
               :spare-on-second strike-counter
               :strike (inc strike-counter)
               nil strike-counter}
             ({:spare-on-first (rest scores)
               :spare-on-second (nthrest scores 2)
               :strike (nthrest scores 2)
               nil (nthrest scores 2)

Alexander Kaiser21:04:42

a bit cleaner

(defn frame-length[scores]
      [frames []
       scores scores]

         (if (= (first scores) 10)
         new-scores (nthrest scores round-length)
         new-frames (conj frames (take round-length scores))

          (= (count new-scores) 0)

(def scores '(6 4  0 0 10 4 3 0 0 0 0 0 0 8 9 10))

(frame-length scores)
(count (filter #(contains? (set %) 10) (frame-length scores)))
(count (filter (fn[f] (and (<= 10 (reduce + f))
                           (not (contains? (set f) 10))))
               (frame-length scores)))


@roelofw I gave it a shot. Since there is quite a bit of state to care about, I modeled the current state as a map, and put all the rules in a reducing function called roll with arity [state throw] => next-state. The function to get the final state then becomes simply:

(defn game [throws]
  (reduce roll empty-state throws))
A nice thing about this approach is that I can use reductions to get a vector of all the states, which makes it easy to debug. This is my attempt:

Lucas Garcia22:04:20

Hello y'all! I just started learning clojure and I found some resources about the language, but I was looking for something more focused on web development. Do you have any recommendations? Thanks in advance!


Hi @lucas.garcia Have you taken a look at Dmitri Sotnikov’s book on Web Development with Clojure/Script?


There’s a 3rd in beta at the moment I believe.


It’s not exactly beginner material, but it will give you an idea of the scope of web development using Clojure on the backend and ClojureScript on the frontend.


If you just want to see a beginner-level example of a (server-side) Clojure web app, I generally point beginners at — and there’s a version using different libraries linked from the README.


It also often helps us point people at appropriate material if they give us a bit of information about their background (amount of programming overall, what languages/tech they’ve used, etc).