This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-10-28
Channels
- # beginners (45)
- # boot (4)
- # cider (5)
- # cljs-dev (8)
- # cljsrn (4)
- # clojure (38)
- # clojure-conj (7)
- # clojure-dev (4)
- # clojure-russia (3)
- # clojure-spec (20)
- # clojure-uk (3)
- # clojurescript (28)
- # core-logic (29)
- # figwheel-main (10)
- # fulcro (2)
- # onyx (1)
- # other-languages (5)
- # parinfer (3)
- # pathom (98)
- # portkey (1)
- # reagent (15)
- # reitit (9)
- # shadow-cljs (22)
- # spacemacs (10)
- # sql (22)
- # tools-deps (1)
@jesse.wertheim aha! that makes sense. thanks!
Hello. I have this snippet:
(let [blocks (repeatedly #(parse-block file))]
(->> blocks
(take-while (complement :header/last?))
vec))
That repeatedly calls a function which returns a map and produces a vector of maps until some key in the returned map is logical true. My question is what would be an idiomatic way to extend this, such that the resulting vector also includes the last item - the one where the key's value was true, but no more after that?Sounds like you want take-until
https://dev.clojure.org/jira/browse/CLJ-1451
Thanks, although I was looking for a way to do it with Clojure 1.9.0
Yeah, you could copy the fn from the patch and use it
(defn take-until [pred coll]
(lazy-seq
(when-let [s (seq coll)]
(if (pred (first s))
(cons (first s) nil)
(cons (first s)
(take-until pred
(rest s)))))))
@mfiano since it's eager you could also just do (reduce #(if (:name %2) (reduced (conj %1 %2)) (conj %1 %2)) [] blocks)
I like the take-until
idea better but I would probably use the full definition so you can do (into [] (take-until :name) blocks)
instead of making a lazy-sec to turn into a vec
what are some good source code for beginners of clojure to read and understand clojure programming idioms?
@kingjuliyen often those of weavejester are mentioned
Thanks borkdude
What is a nice way to filter a vector of maps, such that a specific key of each map can only one of a predefined set of values, removing the maps that have different values?
cljs.user=> (filter
#_=> (comp #{:allowed-1 :allowed-2} :my-key)
#_=> [{:a 1}
#_=> {:my-key :allowed-1 :other-key 2}
#_=> {:my-key :not-allowed :other-key 3}])
({:my-key :allowed-1, :other-key 2})
@mfikes Thank you. That is very nice
Just be careful if the set of values allowed includes nil
or false
, in which case the above wouldn't be sufficient.
Nope, always keywords. I see how that wouldn't work though. Thanks for the tips!
Hi people, I am playing around with the ‘apply’ function but there is a behaviour I do not quite understand. In the following snippet, why is the last format specifier %s truncating the word ?
all the args before the last can be provided normally though
user=> (apply format "dear %s, thanks a lot for sending me the %s" "Tom" "Gift" [])
"dear Tom, thanks a lot for sending me the Gift"
there's not much reason to use apply in a situation like that
but this does come up:
user=> ,(apply + 1 [2 3])
6
the reason it got weird in your example is it turns out strings are also sequences 😉 so in your example it passes in each character, G
in to format
like:
(format "dear %s, thanks a lot for sending me the %s" "Tom" "G" "i" "f" "t")
it's a special form, defined (i think) here: https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Compiler.java#L2163 i like reading clojure's source, but i i can't read java very well
there is no other form in clojure that handles exceptions, so if you removed try and tried to write a macro that does what try does, there is nothing for it to expand to
so a try
macro would have to take a form like (try ... (catch ...) (finally ...))
and transform it in to some other sytnax
like when
is a macro, it transforms (when X Y)
in to (if X Y)
(technically a little more complicated because when introduces a do, but whatever)
ok thanks. as regards handling exceptions as the JVM throws them--agreed. can a clojure macro try to eval an expression, and if that expression bubbles up an error value of some sort, short-circuit the evaluation of that expression, and eval a (also a macro) catch expression?
a macro can contain a try/catch
yeah! that's part of why i'm posting here. i was looking at try-let and try+, and saw that they're wrappers around try
that's different, that's generating a try/catch as the result of the macro
macros take a source form and return a replacement source form