This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-09-28
Channels
- # 100-days-of-code (10)
- # announcements (2)
- # architecture (16)
- # beginners (51)
- # bitcoin (3)
- # calva (1)
- # cider (6)
- # cljdoc (8)
- # cljs-dev (14)
- # cljsrn (4)
- # clojure (59)
- # clojure-italy (26)
- # clojure-losangeles (1)
- # clojure-nl (13)
- # clojure-spec (54)
- # clojure-uk (81)
- # clojurebridge (4)
- # clojurescript (20)
- # core-async (16)
- # cursive (39)
- # datomic (27)
- # emacs (12)
- # events (1)
- # figwheel-main (20)
- # fulcro (35)
- # funcool (1)
- # graphql (9)
- # hyperfiddle (10)
- # jobs (1)
- # jobs-discuss (7)
- # keechma (10)
- # lumo (22)
- # nrepl (18)
- # off-topic (28)
- # onyx (3)
- # pedestal (4)
- # re-frame (8)
- # reagent (8)
- # ring (4)
- # rum (3)
- # shadow-cljs (29)
- # testing (5)
I have a web programming question regarding migratus. Is there a channel more suitable than #clojure?
@yogthos I get an error Failure: Query returns results
in response to a ALTER TABLE ... RENAME COLUMN ... TO ...;
query. (https://github.com/yogthos/migratus/issues/149) Any idea what's wrong or how to wrokaround that?
The only other thing I tried was the sqlite3
CLI program. And there I get no output at all.
In case anyone else wonders whether there is any documentation besides https://clojure.github.io/java.jdbc/ that is more beginner friendly: There is http://clojure-doc.org/articles/ecosystem/java_jdbc/home.html , which seems pretty solid.
Was quite easy. Discovered https://github.com/bhauman/rebel-readline along them way.
@yogthos clojure.java.jdbc replies with ()
: https://github.com/yogthos/migratus/issues/149#issuecomment-425426230
what could be a nice way to deduplicate a vector of maps based on one of the keywords
for example this works
user> data
[{:a 1, :b 2} {:a 1, :b 3}]
user> (flatten (map first (clojure.core/vals (group-by :a data))))
({:a 1, :b 2})
In my utils namespace there is a distinct-by
function, a custom version of distinct
that applys a f
in the values
https://gist.github.com/souenzzo/2a2e74d56fc7bd6e7a35977bfb0939b9
It do the same of distinct
Or you can do (def distinct-by #(map (comp first val) (group-by %1 %2)))
but I wonder if there is a better way to do that
is there something more stringly for this?
(map #(apply str %) (partition-by #{\. \#} "foo#id.class")) ;;=> ("foo" "#" "id" "." "class")
Could do regex I guess, but str/partition-by would be nice if it existed 🙂@andrea.crotti you could use reduce and assoc into a new map based on value
You don’t need to group by because any two maps with the same particular k/v pair are considered the same, so just replace whatever was there already.
If you don’t want to build a map, you could have a set of seen values and a seq of maps. If you’ve seen the value before, don’t add the value to the seq of maps.
uhm yes I don't want to get a map as result
and yes I could two passes, but I think I prefer the group-by/flatten solution then
user=> (defn dedupe-by [key-fn maps]
#_=> (second
#_=> (reduce
#_=> (fn [[seen-vals deduped-maps] m]
#_=> (if (contains? seen-vals (key-fn m))
#_=> [seen-vals deduped-maps]
#_=> [(conj seen-vals (key-fn m)) (conj deduped-maps m)]
#_=> ))
#_=> [#{} []]
#_=> maps)))
nice but way too complicated imho
@futuro dedupe-by
might not be a good name because clojure.core.dedupe
does something different. It only deduplicates consecutive repeating values. distinct-by
might be better.
This is what you get when you add a key-fn
to implementation of clojure.core.distinct
That's a good point @jaihindhreddy
(defn distinct-by
"Returns a lazy sequence of the elements of coll with duplicates according to key-fn removed.
Returns a stateful transducer when no collection is provided."
([key-fn]
(fn [rf]
(let [seen (volatile! #{})]
(fn
([] (rf))
([result] (rf result))
([result input]
(let [lookup-key (key-fn input)]
(if (contains? @seen lookup-key)
result
(do (vswap! seen conj lookup-key)
(rf result input)))))))))
([key-fn coll]
(let [step (fn step [xs seen]
(lazy-seq
((fn [[f :as xs] seen]
(when-let [s (seq xs)]
(let [lookup-key (key-fn f)]
(if (contains? seen lookup-key)
(recur (rest s) seen)
(cons f (step (rest s) (conj seen lookup-key)))))))
xs seen)))]
(step coll #{}))))
@andrea.crotti Your code then becomes
user=> (def input [{:a 1 :b 1} {:a 2 :b 2} {:a 1 :b 99999} {:a 2 :b 99999} {:a 3 :b 4}])
#'user/input
user=> (distinct-by :a input)
({:a 1, :b 1} {:a 2, :b 2} {:a 3, :b 4})
user=> ; Or with a transducer
user=> (into [] (distinct-by :a) input)
[{:a 1, :b 1} {:a 2, :b 2} {:a 3, :b 4}]
user=>
hehe we have a list of max 50 elements and 10 keys, I'd rather use the simplest possible solution
I’m fairly new to Clojure and I’ve got a question regarding core.async channels. I’m using a http library which returns a channel - Now I want to do two http request at the same time and then “wait” for both to complete. How do I achieve such?
you can also check out alts!!
conversation continues in #core-async but since both results are needed for the next step, just calling <!!
on each channel suffices
I have a map of var-quoted functions that I'm using as a lacinia resolver map. After adding fspecs for all of them and turning on instrumentation, I'm getting this error:
com.fasterxml.jackson.core.JsonGenerationException: Cannot JSON encode object of class: class clojure.spec.alpha$regex_spec_impl$reify__2436: [email protected]
removing the var-quoting resolves the error. is there any way I can var-quote a function that's been fspecced?
I am trying to print a LazySeq as (println my-lazy-seq)
, but Clojure tells me: java.lang.ClassCastException: clojure.lang.LazySeq cannot be cast to clojure.lang.IFn
Any suggestion on what my mistake might be?
println won't cause that error, but extra parens around your lazy-seq will
Was rather easy: I used (-> ... (map f))
to create the LazySeq, a mistake that only surfaced when trying to use its elements and thus realising it.
for those of you that use JDBC, how do you go about automating tests for your queries?
You can use clojure.test
fixtures
to set up DB state and have it automatically roll-back between tests, then have individual tests run queries
e.g. my app talks to Postgres in AWS RDS, but I imagine I could spin up a local Postgres(-ish?) instance and initialize test data in it
breathes Had to go to the django IRC
Fine community but once you get into Clojure & FP having to ask questions like “How I do model this SQL in Django’s QuerySet?” reminds me why I sought refuge elsewhere 🙂
Django ORM is fairly broken 😕
Hah that doesn’t surprise me! Any specific issues come to mind? Anything that makes a good case to start moving towards Clojure would be much appreciated.