Fork me on GitHub
#beginners
<
2019-05-06
>
tabidots05:05:58

Is there any way to use type hints to improve performance on JVM arrays of BigIntegers? Looks like I can use ^BigInteger when asetting single values, but there seems to be no plural equivalent to ^longs, ^doubles, etc. for agetting values from BigInteger arrays.

tabidots05:05:12

Or perhaps I should use an array of byte-arrays?

tabidots09:05:59

Why is

(merge-with bit-and {1 1} {5 0})
returning
{1 1, 5 0}
? It should return {1 0, 5 0}, because neither map shares a common value for a given key. I have tried writing a function to make bit-and consider the case where one argument is nil, but it does not seem to make a difference. What is happening here?

lennart.buit09:05:42

I think you are misunderstanding merge-with, it merges the maps based on keys, since there is no conflict in the keys, there is no need to “merge” the conflicting values with the bit-and function.

tabidots09:05:22

Gotcha. yeah, I noticed my print statement was not being called for non-shared keys.

tabidots09:05:06

Thanks! That was the last bug in my program. I ended up writing this function instead, which makes use of the useful library (https://github.com/clj-commons/useful). Not sure if there’s a more ideal way to do this with just the standard library.

(defn common-factors
  [m1 m2]
  (->> (merge-with + m1 m2)
       (filter-keys-by-val #(= 2 %))))
All input values will be 1 or 0.

qythium11:05:10

I'd probably just do a single pass reduce-kv over one of the maps.

qythium11:05:18

It's not so clear what the inputs to your function are supposed to be, the prime factorizations of two numbers?

qythium11:05:58

eg.

(defn common-factors
  [m1 m2]
  (reduce-kv
    (fn [acc k v1]
      (if-let [v2 (m2 k)]
        (assoc acc k (min v1 v2))
        acc))
    {} m1))

tabidots12:05:51

Yes, the inputs will be in the form {2 0, 3 1, 5 0…}

tabidots12:05:37

The reduce-kv makes a lot of sense! Was definitely overthinking it. Although for my particular case, I just need to conj the ks to a []. Thanks :+1::skin-tone-2:

tabidots12:05:26

Ah but wait, what if m2 has keys that are not present in m1?

tabidots12:05:57

The keys in the two maps may not be identical.

qythium12:05:53

yeah, I assumed that leaving the key out carried the same information as setting its value to zero

qythium12:05:26

you would filter it out anyway in your original solution

tabidots13:05:58

oh, right of course… haha Reading this over again it was obvious. Of course if any key is present in m2 but not in m1 it doesn’t matter

twaima13:05:34

it there an easy way to convert a map to XML in Clojure?

mario.cordova.86217:05:14

If run lein with-profile clj,dev classpath I see the dependencies that I should have access to. But when I run lein with-profile clj,dev exec test/main/clojure/path/to/myfile.clj I get Could not locate cheshire on classpath

mario.cordova.86217:05:37

Even though I clearly see cheshire on the classpath... I am stumped

mario.cordova.86217:05:26

My project.clj has :profiles {:dev [:project/dev :profiles/dev], :profiles/dev {} :project/dev {:dependencies [...]} :clj {:dependencies [[cheshire "5.5.0"]]}}

hiredman17:05:06

"Could not locate cheshire on classpath" is not an exception that clojure throws

hiredman17:05:36

so you have some other code somewhere that throws that exception for some reason

hiredman17:05:19

the big clue there is to clojure there is no such thing as "cheshire" just a bunch of namespaces that start with "cheshire.*" so if the language itself was reporting an error it wouldn't refer to "cheshire" but it would refer to either a file name or a namespace name

mario.cordova.86217:05:25

hmm another thing is that it doesn't matter what I use in the require... I can use any lib and it will throw the exact same message

hiredman17:05:30

right because it isn't the require that is causing it

hiredman17:05:40

it is some other code somewhere

hiredman17:05:50

what does the stacktrace say?

mario.cordova.86217:05:25

What do you mean that its some code somewhere that is causing it? The stacktrace just says that

Exception in thread "main" java.io.FileNotFoundException: Could not locate cheshire__init.class or cheshire.clj on classpath., compiling:(com/foo/bar/automated_tests.clj:1:1)

hiredman17:05:19

that is not the error message you reported

hiredman17:05:29

the error there is clear

hiredman17:05:49

automated_tests is requiring a namespace named cheshire which doesn't exist

mario.cordova.86217:05:55

Ah sorry for the confusion

hiredman17:05:07

if you look at the cheshire project the main namespace is named cheshire.core

mario.cordova.86217:05:27

It can be anything. Its not just exclusive to cheshire

hiredman17:05:27

what does the error say for other things?

mario.cordova.86217:05:29

Exception in thread "main" java.io.FileNotFoundException: Could not locate etaoin__init.class or etaoin.clj on classpath., compiling:(com/foo/bar/automated_tests.clj:1:1)
	at clojure.lang.Compiler.load(Compiler.java:7391)
	at clojure.lang.RT.loadResourceScript(RT.java:372)
	at clojure.lang.RT.loadResourceScript(RT.java:363)
	at clojure.lang.RT.load(RT.java:453)
	at clojure.lang.RT.load(RT.java:419)
	at clojure.core$load$fn__5677.invoke(core.clj:5893)
	at clojure.core$load.invokeStatic(core.clj:5892)
	at clojure.core$load.doInvoke(core.clj:5876)
	at clojure.lang.RestFn.invoke(RestFn.java:408)
	at clojure.core$load_one.invokeStatic(core.clj:5697)
	at clojure.core$load_one.invoke(core.clj:5692)
	at clojure.core$load_lib$fn__5626.invoke(core.clj:5737)
	at clojure.core$load_lib.invokeStatic(core.clj:5736)
	at clojure.core$load_lib.doInvoke(core.clj:5717)
	at clojure.lang.RestFn.applyTo(RestFn.java:142)
	at clojure.core$apply.invokeStatic(core.clj:648)
	at clojure.core$load_libs.invokeStatic(core.clj:5774)
	at clojure.core$load_libs.doInvoke(core.clj:5758)
	at clojure.lang.RestFn.applyTo(RestFn.java:137)
	at clojure.core$apply.invokeStatic(core.clj:648)
	at clojure.core$require.invokeStatic(core.clj:5796)
	at clojure.core$require.doInvoke(core.clj:5796)
	at clojure.lang.RestFn.applyTo(RestFn.java:137)
	at clojure.core$apply.invokeStatic(core.clj:648)
	at clojure.core$apply.invoke(core.clj:641)
	at user$eval6299.invokeStatic(form-init125445217099502316.clj:1)
	at user$eval6299.invoke(form-init125445217099502316.clj:1)
	at clojure.lang.Compiler.eval(Compiler.java:6927)
	at clojure.lang.Compiler.eval(Compiler.java:6917)
	at clojure.lang.Compiler.load(Compiler.java:7379)
	at clojure.lang.Compiler.loadFile(Compiler.java:7317)
	at clojure.main$load_script.invokeStatic(main.clj:275)
	at clojure.main$init_opt.invokeStatic(main.clj:277)
	at clojure.main$init_opt.invoke(main.clj:277)
	at clojure.main$initialize.invokeStatic(main.clj:308)
	at clojure.main$null_opt.invokeStatic(main.clj:342)
	at clojure.main$null_opt.invoke(main.clj:339)
	at clojure.main$main.invokeStatic(main.clj:421)
	at clojure.main$main.doInvoke(main.clj:384)
	at clojure.lang.RestFn.invoke(RestFn.java:421)
	at clojure.lang.Var.invoke(Var.java:383)
	at clojure.lang.AFn.applyToHelper(AFn.java:156)
	at clojure.lang.Var.applyTo(Var.java:700)
	at clojure.main.main(main.java:37)
Caused by: java.io.FileNotFoundException: Could not locate etaoin__init.class or etaoin.clj on classpath.
	at clojure.lang.RT.load(RT.java:456)
	at clojure.lang.RT.load(RT.java:419)
	at clojure.core$load$fn__5677.invoke(core.clj:5893)
	at clojure.core$load.invokeStatic(core.clj:5892)
	at clojure.core$load.doInvoke(core.clj:5876)
	at clojure.lang.RestFn.invoke(RestFn.java:408)
	at clojure.core$load_one.invokeStatic(core.clj:5697)
	at clojure.core$load_one.invoke(core.clj:5692)
	at clojure.core$load_lib$fn__5626.invoke(core.clj:5737)
	at clojure.core$load_lib.invokeStatic(core.clj:5736)
	at clojure.core$load_lib.doInvoke(core.clj:5717)
	at clojure.lang.RestFn.applyTo(RestFn.java:142)
	at clojure.core$apply.invokeStatic(core.clj:648)
	at clojure.core$load_libs.invokeStatic(core.clj:5774)
	at clojure.core$load_libs.doInvoke(core.clj:5758)
	at clojure.lang.RestFn.applyTo(RestFn.java:137)
	at clojure.core$apply.invokeStatic(core.clj:648)
	at clojure.core$require.invokeStatic(core.clj:5796)
	at clojure.core$require.doInvoke(core.clj:5796)
	at clojure.lang.RestFn.invoke(RestFn.java:408)
	at com.foo.bar.automated_tests$eval6404$loading__5569__auto____6405.invoke(automated_tests.clj:1)
	at com.foo.bar.automated_tests$eval6404.invokeStatic(automated_tests.clj:1)
	at com.foo.bar.automated_tests$eval6404.invoke(automated_tests.clj:1)
	at clojure.lang.Compiler.eval(Compiler.java:6927)
	at clojure.lang.Compiler.eval(Compiler.java:6916)
	at clojure.lang.Compiler.load(Compiler.java:7379)
	...
Tests failed.
Error encountered performing task 'test' with profile(s): 'clj,dev,dev'
Tests failed.

mario.cordova.86217:05:34

I can't spot anything else

hiredman17:05:55

etaoin as a namespace also doesn't exist as far as I can tell

hiredman17:05:09

it seems like you are confusing the maven coordinates for namespace names

mario.cordova.86218:05:49

It worked.. I could have sworn I tried all possible combinations. :man-facepalming:

hiredman17:05:44

the names you put in project.clj are maven coordinates, the names you put in require are clojure namespace names

hiredman17:05:57

they don't have anything to do with each other

hiredman17:05:56

just searching for etaoin in github brings up https://github.com/igrishaev/etaoin which has namespaces like etaoin.api but no etaoin

hiredman17:05:26

the readme for the project shows namespaces like:

(use 'etaoin.api)
(require '[etaoin.keys :as k])

hiredman17:05:13

(technically the names you put in project.clj are part of the maven coordinate, the other part is the version)

luizedusol17:05:44

Can someone tell me how to pass parameter to ring? After reading this https://github.com/weavejester/lein-ring I've already tried: lein ring server {:port 8881 :nrepl {:start? true :port 8801}} and lein ring server 8881 --nrepl {:start? true :port 8801} How am I supposed to pass such parameters?

matt.kipps18:05:57

@luizedusol do you have a project.clj ?

matt.kipps18:05:18

great, you have a :ring config already. additional options can be added to the map which currently specifies your handler

luizedusol18:05:52

thanks a lot @matt.kipps

jaihindhreddy18:05:08

What's the difference b/w (fn [] ...) and (fn foo [] ...).

ghadi18:05:15

The latter allows you to use or pass the "this" object (the function instance itself)

lilactown18:05:33

also better stack traces

borkdude18:05:37

@jaihindhreddy it also allows you to call a different arity than with recur which targets only the arity of the current body

borkdude18:05:59

(fn foo ([] (foo 1)) ([x] x))