Fork me on GitHub
#beginners
<
2019-11-08
>
sova-soars-the-sora02:11:56

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...

sova-soars-the-sora02:11:00

I get something like "VALUE is {:lesson-number 0, :lesson-name &quot;&quot;, :lesson-parts [&quot;Lesson 0 Objectives&quot; &quot;Particles"

sova-soars-the-sora02:11:17

(defn html-lesson-array-bits [section]
 (sp/render 
 "{% for lesson in lessons %}
 		VALUE is {{lesson}}
 	{% endfor %}" 
  {:lessons @jc-lessons-array-s1}))

sova-soars-the-sora02:11:37

jc-lessons array looks like

sova-soars-the-sora02:11:24

(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: に で を" 

sova-soars-the-sora02:11:40

i tried adding selmer filters like

sova-soars-the-sora02:11:47

(selmer.filters/add-filter! :key key)
(selmer.filters/add-filter! :val val)

sova-soars-the-sora02:11:09

and trying to invoke by

VALUE is {{lesson|val}}

sova-soars-the-sora02:11:25

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

sova-soars-the-sora02:11:15

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?

sova-soars-the-sora02:11:25

is a for loop the best way?

sova-soars-the-sora02:11:48

I was making it very hard

sova-soars-the-sora02:11:56

selmer can do keys just use dot indexing

sova-soars-the-sora02:11:01

{{lesson.lesson-number}}

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))

andy.fingerhut02:11:21

That isn't the only way, but it is one of the most concise ways to write code that does that.

andy.fingerhut02:11:06

I mean, you'd want to put the collection as the second arg to the filter call, of course, and balance the parens.

dpsutton02:11:40

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

dpsutton02:11:30

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

dpsutton02:11:06

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

👍 8
chepprey13:11:19

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

dpsutton13:11:21

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

chepprey14:11:03

Anyone who has a valuable answer is fine with me. Please pardon my Slack manners if I'm not doing this right 🙂

dpsutton14:11:33

gotcha. just didn't want to answer you if you knew the answer and wanted to ask the original poster

dpsutton14:11:00

but the answer is that what benefit would a macro bring? this is pretty concise. a macro would just introduce problems

chepprey15:11:59

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,

chepprey15:11:52

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 ?

seancorfield04:11:54

@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 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?

jumar07:11:02

@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))

jumar07:11:30

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

jumar08:11:56

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 🙂

jaihindhreddy10:11:32

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?

mloughlin14:11:33

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
agigao15:11:11

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

👍 4