This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-01-14
Channels
- # announcements (6)
- # architecture (5)
- # aws (4)
- # beginners (79)
- # boot (3)
- # boot-dev (7)
- # calva (21)
- # cider (17)
- # cljdoc (12)
- # clojure (83)
- # clojure-art (2)
- # clojure-belgium (2)
- # clojure-brasil (1)
- # clojure-estonia (2)
- # clojure-europe (3)
- # clojure-finland (5)
- # clojure-india (2)
- # clojure-italy (49)
- # clojure-losangeles (1)
- # clojure-nl (12)
- # clojure-spec (120)
- # clojure-sweden (2)
- # clojure-switzerland (4)
- # clojure-uk (31)
- # clojurescript (80)
- # data-science (17)
- # datavis (2)
- # datomic (31)
- # emacs (31)
- # figwheel-main (28)
- # fulcro (6)
- # jobs (2)
- # liberator (7)
- # luminus (1)
- # nrepl (2)
- # off-topic (51)
- # overtone (2)
- # pathom (4)
- # re-frame (28)
- # reitit (1)
- # rum (6)
- # shadow-cljs (26)
- # specter (2)
- # tools-deps (33)
- # yada (3)
repeatedly
if function has no args: https://clojuredocs.org/clojure.core/repeatedly
iterate
if function has args (and is without side-effects): https://clojuredocs.org/clojure.core/iterate
if you want to call a function which always returns the same value for n times, like (Thread/sleep 10000), repeatedly is OK; otherwise you should use iterate, for it re-use the return-value of the previous calling as the argument of the next calling
Ι don't think that's correct, repeatedly
takes a function with side effects as argument, so it shouldn't have to return the same value
If you are just doing it for side effects and don’t care about the return values, you can also do (dotimes [_ n] (f))
Since we’re talking side effects, run!
is also suited for side-effecting functions, if you want to run it over some reducible collection:
(run! (fn [i] (println i)) (range 10))
Wondering if Clojure has taken advantage of the new CHAMP (Compressed Hash Array-mapped Prefix Tree) data structure for their default HashMap/HashSet implementations?
in the upcoming Scala release (2.13) the existing HAMT structures were replaced with CHAMP ones
the hashmap impl has certainly not changed. That said, you can implement the correct protocols for whatever hashmap impl you want and it will play nice
I think the only thing you won’t get is its literal {}
…
you can get it to pr as a {}
(even java.util.HashMap gets that behavior already), but yeah, you shouldn't even try to override the reader for {}
now I got curious…
Do you know if a change to CHAMP structure has been considered and rejected, or nobody has proposed it yet?
I think that a change as fundamental as what algorithm to use for a hashmap has to have an incredibly high payoff.
Especially because it is often not “this impl is flat better”, but more “this impl can better deal with these situations, but worse with these”
reading about what CHAMP does that existing immutable hm impls don't, Clojure already uses a tree of arrays, which gets some of those advantages. And the lengths of these arrays were performance tuned.
but yeah, using deftype or a bit of java code to implement the right protocols for Hashmap in clojure is a weekend hack, why not try
You can probably bind to the Scala impl if you really wanted to
this was discussed a couple of years ago on the mailing list https://groups.google.com/forum/#!topic/clojure/zO-5yUx2aSk
there are serious clojurists who have prototyped it and found the claims to not be achievable
there was a phenomenal talk at clojure/west in 2016 about changing the tree structure but it had to do with sorting the values versus pointers to new nodes. I really enjoyed that talk
tl;dr of my links above: you can try @ztellman’s bifurcan lib if you want to use an implementation of CHAMP
speed of my programs is way more often plagued by my bad programming, than with what hashmap algorithm is used :’)
If I have some record in a clojure namespace and it gets serialized to EDN, its tag becomes a Javaified name. Can I programatically construct this same name?
So, my-awesome-namespace.MyRecord
becomes my_awesome_namespace.MyRecord
@lennart.buit (class my-obj)
should get you this, given an instance
but before you go to far with that, depending on what you are doing, using a tagged literal is often better then using record literals directly
Also some improvements that we are doing in the next Scala release which Clojure could also do, is to build up hashmaps / hashsets mutably (but never mutating one that has been returned to a caller)
Well, I have records that are serialized to EDN, and I want to define a reader map that takes the generated tag and returns a record
Sorry if it already does this, I’m looking at PersistentHashMap.java
right now and I don’t think it does do that
@joshlemer that's how transients work currently
implicitly used by eg. into
but saying that {'my_awesome_namespace.MyRecord my-awesome-namespace/map->MyRecord}
becomes tiresome quite quick
@lennart.buit I'm confused - defining your record implicitly creates a reader for that record, it should just work
@noisesmith ah yes I stand corrected 🙂
but I think if you are creating lots of different kinds of defrecords, and also serializing them, that kind of sounds like the kind of system that would be better served using plain maps
they are records because they are implementing protocols, but maybe you are right
but hypothetically, how would I do this in the least amount of keystrokes given clojure.edn
?
the problem is clojure.edn is designed explicitly so that you have to whitelist everything, because the clojure.core functions allow all kinds of stuff that are security concerns
hmm actually I don’t think the tree structure of the map is being mutated, just the root wrapping object
so the least amount of key strokes would be using clojure.core/read-string over clojure.edn/read-string(same for read), but you can only do that for trusted data
If I am using Liberator and in my :handle-exception
an exception occurs... What handles that? The same function? It seems it loops for a while
You should avoid throwing exceptions from handle-exception
. You might have found a bug. Can you please find an issue at https://github.com/clojure-liberator/liberator?
I think this one is related to what I am seeing https://github.com/clojure-liberator/liberator/issues/289
if you are using clojure.edn because your data is untrusted(api exposed to the outside world, etc), then I think tying the data format to your internal class names (which is what those record tags are) is a bad idea
I am okay with the whitelisting, actually quite prefer it, I am just a bit tired of typing the entire java-ified name of a record, making seven typos, not understanding why my code crashes, fixing 5 out of the 7, repeat for each record. While I can have the record imported…
sorry - I am actually not
(importing records instead of using the factory functions leads to common errors like failing to require the namespace that defines the record)
Right, that was a bit of a hyperbole on my part, I don’t actually import the generated classes, I use plain old defn
s as facades for the map->...
and ->...
constructors
Anyhow, given your concerns I will rethink what I have now
like, if this isn't an exposed api end point or whatever, just use the clojure.core versions of the functions, not the clojure.edn ones, and the record readers will just automatically work (if I recall)
It is certainly not, I was looking for (.getCanonicalName ...)
btw
user=> (defrecord Foo [])
user.Foo
user=> (->Foo)
#user.Foo{}
user=> #user.Foo{}
#user.Foo{}
user=>
Yeah they are, its just the default
if yours isn't behaving like that you have something breaking it (either your code, or some library you use)
I think the other EDN reader will work fine, but I have to make up my mind whether I like it… haha. Help much appreciated!
When working with hiccup, what’s the best way to create a custom component? Is it just a function I call that returns a hiccup vector or is there some macro I import like defelement
so I can do [:my-element …]
?
@jayzawrotny it varies on the library. are you using reagent, or weavejester's hiccup
library, or rum...?
Ah, I never realized those were technically separate flavors. I’m using weavejester’s hiccup lib.
in that case, you wouldn't create "custom components". instead, you just have functions that return hiccup data and call them as normal functions
Hey, So I'm trying to create a simple CRUD to taste some TDD, and I'm completely lost on how to "mock"a in memory database
Could be yes. You could also use DataScript.