This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-04-21
Channels
- # announcements (12)
- # aws (1)
- # babashka (82)
- # beginners (89)
- # calva (8)
- # cider (20)
- # clj-kondo (1)
- # clojars (9)
- # clojure (120)
- # clojure-australia (3)
- # clojure-europe (14)
- # clojure-france (24)
- # clojure-germany (3)
- # clojure-italy (16)
- # clojure-nl (1)
- # clojure-spec (3)
- # clojure-uk (22)
- # clojurescript (26)
- # cursive (38)
- # datascript (1)
- # emacs (10)
- # events (1)
- # helix (7)
- # jackdaw (5)
- # jobs-discuss (37)
- # lambdaisland (10)
- # malli (20)
- # meander (2)
- # off-topic (15)
- # pathom (42)
- # quil (1)
- # re-frame (38)
- # react (2)
- # reitit (2)
- # reveal (9)
- # rewrite-clj (3)
- # ring (5)
- # shadow-cljs (84)
- # spacemacs (1)
- # tools-deps (23)
- # vim (8)
- # vscode (1)
this will be equivalent of second
and unlike second
it will fail if collection is empty
consider this #(nth % 0 nil)
Hello. I'm having an issue using slurp. I was following this article to get some experience using it: https://practicalli.github.io/blog/posts/consuming-apis-with-clojure/ When I run the command:
(slurp "")
The result looks like this:
9vTƌE,QZ5,V.&?7WIrrP36틈]H<,ASulbdx[L!ż\tzI:$\r<?:r#F@kk-ד=\tfcZw.f\nNn3{O{\b2I 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 https://github.com/dakrone/clj-http 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
Thanks!
Good day. Where can I get a tutorial or guide on how to use the repl?
I think youtube might be the best place: https://www.youtube.com/watch?v=gIoadGfm5T8
its interactive so it is easier to demonstrate with a video than a guide
and there is the official clojure guide: https://clojure.org/guides/repl/introduction
Thank you
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 logicOn 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)
or maybe you need to calculate the bonuses over the top of that - still doable with reduce
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
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
maybe pre-process the list to create a list of numbers you can simply add. like a raw-score that you derive from the input score sheet.
you can split the input vector on tens, for example
Maybe, I do not see totally where you are heading to @U3ES97LAC
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 https://groups.google.com/g/clojure-dev/c/NaAuBz6SpkY 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?
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]
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?
Ah, OK. Will look it up. Thanks!
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)
Ah… yeah, that’s a bit subtle…
No, that makes perfect sense now you’ve explained it.
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)]
(mapv
(fn[album]
(->>
;; 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)
)
)
albums
))
)
(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?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
dev=> (foo {:a 1})
"key=:a, val=1"
dev=> (foo {:a 1 :b 2})
"key=:a, val=1"
dev=>
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?
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
dev=> (foo {"foo" 42 "bar" :thirteen})
"v=42, w=:thirteen"
dev=>
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 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
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 😄
@roelof not elegant but maybe a starting point:
(loop
[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
(if
(= 10 (first scores))
:spare-on-first
(cond
(= 10 (second scores))
:spare-on-second
(<= 10 (reduce + (take 2 scores)))
:strike
)
)]
(if
(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}
round-result)
({:spare-on-first strike-counter
:spare-on-second strike-counter
:strike (inc strike-counter)
nil strike-counter}
round-result)
({:spare-on-first (rest scores)
:spare-on-second (nthrest scores 2)
:strike (nthrest scores 2)
nil (nthrest scores 2)
}
round-result)
)))
)
a bit cleaner
(defn frame-length[scores]
(loop
[frames []
scores scores]
(let
[round-length
(if (= (first scores) 10)
1
2)
new-scores (nthrest scores round-length)
new-frames (conj frames (take round-length scores))
]
(if
(= (count new-scores) 0)
new-frames
(recur
new-frames
new-scores
)))
)
)
(def scores '(6 4 0 0 10 4 3 0 0 0 0 0 0 8 9 10))
(frame-length scores)
;;spares
(count (filter #(contains? (set %) 10) (frame-length scores)))
;;strikes
(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: https://gist.github.com/madstap/d9eb24395841f5a2ee2292ccbd0baec4Hello 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?
Hi, @U04V70XH6! Thanks for response
I'm checking Dmitri Sotnikov's book right now
I don't know much about web applications, but I have experience with mobile application with flutter and kotlin.
I wanted something that could look for when I face any problem.
There’s a 3rd in beta at the moment I believe.
https://pragprog.com/titles/dswdcloj3/web-development-with-clojure-third-edition/
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 https://github.com/seancorfield/usermanager-example — 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).