This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-07-22
Channels
- # announcements (4)
- # aws (1)
- # beginners (82)
- # boot (14)
- # braveandtrue (1)
- # calva (73)
- # cider (7)
- # cljs-dev (7)
- # cljsrn (1)
- # clojure (83)
- # clojure-belgium (1)
- # clojure-dev (3)
- # clojure-europe (4)
- # clojure-france (3)
- # clojure-italy (31)
- # clojure-nl (11)
- # clojure-uk (15)
- # clojurescript (19)
- # code-reviews (9)
- # cursive (16)
- # data-science (1)
- # datomic (16)
- # events (1)
- # fulcro (4)
- # graalvm (1)
- # hoplon (7)
- # jackdaw (5)
- # java (10)
- # jobs (6)
- # jobs-discuss (7)
- # juxt (1)
- # leiningen (6)
- # midje (1)
- # off-topic (2)
- # pathom (18)
- # pedestal (1)
- # reagent (8)
- # reitit (2)
- # ring (1)
- # shadow-cljs (38)
- # spacemacs (5)
- # sql (41)
- # vim (10)
- # xtdb (1)
Please suggest how to improve this code (and whether it needs to be improved): https://gist.github.com/dottedmag/f38b25718cb151b81ed429312092546b
@dottedmag both disj and and conj behave reasonably if you try to add an element that isn’t/is present in the set
@cfeckardt Unfortunately, (conj #{} (peek clojure.lang.PersistentQueue/EMPTY))
gives #{nil}
, not #{}
Not a big deal, but I prefer defn ^:private
over defn-
, for consistency with other defining operators. See https://github.com/bbatsov/clojure-style-guide/issues/124#issuecomment-175068673
you might try proxy
with proxy-super
(but this has concurrency gotchas...)
🖐️ im trying to test a ring clojure app with jsoup
, but im not able to keep the session while connecting to logged urls.
The session is not in a cookie
, neither on headers
or params
Altough there are plenty of resources with jsoup, I am not able to find a way to add a custom session object on a request 😕
I am writing a C# + OpenTK (Open GL) 2D game, i was wondering if someone has any idea about embedding Clojure CLR in a C# app for scripting purposes?
Have you looked at Arcadia? https://github.com/arcadia-unity/Arcadia
Yes but its code isnt something i can really understand yet. It is too specific to Unity which i dont know how to work on.
@metacritical I'd expect something that looks a lot like this https://clojure.org/reference/java_interop#_calling_clojure_from_java but s/java/clr/
there's only two or three methods you actually need to load up and exec clojure (the clr port is of course different, but not massively so...)
in fact this code might even work directly:
IFn require = Clojure.var("clojure.core", "require");
require.invoke(Clojure.read("clojure.set"));
with maybe some minor syntax tweakswhere the foo.invoke()
in host side interop is the same as (foo)
in clojure itself
@noisesmith why is Clojure.read
necessary there? I haven’t had to do that in the past to call Clojure from Java, but maybe I’m just getting lucky?..
read takes a string and here returns a symbol - require operates on symbols not strings
Ah, I just use symbol manually
this is an example that can be generalized to loading arbitrary code and running it, not just running a single file / ns
That’s why
right, symbol vs. read is a stylistic approach I guess
Makes sense! Just wanted to make sure i wasn’t missing something 🙂
some funny corner cases here :D
=> (= '1 (symbol "1"))
false
oh boo
the symbol of "1" looks like the number 1 but doesn't act like it at all
also, imagine using hash-map and symbol and keyword and Integer/parseInt to construct {:a 0 'b 1}
as opposed to one call to read on a string
Oh yeah no thanks!
Luckily my usages of calling Clojure from Java are pretty limited and generally limited to requiring some namespaces and then calling a few functions off of some deftype
s
yeah, I think that's typical
But yeah, if it ever gets beyond that I’ll have to remember that
Anyone know an easier way of writing this function?
(defn convolute
[coll]
(apply map (fn [& xs] (into {} xs))
(map
(fn [[k vs]]
(map (fn [v] [k v]) vs))
coll)))
(convolute {:a [1 2] :b [3 4]})
;; => ({:a 1, :b 3} {:a 2, :b 4})
usually nested map calls can be replaced by for
Yeah you can come up with a partition
/`interleave` thing here but IMO it looks worse
(for [m (iterate
(fn [m]
(zipmap (keys m)
(map rest (vals m))))
{:a [1 2] :b [3 4]})
:while (first (first (vals m)))]
(zipmap (keys m) (map first (vals m))))
I almost like thisIf you go the for
route I think you have to use partition / interleave
(let [m {:a [1 2] :b [3 4]}]
(for [i (range (count (first (vals m))))]
(zipmap (keys m) (for [v (vals m)] (nth v i)))))
I think the nested sub-sequences here actually make it easier vs. removing the nesting with for
another approach
(defn convolute'
[coll]
(map (partial into {})
(apply (partial map list)
(for [[k vs] coll]
(for [v vs]
[k v])))))
So many ways of writing the same function! I like @hiredman’s second solution, that seems the closest to being "declarative" without the reader having to reconstruct the control flow and intermediate structures in their head 🙂
I do too
One thing to watch out with that solution is that if (count (first (vals m))
is larger than the vals of some other item in your map you’ll get an IndexOutOfBounds exception
Because of the nth
call
(min-key count (vals m)) ?
that fails
(let [m {:a [1 2] :b [3 4] :c [5]}]
(for [i (range (apply min (mapv count (vals m))))]
(zipmap (keys m) (for [v (vals m)] (nth v i)))))
=> ({:a 1, :b 3, :c 5})
=> (apply min (map count (vals {:a [1 2 3] :b [0] :c [3 4]})))
1
I didn’t try min-key
min-key fails because it returns the object with the lowest score, not the score itself (and it also needs apply)
if you must
(let [m {:a [1 2] :b [3 4] :c [5]}
db (clojure.set/index
(for [[k vs] m
[i v] (map-indexed vector vs)]
{:k k
:i i
:v v})
[:i])]
(for [i (range (count (first (vals m))))]
(into {}
(for [{:keys [k v]} (get db {:i i})]
[k v]))))
(let [m {:a [1 2] :b [3 4] :c [5]}
db (clojure.set/index
(for [[k vs] m
[i v] (map-indexed vector vs)]
{:k k
:i i
:v v})
[:i])]
(for [i (range)
:let [s (for [{:keys [k v]} (get db {:i i})]
[k v])]
:while (seq s)]
(into {} s)))
(defn convolute-2 [m]
(let [[ks vss] [(keys m) (vals m)]]
(for [vs (apply map list vss)]
(into {}
(for [[k v] (map list ks vs)]
[k v])))))
@hiredman Is map vector
better than map list
? I’ve always used the former, but I copied the latter from something above.
you can replace [(keys m) (vals m)]
with ((juxt keys vals) m)
(whether that's an improvement is up to you)
right, the body creates the same data that comes into the destructure
Ah, yes, of course. Time to go and do something that isn’t coding. 🙂
(defn convolute-2 [m]
(let [[ks vss] [(keys m) (vals m)]]
(for [vs (apply map vector vss)]
(into {} (map vector ks vs)))))
(let [m {:a [1 2] :b [3 4] :c [5]}]
(for [i (range (count (first (vals m))))]
(zipmap (keys m) (for [v (vals m) :when (> (count v) i)] (nth v i)))))
or :when (contains? v i)
or even :while (contains? v i)
- that should short circuit properly