Fork me on GitHub
#beginners
<
2021-12-04
>
paulocuneo02:12:01

hi howdy? when a hashmap is coersed into a seq does it preserve the insertion order of the hashmap?

emccue02:12:52

(for [i (range 10)]
  (seq (reduce (fn [m v] (assoc m v v)) {} (range i))))
=>
(nil
 ([0 0])
 ([0 0] [1 1])
 ([0 0] [1 1] [2 2])
 ([0 0] [1 1] [2 2] [3 3])
 ([0 0] [1 1] [2 2] [3 3] [4 4])
 ([0 0] [1 1] [2 2] [3 3] [4 4] [5 5])
 ([0 0] [1 1] [2 2] [3 3] [4 4] [5 5] [6 6])
 ([0 0] [1 1] [2 2] [3 3] [4 4] [5 5] [6 6] [7 7])
 ([0 0] [7 7] [1 1] [4 4] [6 6] [3 3] [2 2] [5 5] [8 8]))

Michael W02:12:03

After 8 entries it is converted to an unordered map, so you can't rely on the default hashmap if you want an ordered map.

paulocuneo02:12:05

thanks you! 🙏

manas_marthi05:12:55

Hi All, Let's say I need two webapps that in Production will be deployed in two JVMs. In lower environments, can I convert them into osgi bundles nd deploy in single JVM

hiredman06:12:47

I think it's more common to just load the two apps in a single jvm, and not bother with any kind of isolation stuff

hiredman06:12:55

monorepos with managed common sets of dependencies are all the rage, that changes the trade offs on the heavy duty isolation machinery a little

hiredman06:12:53

Our billing system at work, in production has sort of 3 different services, each running on their own jvms, but in dev I usually load them up in one, no isolation between them

hiredman07:12:49

I know people have done osgi stuff with clojure, but I've only seen it come up in mailing list threads, and not for a long time

roelof11:12:45

Why do I get this error message :

; form-init3986065220437913819.clj:10 recur arg for primitive local: prod is not matching primitive, had: Object, needed: long
; Auto-boxing loop arg: prod

roelof11:12:13

on this code :

(ns armstrong-numbers)

(set! *warn-on-reflection* true)

(defn exp [x n]
  (loop [x x, n n, prod 1]
    (cond (zero? n) prod
          (= n 1) (* x prod)
          :else
          (recur (* x x)
                 (quot n 2)
                 (if (even? n) prod (* prod x))))))

(defn armstrong? [num]
  (let [digits (map #(Character/digit ^char % 10) (str num))
       count-numbers (count digits) ]
  (->>  digits
       (mapv #(exp % count-numbers ))
        (reduce +')
        (= num))))

Ben Sless11:12:44

The compiler needs to to quite a bit of boxing in exp, try type hinting the arguments and return values

roelof11:12:09

oke, then I have to think how I do this . For small numbers its int but for real big numbers it schould be another one such as Bigint

Ben Sless11:12:28

The solution there would be to use bigints from the get go

roelof11:12:59

oke, can I put the typehints here (defn exp [x n] ?

Ben Sless11:12:53

Yes, but if you're going to work with big ints then you won't need them

roelof11:12:21

sorry now im confused. First you said I need type hints and now I do not need them ?

Ben Sless11:12:24

If you want to avoid boxed warnings you need to either do unboxed maths or explicitly work with reference types, like bigints

Ben Sless11:12:53

Since you said you'll end up in big int land, then you need to work with them, not primitive maths

roelof11:12:21

oke and how do I tell the compiler

roelof11:12:37

I said I could end up in BigInt land

Ben Sless11:12:35

instead of + or * use +' and *'

Ben Sless11:12:06

And wrap the initial prod in bigint

roelof12:12:18

oke, I jhave to find out how to do that

roelof12:12:26

Thanks, the annoying message is gone with this code

(ns armstrong-numbers)

(set! *warn-on-reflection* true)

(defn exp [x n]
  (loop [x x, n n, prod (bigint 1)]
    (cond (zero? n) prod
          (= n 1) (*' x prod)
          :else
          (recur (*' x x)
                 (quot n 2)
                 (if (even? n) prod (*' prod x))))))


(defn armstrong? [num]
  (let [digits (map #(Character/digit ^char % 10) (str num))
       count-numbers (count digits) ]
  (->>  digits
       (mapv #(exp % count-numbers ))
        (reduce +')
        (= num))))

Stuart15:12:25

Why is this a compiler error?

(apply or '(false false false false false))
IT says can't take value of a macro or, but I'm not sure what this means.

wilkerlucio15:12:14

apply only works with functions, macros look like functions buy they are something else, they run during the compilation time of code, to generate code

wilkerlucio15:12:31

this is a good place to start reading about them: https://clojure.org/reference/macros

Stuart15:12:16

Thank you, that makes sense!

wilkerlucio15:12:56

but for your specific problem, if your intention is the get the first truthy value on a list, you can do this: (first (keep identity [false false false true false]))

Stuart16:12:31

What I was trying to do was to see if they are all true

wilkerlucio16:12:56

them you can use: (every? identity [false true false true])

Stuart16:12:09

thanks!, that's neater then the reduce i wrote 🙂

🙂 1
wilkerlucio16:12:20

or, if you look for true specifically, you can do: (every? true? [...])

Stuart16:12:02

Looks like it could be good for discovering core functions

wilkerlucio16:12:10

yeah, its a jorney to learn all the core functions, but its very cool, from time to time you find a new function and every changes XD

wilkerlucio16:12:34

I learned a lot from a series called "Function of The Day", from Tim Baldridge youtube channel https://www.youtube.com/c/ClojureProgrammingTutorials/videos

👍 1
thom17:12:47

Worth pointing out that the reason for or to be a macro is that it allows you to avoid evaluating all the possible arguments. So if you have code like (or (be-friends) (tolerate-differences) (fisticuffs) (launch-nukes)), you can stop on the first truthy value. It could easily be a function if not for that concern.

👍 3
seralbdev19:12:23

Hi folks! Where should I request a fix in clojure web?file:///home/berto/Pictures/typo-in-tools_build-guide.png

seralbdev19:12:56

There is typo here, it should be :main 'my.lib.main

seancorfield20:12:14

("ask" is the best place to start overall -- but this page talks in more detail about contributing to parts of Clojure: https://clojure.org/community/contributing_site and for http://clojure.org specifically, you can go straight to GitHub and create an issue)

seralbdev20:12:13

umm I have already created a question under clojure / docs

seancorfield21:12:02

That's fine. Starting with "ask" is a good habit to get into, even if there are "shortcuts" for some specific things.

seancorfield21:12:27

Sometimes a post on "ask" will trigger a good discussion about other, related things that might need to be addressed.

seralbdev23:12:12

Hi Sean, good to know...I am not very much used to the community structure and rules...thx for your guidance & help