This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-03-04
Channels
- # adventofcode (6)
- # announcements (1)
- # aws (18)
- # beginners (104)
- # boot (11)
- # cljsrn (31)
- # clojure (49)
- # clojure-dev (16)
- # clojure-europe (2)
- # clojure-greece (9)
- # clojure-houston (1)
- # clojure-italy (12)
- # clojure-nl (3)
- # clojure-spec (46)
- # clojure-uk (148)
- # clojurescript (12)
- # community-development (13)
- # core-async (7)
- # cursive (35)
- # data-science (13)
- # datomic (70)
- # events (1)
- # fulcro (22)
- # hyperfiddle (1)
- # jobs-discuss (10)
- # kaocha (3)
- # off-topic (7)
- # om (2)
- # other-languages (32)
- # parinfer (1)
- # portkey (4)
- # re-frame (3)
- # reitit (12)
- # shadow-cljs (49)
- # spacemacs (1)
- # specter (6)
- # sql (5)
- # tools-deps (58)
Hey all, new to clojure and enjoying it so far. One question I have is, is there a quicker way to traverse a list of lists to find the x,y
coordinate than nested for
statements? I'm doing that now and I wonder if it's inefficient
these kinds of questions come up a lot. I don't see for
working very well here at all though, frankly. Also, if you control the data, and you need to reach into it like this its advisable to use a data structure that gives you the addressability you like. Think maps or vectors if possible.
I did more reading, and clojure has a library for cartesian products, which is perfect here
I'll just need to flatten it when I save it with a java function. And sorry, I did as much reading as I could and followed clojure for the brave and true all the way through
do you need a particular element at position x,y or the whole sequence. I'm not sure what you're trying to accomplish
I'm trying to draw a line (change 0 to java.awt.Color.black
) over an image read, and then save that back again
I'm bad at math, cartesian product is probably not what I want. I'm trying to set plane[1,1] = 1
as I would write it in python, but I'm still wrapping my head around how this all works
If this were python, it would be a 2d array, but because it's not, I don't really know what to make it. It needs to be addressable by coordinates, and store a value, but I'm beginning to wonder if it should be {:x 0 :y 1 :value black}
https://codereview.stackexchange.com/questions/87325/starting-a-clojure-chess-engine helped a lot, so basically I was trying to use a list where I shouldn't be
Okay, yeah using a vector is what I need, clojure is really different from what I'm used to but that's one of the reasons I'm hoping to learn it
I have an issue with lein and a compojure project. If I run lein ring server-headless
everything starts fine and all, but if I do lein repl
I returns an error
:cause Could not locate ring/middleware/reload__init.class, ring/middleware/reload.clj or ring/middleware/reload.cljc on classpath.
If I have a config map which is created using cli arguments in -main
how do I expose that map to other namespaces?
If you don't want to pass the config object around in every function as an argument, maybe you can store it in a var in the main namespace using def
and set!
How would I store it in my core.clj
namespace from within the -main
function? Is there a way to set variables to a global namespace from anywhere?
(def config {})
(defn -main
[& args]
(alter-var-root #'config
(constantly {:param1 "val1"
:param2 "val2"})))
this will set the value of the var config
to the provided hashmap and it will be available to other namespaces
What would be an idiomatic way of removing duplicates from a coll of maps based on a predicate? Essentially the predicate would take two maps and return true/false based on whether the two are considered "equal"
@mbjarland a lot of helper libs have distinct-by
which is not in clojure.core
@borkdude I was hoping I was being daft and there was some cool combination of core functions to accomplish this but I'll take it : )
a less efficient version could be implemented with group-by, vals and them map + first or something
no such luck I'm afraid : ) maybe something like this?
(defn distinct-by [keys rs]
(map first (vals (group-by (apply juxt keys) rs))))
which is more or less what you were saying above...right, agree, that would make it more generic. Though this is a command line ad-hoc script which will never (hahaha...yeah...can't believe I'm saying this...that just never happens) be re-used : )....
well isn't the clojure philosophy that you grab the data you want from the maps that come in and pass whatever you don't care about along?
and maybe I was not explaining myself well either, there is a fixed set of keys and I would like to keep whatever keys come in, and I would like a way to filter out duplicates based on a sub-set of the keys. I agree that that would throw away one or more (arbitrary) duplicate items, but in this scenario I'm ok with that
if you know the unique combination of keys up front, you can also use that to not even add the maps to that input collection
e.g., are {:used-k1 x :used-k2 y :ignored-k1 b}
and {:used-k1 x :used-k2 y :ignored-k1 a}
duplicates?
true. In this particular case I thought this was an interesting problem which I would probably run into again so I figured it was worth thinking a bit about. @borkdude thanks for all the above
so the ignored keys will have arbitrary values based on runtime implementation details, was the point @borkdude was making and I think you already understood
@mrevelle yes, in this case not really runtime implementation details, but yes the same effect
ah and @mrevelle in your example above, I would like to be able to define the set of keys used for uniqueness, i.e. [:used-k1 :used-k2]
above
that would be (distinct-by (juxt :username :birth-date) ...)
if you would have distinct-by
take a function
anyway, I assumed there was some cool combination of core functions which I failed to grok. If there is - great, if not, the above already gave me a bunch of ways of solving this
as performance is not a concern here, I guess I will go with:
(defn distinct-by [uniq-fn maps]
(map first (vals (group-by uniq-fn maps))))
for now, usage as in @borkdude's example aboveI need help to understand why this generates a StackOverflowError
:
(defn drop-every [nth coll]
(loop [selected '()
candidates coll]
(if (empty? candidates)
selected
(recur (concat selected (take (dec nth) candidates)) (drop nth candidates)))))
(comment
(drop-every 3 [:a :b :c :d :e :f :g])
; => (:a :b :d :e :g)
(drop-every 3 (range 10000))
; Error: StackOverflowError clojure.lang.LazySeq.sval (LazySeq.java:40)
)
Note, I know it is an inefficient solution. đ I have written a few of them today, most much more efficient, but I donât understand why the stack blows up for this one.https://stuartsierra.com/2015/04/26/clojure-donts-concat this seems particularly relevant to your example, and explains why this happens.
Quote from the link in @boris834's answer: > In the case of our concat chain, each LazySeqâs fn returns another LazySeq. seq has to recurse through them until it finds an actual value. If this recursion goes too deep, it overflows the stack.
My biggest take-away from the article is: > Donât use lazy sequence operations in a non-lazy loop.
I think this is a good one: http://clojure-doc.org/articles/language/laziness.html
There are a couple of examples on https://clojuredocs.org/clojure.core/lazy-seq
I like to think of lazy-seq as producer function that returns infinite items. And never realize the lazy-seq functions on it's own.
Yes, thatâs how I imagine them as well. My test for if I have managed to create a lazy function is if it goes faster if i (take 10)
from it.
(defn drop-every [nth coll]
(lazy-seq
(when-let [s (seq coll)]
(concat (take (dec nth) coll)
(drop nth coll)))))
I looked to (source take-nth)
for inspiration.oops! forgot the recursive call
(defn drop-every [nth coll]
(lazy-seq
(when-let [s (seq coll)]
(concat (take (dec nth) coll)
(drop-every nth (drop nth coll))))))
Cool! Maybe this is a slight readability improvement:
(defn drop-every [nth coll]
(lazy-seq
(when-not (empty? coll)
(concat (take (dec nth) coll)
(drop-every nth (drop nth coll))))))
You should send it to Eric Normand: https://purelyfunctional.tv/issues/purelyfunctional-tv-newsletter-316-avoid-licensing-and-support-issues-with-the-right-jdk-for-you/?__s=wkwggxzow6hmjokqmbnf
I am getting this exception when I try to run a jdbc query
against a mysql database and I have no clue why it is happening. Does anyone know?
Unhandled javax.naming.NoInitialContextException
Need to specify class name in environment or system property, or in
an application resource file: java.naming.factory.initial
I have these in my dependency map in project.clj
:
[mysql/mysql-connector-java "8.0.15"]
[org.clojure/java.jdbc "0.7.9"]
@pez I'm interested to see your other solutions. Would you share them? There's one solution on top of my head involves: mapped-index
and filter
Something like this?
(defn drop-every [nth coll]
(->> coll
(map-indexed #(when (< 0 (rem (inc %1) nth)) %2))
(remove nil?)))
đYeah like this! I'm wondering if there's other way of solving this problem
(There must be.)
This one is less efficient:
(defn drop-every [nth coll]
(let [nth-1 (dec nth)]
(->> coll
(partition-all nth)
(map #(take nth-1 %))
(apply concat))))
Thanks I like this one
Hereâs why I picked up this particular problem: https://purelyfunctional.tv/issues/purelyfunctional-tv-newsletter-315-use-the-correct-data-structure-for-the-job/?__s=wkwggxzow6hmjokqmbnf
Thanks for sharing! I've wanted to sign up more clojure newsletters for a while
Now I remembered about keep-indexed:
(defn drop-every [nth coll]
(keep-indexed #(when (< 0 (rem (inc %1) nth)) %2) coll))
I saw your solution, but then it wasnât there. I tried it though, and it works and is pretty fast.
Since you seem to share my interest for these kind of problems you might like this thread: https://clojureverse.org/t/eratosthenes-party-time-a-k-a-feedback-wanted-on-this-implementation-of-eratosthenes-sieve/3801/16
Turns out partitition-all
has this behaviour built in:
(defn drop-every [nth coll]
(apply concat (partition-all (dec nth) nth coll)))
Thanks for sharing the clojureverse discussion. It looks very cool and I think there're a lot of things that I can learn from
@lvbarbosa *e should give you a complete stacktrace
#error {
:cause Need to specify class name in environment or system property, or in an application resource file: java.naming.factory.initial
:via
[{:type javax.naming.NoInitialContextException
:message Need to specify class name in environment or system property, or in an application resource file: java.naming.factory.initial
:at [javax.naming.spi.NamingManager getInitialContext NamingManager.java 685]}]
:trace
[[javax.naming.spi.NamingManager getInitialContext NamingManager.java 685]
[javax.naming.InitialContext getDefaultInitCtx InitialContext.java 305]
[javax.naming.InitialContext getURLOrDefaultInitCtx InitialContext.java 342]
[javax.naming.InitialContext lookup InitialContext.java 409]
[clojure.java.jdbc$get_connection invokeStatic jdbc.clj 419]
[clojure.java.jdbc$get_connection invoke jdbc.clj 274]
[clojure.java.jdbc$db_query_with_resultset_STAR_ invokeStatic jdbc.clj 1093]
[clojure.java.jdbc$db_query_with_resultset_STAR_ invoke jdbc.clj 1075]
[clojure.java.jdbc$query invokeS
tatic jdbc.clj 1164]
[clojure.java.jdbc$query invoke jdbc.clj 1126]
[clojure.java.jdbc$query invokeStatic jdbc.clj 1142]
[clojure.java.jdbc$query invoke jdbc.clj 1126]
[learn.core$eval5973 invokeStatic form-init2045875664231350038.clj 11]
[learn.core$eval5973 invoke form-init2045875664231350038.clj 11]
[clojure.lang.Compiler eval Compiler.java 7176]
[clojure.lang.Compiler eval Compiler.java 7131]
[clojure.core$eval invokeStatic core.clj 3214]
[clojure.core$eval invoke core.clj 3210]
[clojure.main$repl$read_eval_print__9068$fn__9071 invoke main.clj 414]
[clojure.main$repl$read_eval_print__9068 invoke main.clj 414]
[clojure.main$repl$fn__9077 invoke main.clj 435]
[clojure.main$repl invokeStatic main.clj 435]
[clojure.main$repl doInvoke main.clj 345]
[clojure.lang.RestFn invoke RestFn.java 1523]
[nrepl.middleware.interruptible_eval$evaluate invokeStatic interruptible_eval.clj 79]
[nrepl.middleware.interruptible_eval$evaluate invoke interruptible_eval.clj 55]
[nrepl.middleware.
interruptible_eval$interruptible_eval$fn__921$fn__925 invoke interruptible_eval.clj 142]
[clojure.lang.AFn run AFn.java 22]
[nrepl.middleware.session$session_exec$main_loop__1022$fn__1026 invoke session.clj 171]
[nrepl.middleware.session$session_exec$main_loop__1022 invoke session.clj 170]
[clojure.lang.AFn run AFn.java 22]
[java.lang.Thread run Thread.java 834]]}
I still don't understand the reason :(
clojure.java.jdbc tries to accept a wide range of different connection specifications, and whatever is in your connection specification makes it thing you are using jndi(which is this whole other thing) to inject the connection information, so it is trying to use jndi and getting that error when doing so
I just got the latest mysql connector, maybe something changed?
I'll try to get an older version
basically the map specifying your connection to mysql is "wrong" but wrong in a way that makes jdbc think you are trying to use jndi, and because you aren't using jndi you get that error
look at your connection spec map and compare it to the the docs and make sure it is correct
this is my db-spec:
(def db-spec {:dbtype "mysql"
:name "learn"
:user "root"
:password "root"})
I'm probably missing something obvious
ok, I found it
:dbname instead of :name
thanks @U0NCTKEV8