Fork me on GitHub

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 &quot;&quot;, :lesson-parts [&quot;Lesson 0 Objectives&quot; &quot;Particles"


(defn html-lesson-array-bits [section]
 "{% 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: に で を" 


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?


I was making it very hard


selmer can do keys just use dot indexing



Marcio Arakaki02:11:15

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.


what does collection.Count(x => x > 2) do? i'm not familiar with that from linq


ah, i wasn't aware that they had an extension method taking a predicate


(defn count-matching [pred coll]
  (count (filter pred coll)))
and then you have a single one liner just like linq.

👍 8

(followup beginner Q) is this the kind of fn that would be well suited for a macro?


are you asking me or posing this to the original questioner?


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?

Marcio Arakaki03:11:46

@andy.fingerhut thanks, fixed the code for future reference. @dpsutton interesting... thanks ! 🙂

Marcio Arakaki04:11:22

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 ?


@marcio.akk You can wrap the call in (fn [_ _ pc] (func-c pc))

👍 4
Guillaume De Lazzer07:11:27

Hi, I am having trouble working with big numbers (bigint/long), I am working through the 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: ->

(def digits (str 21897142587612075) )
(def exponent (int (count digits)))
(reduce + (map (fn [digit]
                 (.pow (BigInteger/valueOf (Integer/parseInt (str digit)))


The difference you're getting is likely due to rounding errors when using Math/pow which uses doubles

Guillaume De Lazzer07:11:49

i see, that makes sense


I edited the snippet above as it was slightly incorrect (using your a instead of my digits)

Guillaume De Lazzer08:11:36

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.

👍 4
Joaquin Iglesias12:11:31

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?


For web, there are almost too many options! You can't really go wrong with luminus, though it's worth knowing that there are other libraries available.

👍 4

Yeah, Luminus is a great piece, especially for the concise and very spot-on documentation.

👍 4