This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-01-03
Channels
- # aleph (1)
- # beginners (99)
- # boot (16)
- # cider (35)
- # cljs-dev (46)
- # cljsrn (7)
- # clojure (152)
- # clojure-austin (7)
- # clojure-dusseldorf (8)
- # clojure-italy (1)
- # clojure-uk (7)
- # clojurescript (3)
- # core-async (12)
- # css (8)
- # cursive (18)
- # datascript (2)
- # datomic (19)
- # defnpodcast (6)
- # duct (3)
- # editors (8)
- # emacs (8)
- # figwheel (1)
- # fulcro (20)
- # hoplon (18)
- # jobs-discuss (5)
- # lein-figwheel (1)
- # luminus (3)
- # lumo (19)
- # off-topic (15)
- # onyx (9)
- # parinfer (2)
- # planck (6)
- # portland-or (7)
- # re-frame (4)
- # reagent (7)
- # remote-jobs (1)
- # ring (6)
- # ring-swagger (4)
- # spacemacs (10)
- # specter (3)
- # unrepl (131)
I bet this problem is simple but I can't really figure out the easiest way to do it. I have a collection like this
[{:emails ["first" "second"] :id 1} {:emails ["third"] :id 2}]
- basically key/val with option for mutliple emails which is always a collection. And I would like to duplicate the map for each email in it's vector so the result looks like this
[{:id 1 :email "first"}
{:id 1 :email "second"}
{:idi 2 :email "third"}]
Any hints where to look? Thanks a lot guys@petr.mensik this can be done with the sequence fns in clojure.core, here’s one way to do it:
user=> (def a [{:emails ["first" "second"] :id 1} {:emails ["third"] :id 2}])
#'user/a
user=> (mapcat (fn [{:keys [id emails]}] (map #(hash-map :email % :id id) emails)) a)
({:email "first" :id 1} {:email "second" :id 1} {:email "third" :id 2})
@schmee nice, haven't thought about mapcat
. Thanks a lot
I came up with
(reduce #(into %1 (for [email (:emails %2)] {:id (:id %2) :email email})) [] a)
which gives [{:id 1, :email "first"} {:id 1, :email "second"} {:id 2, :email "third"}]
not sure which is better..If I have a lazyseq that traverses over a very large collection (>14 million items) - does that data accumulate in memory as I do a doseq over it?
@donmullen no, that is what’s meant by “does not retain the head of the sequence” in the docstring: http://clojuredocs.org/clojure.core/doseq
@schmee So in this code - I do have ‘data’ that keeps the head — so likely need to figure a way to not do that?
I can’t see anything holding on to the head in there, maybe csv/parse-csv
does some shenanigans?
Any one know how to make the websocket connection always connect? I can receive a few messages and the connection close.
why would (clojure.data.json/read-str (slurp filename))
throw java.lang.StringBuilder cannot be cast to java.io.PushbackReader,
?
@teikfaiv it's a reader macro. check https://clojure.org/reference/reader. That particular one ignores the next form at a reader level. It skips that form completely
@teikfaiv It generally tells Clojure to generate a Java-compatible function (without the leading -
).
So when you see (defn -main [& args] ...)
that's like a Java main()
function.
if I pass to a java method a class generated by gen-class, does it mean that I can forget about running the application from the repl?
@teikfaiv Not sure what you mean... can you elaborate?
@seancorfield I think the concern is that the class created by gen-class can’t be passed to a method that wants a class if you are loading the code from the repl
@teikfaiv if I understand your question correctly, gen-class works even if your namespace is compiled from the repl, as long as you load it from a file (via require or load-file)
in fact the clojure repl is the clojure compiler - there’s no separate code path (though there are some things like gen-class that become no-ops if typed directly into a repl instead of being loaded from a file)
Hey guys, I have a brain fart on a threading macro, can anyone help me out?
(-> args
add-foo
(partial add-bar context))
I keep getting a thing back, #object[clojure.core$partial$fn__5563 0x7cb4782b "clojure.core$partial$fn__5563@7cb4782b"]
@admay ->
is not smart, it’s a thing that fiddles with lists before they are compiled
that form you posted expands to (partial (add-foo args) add-bar context)
I’m not saying you aren’t smart for using it, I’m just saying it has no “sense” - it’s just a rote list transform
you can use ->> inside -> and skip using partial
(-> args (add-foo) (->> (add-bar context)))
will do what you want
or you can just use (add-bar context (add-foo args))
of course (but I assume this is a placeholder for a more complex expression)
> I’m not saying you aren’t smart for using it, I’m just saying it has no “sense” - it’s just a rote list transform Totally did not take it as that! 😄 > but I assume this is a placeholder for a more complex expression Not very complex however theres a good chance of this being expanded to something more complex in the future, i.e.
(-> args
add-a
add-b
add-c
...)
So I’m trying to make it a bit easier on the reader in future iterations.yeah - you can use ->
on the outside, and alternately use ->>
inside as needed to use first position vs. last position of threaded value
actually, now that I think of it, (->> args (add-foo) (add-bar context))
works in your case since you never actually need thread-first behavior here
(->> args (add-foo) (add-bar context))
This did it!
I also just realized that I ought to be using macroexpand
to look into these too!
Thanks for the help @noisesmith, this helped to clarify a lot about the threading macro behavior!
@admay my favorite illustration of how naiive ->
is
=> (-> [a 22 b 20] (let (+ a b)))
42
don’t write code like that though 😄
the threading macros can simplify code a lot but don’t expect them to make sense without your help and guidance
@admay if the standard threading macros become too easy / not confusing enough for others, have a go at https://github.com/rplevy/swiss-arrows 😄
a lot of stuff from swiss-arrows is made redundant with things in clojure.core now though as->
, some->
, cond->
etc.
haha yeah, I wouldn’t recommend anyone to use a library with a function called -<><:p
for anything other than educational purposes 😉
clearly someone let the perl guys into the party
some->
is under-rated, massively simplifies a lot of my code
I wish there was a cond-as->, cond->/>> is hugely underrated for composing conditional updates.
I should make a version of some->
that has an extra “value to return if we stopped here” expression, then I could use it even more
I have an entire codebase that’s mostly core.logic.pldb with cond-> to implement the application logic
@noisesmith sounds like what you really want is an MEmpty that isn’t nil
@arrdem I want a value that is specific to the expression - if that’s what that means
so I can know in symbolic form which step we had to stop at
I was born with an obscure disability that prevents me from ever understanding monads or datomic
@seancorfield @noisesmith this is an example (see issues in the readme): https://github.com/nblumoe/clj-ui-spikes/tree/master/javafx-ui
isn’t this about order of initialization? surely the gen-class created class exists in the repl
right, and “compiling” is loading something from a file
if you didn’t type or copy paste into the repl itself, it is compiling when it loads the code
clojure is a compiler, it doesn’t have an interpreter
(on the jvm, that is)
well, then I have no idea, lein run
returns an error though. While you can uberjar and run it.
but clojure won't do it for you unless are aot compiling, for reasons which I think have to do with classloaders and class visbility
so from a repl you could call compile
right?
oh it can’t use a resource on classpath?
compile also has to write the classfiles to disk to a location that is on the classpath
I’m coming to the conclusion that questions about gen-class
don’t belong in #beginners
in fact, definterface worked like gen-class and i changed it to generate bybtecode while jiting too
there’s fn-fx - I’d solidly bet it’s better than any of the other wrappers you might find
and afaik it doesn’t even use gen-class
I spent some time poking at javafx for a bit, but that was a while ago, it seemed to make a lot of static assumptions(like exiting the jvm when you closed a window by default) which made it a challenge to work with
that other lib you linked to hasn’t seen a commit in the last year
@noisesmith that is just a javafx hello world using plain clojure interop with java
oh, OK
anyway, FYI, the author of fn-fx is still very active in the clojure community, and has made a lot of good contributions, I’d be more likely to trust something he wrote than most alternatives (though honestly I have not used fn-fx)
@teikfaiv One thing you'll probably have to get used to in the Clojure world is that there are a lot of small, focused libraries that look "abandoned" but are actually stable and functionally complete 🙂
(I've not looked at JavaFX but can't you just use proxy
and reify
to generate objects on the fly to interact with it?)
Yeah, fn-fx isn't abandoned, there simply aren't a ton of people doing JavaFX work in the Clojure community.