This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-11-08
Channels
- # adventofcode (9)
- # announcements (5)
- # babashka (3)
- # beginners (46)
- # calva (21)
- # cider (15)
- # clj-kondo (20)
- # cljs-dev (57)
- # cljsrn (10)
- # clojure (147)
- # clojure-dev (6)
- # clojure-europe (3)
- # clojure-france (2)
- # clojure-italy (8)
- # clojure-nl (18)
- # clojure-norway (5)
- # clojure-spec (16)
- # clojure-uk (63)
- # clojuredesign-podcast (7)
- # clojurescript (65)
- # clojurex (42)
- # cursive (12)
- # datomic (10)
- # fulcro (47)
- # graalvm (102)
- # graphql (2)
- # jobs (5)
- # joker (12)
- # off-topic (33)
- # pedestal (6)
- # re-frame (6)
- # reagent (8)
- # reitit (6)
- # remote-jobs (4)
- # rewrite-clj (10)
- # shadow-cljs (86)
- # sql (45)
- # testing (4)
- # tools-deps (43)
- # vim (8)
- # xtdb (3)
hi! i'm trying to template with selmer ... i'm not sure how to do a for loop on a vector of maps i have...
I get something like "VALUE is {:lesson-number 0, :lesson-name "", :lesson-parts ["Lesson 0 Objectives" "Particles"
(defn html-lesson-array-bits [section]
(sp/render
"{% for lesson in lessons %}
VALUE is {{lesson}}
{% endfor %}"
{:lessons @jc-lessons-array-s1}))
jc-lessons array looks like
(def jc-lessons-array-s1 (atom [{:lesson-number 0 :lesson-name "" :lesson-parts ["Lesson 0 Objectives" "Particles Introduction に で を が" "Intro to Katakana and Kanji" "Kanji and Creative Memory"] :lesson-kanji #{} :lesson-grammar #{"ni-time" "de-setting" "wo-directobj" "ga-subject"} :lesson-verbs #{} } {:lesson-number 1 :lesson-name "" :lesson-parts ["Lesson 1 Objectives" "Particles: に で を"
(vector of maps)
i tried adding selmer filters like
(selmer.filters/add-filter! :key key)
(selmer.filters/add-filter! :val val)
and trying to invoke by
VALUE is {{lesson|val}}
but the error is On filter body 'lesson|val' and filter 'val' this error occurred:clojure.lang.PersistentArrayMap cannot be cast to java.util.Map$Entry
just invoking {{lesson}} in the for statement brings back the whole map, how can i grab one particular key-val out of the map with selmer?
is a for loop the best way?
Oh I see
I was making it very hard
selmer can do keys just use dot indexing
{{lesson.lesson-number}}
is there anything in clojure like collection.Count(x => x > 2) ? or the only way to do it would be to
(count (filter #(> % 2) col))
That isn't the only way, but it is one of the most concise ways to write code that does that.
I mean, you'd want to put the collection as the second arg to the filter
call, of course, and balance the parens.
(defn count-matching [pred coll]
(count (filter pred coll)))
and then you have a single one liner just like linq.Anyone who has a valuable answer is fine with me. Please pardon my Slack manners if I'm not doing this right 🙂
gotcha. just didn't want to answer you if you knew the answer and wanted to ask the original poster
but the answer is that what benefit would a macro bring? this is pretty concise. a macro would just introduce problems
Problems of "it wouldn't work right in certain edge cases"? I fully agree that the fn is pretty concise. I've also read that, in Clojure (perhaps more than other Lisps), macros ought be used sparingly. OTOH,
I've read that much of Clojure's core library is implemented as macros, building on top of some of the most deep & basic core fn's. So, if hypothetically this count-matching
fn were to be added to core clojure, would it likely be done as a macro?
... answering my own hypotheticals: https://purelyfunctional.tv/mini-guide/when-to-use-a-macro-in-clojure/ https://www.reddit.com/r/Clojure/comments/97cprw/i_do_not_use_macros_am_i_doing_it_wrong/
@andy.fingerhut thanks, fixed the code for future reference. @dpsutton interesting... thanks ! 🙂
I'm lost 😞. I need to make several validations and add the result (result is a string) of each validation to a collection. What I did:
((juxt #(func a) #(func b) #(*func c*)) pa pb *pc*)
I had the feeling that this was not the best solution, now I know for sure.
The func c only uses the parameter c (pc), so it only has 1 parameter and because of the *(juxt) I'm trying to pass 3 (pa, pb and pc) resulting in a "Wrong number of args (3) passed to..." exception.
What can I do here ?Hi, I am having trouble working with big numbers (bigint/long), I am working through the http://exercism.io exercices and I am trying to do the armstrong problem Here are 2 examples of how I am trying to solve it, but I never get the right results, and I don’t understand why:
(def a (str/split (str 21897142587612075) #""))
(long (reduce +' (map #(Math/pow (Integer/parseInt %) (count a)) a)))
=> 21897142587612072
(reduce + (map #(long (Math/pow (Integer/parseInt %) (count a))) a))
=> 21897142587612074
The expected result should be 21897142587612075. Would someone be able to explain me what am I doing wrong and why I get 2 different results?@UPUMAJH0D my version => using BigInteger.pow: https://stackoverflow.com/questions/8071363/calculating-powers-of-integers -> https://docs.oracle.com/javase/6/docs/api/java/math/BigInteger.html#pow%28int%29
(def digits (str 21897142587612075) )
(def exponent (int (count digits)))
(reduce + (map (fn [digit]
(.pow (BigInteger/valueOf (Integer/parseInt (str digit)))
exponent))
digits))
The difference you're getting is likely due to rounding errors when using Math/pow which uses doubles
i see, that makes sense
thank you 🙂
I edited the snippet above as it was slightly incorrect (using your a
instead of my digits
)
yeah, don’t worry, I had figured 🙂
You also might wanna use Long/parseLong
instead of Integer/parseInt
. There are unsigned versions as well, Long/parseUnsignedLong
.
Hello Clojurians! What is your favorite clojure library / framework to write microservices? A quick google search pointed me to luminus, but are there other options? I am fairly inexperience with any web programming, I have only used python's flask?