This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-06-08
Channels
- # aleph (52)
- # beginners (74)
- # boot (8)
- # cider (4)
- # clara (3)
- # cljs-dev (1)
- # cljsjs (2)
- # cljsrn (1)
- # clojars (2)
- # clojure (300)
- # clojure-argentina (1)
- # clojure-dev (9)
- # clojure-italy (10)
- # clojure-nl (1)
- # clojure-russia (77)
- # clojure-sg (9)
- # clojure-spec (38)
- # clojure-uk (70)
- # clojurescript (108)
- # core-async (12)
- # cursive (9)
- # data-science (4)
- # datascript (7)
- # datomic (37)
- # defnpodcast (4)
- # emacs (11)
- # graphql (6)
- # jobs (3)
- # jobs-discuss (1)
- # juxt (3)
- # keechma (1)
- # klipse (4)
- # lein-figwheel (1)
- # lumo (1)
- # off-topic (3)
- # om (5)
- # onyx (10)
- # parinfer (3)
- # pedestal (1)
- # perun (1)
- # protorepl (3)
- # re-frame (35)
- # reagent (19)
- # spacemacs (4)
- # specter (2)
- # uncomplicate (279)
- # unrepl (32)
is there a way to get ring/jetty to serve on https://localhost/ .... or does it make no sense since "localhost" is not a full domain name and thus it can't be https ?
The certificate wouldn’t match. You’d need to self-sign.
and then I can do https://localhost ?
That should do it.
Calling all clojurians who are in SINGAPORE, please search #clojure-sg to join our Singapore clojure channel:grinning:😀
what’s is a good concise resource for someone outside community who wants to learn about clojure’s data-oriented programming style?
@tap Brave Clojure http://www.braveclojure.com/ It's also amusing 🙂
When writing a clj / cljs application, what is a good way to track (server side) "this user here is logged in with username foo" ?
This must be a solved problem, and I absolutely do not want to roll my own custom possibly insecure solution.
When I add an atom from a different ns and set this to a local variable, why isn't the "remote" atom changed when I swap the content?
(ns test.db
(def state (atom nil))
)
(ns test.core
(:require [test.db :as db])
(def local-state db/state)
(reset! local-state 1)
@local-state ; => 1
@db/state ;=> nil
)
@danielgrosse the two vars are references to the same javascript object, so it should work as you expect
there's something wrong with how you test
why the var definition in the ns declaration? shouldn't it be test.db/state
? etc
@U09LZR36F can you give an example of a "single value" in this context?
for most intents and purposes, yes. Altho there exist a non-empty difference between the feature sets those two provide
bronsa: is there a use case, where you would (and could) compose both? Or is it always "use one or the other" situation?
@pesterhazy I forgot one parenthesis in the example above. Also its in clojure not clojurescript, if this makes a difference.
Try it in the repl, it works as expected
@pesterhazy Hm strange. Will look into it, when I have time. currently I have to change the db/atom and the local var changes with it
Is there a macro to test a value on different predictions and only returning true if all succeed? Like some->
but without given the result to the next prediction?
@danielgrosse and
? :)
@curlyfry I ended up using and
:-)
Thought there was some other solution. Maybe and->
and->
sounds like something you could homebrew with and
and apply
---but I’m not quite sure exacly how myself
(every? true? ((juxt keyword? qualified-keyword?) :foo/bar))
=> true
(every? true? ((juxt keyword? qualified-keyword?) :bar))
=> false
if i have two sequences of the same length, a sequence of maps/objects, and another sequence of strings, and i want to assoc each string as the key :foo
to each respective object, what function do i need ?
i was looking at zipmap, but i’m not sure if that’s it
so basically, input ({:bar "1"}, {:bar "2"}) (3 4)
-> ({:bar "1" :foo 3} {:bar "2" :foo 4})
i probably need to first transform (3 4)
to ({:foo 3} {:foo 4})
, and then use interleave
?
although be aware that if you're sequences are not the same size you risk silently dropping data
if you have uneven ones you would lose the {:b 2}
in this case. not sure which is more valuable but maybe wrapping the lesser important one with a (concat imporant (repeat :missing))
yeah but understandable. you're giving it a two arity function and two sequences. when it runs out of one it cannot continue
When I send an vector as string to the server, how can I transform it into an vector back?
Ooh, I didn't know there was a secured version of read-string
Hi, is it possible to let an spec/fdef failure result log to, say, taoensso’s timbre?
We are thinking about enabling instrumentation in our runtime (eventhough it’s not meant for that), occasionally, to hunt down some production bugs.
lxsameer: I’m using it. It’s fine.
@U04V4HWQ4 did you have any problem with it ?
no, it does what it says on the tin. Server/servlet integration is easy, the interceptor chain construction is a little fiddly but much saner than an opaque stack of ring middleware.
So I have a list of maps that contains a milliseconds field. What would be the best way to partition the list into sublists where each sublist contains all the maps for a given timespan, like one minute for instance?
[{:t #inst"2017-06-08T15:46:39"} {:t #inst"2017-06-08T15:46:40"} {:t #inst"2017-06-08T15:46:41"} {:t #inst"2017-06-08T15:47:39"} {:t #inst"2017-06-08T15:48:39"}]
would result in:
[[{:t #inst"2017-06-08T15:46:39"} {:t #inst"2017-06-08T15:46:40"} {:t #inst"2017-06-08T15:46:41"}] [{:t #inst"2017-06-08T15:47:39"}] [{:t #inst"2017-06-08T15:48:39"}]]
[is it pre-sorted? if not sort it, and then…] partition-by
with clj-time’s within?
and interval
functions (look for within?
on https://github.com/clj-time/clj-time
hmm, actually i guess you don’t need within?
, but clj-time is still good stuff, probably truncate/round the time to whatever increment you want instead
Yea, my plan is to convert it to milliseconds,jeez and now I got the solution, of course I just have to cut the ms at the seconds part -.-
this is getting to be a bit much for me w/ the point-free stuff but:
(require '[clj-time.core :as t]
'[clj-time.coerce :as c]))
(partition-by
(comp
(juxt t/year t/month t/day t/hour t/minute)
c/from-date
:t)
times)
or like that, thank you very much @trptcolin 🙂
Anyone an idea how to temporarily work around this issue? https://github.com/ptaoussanis/nippy/issues/98
Also without clj-time dep: (partition-by (comp (juxt :year :month :day :hours :minutes) bean :t) t)
or a shorter one (partition-by #(quot (.getTime (:t %)) 60000) times)
is it possible to create a clojure library jar that can be used from Java, where the Java client is able to pass in an object.method for execution by the clojure function?
in java methods aren't really first class values you can pass around, so you have to pick some representation of the method, of which there are a few possible
I mean that if I have a class Foo { public int bar(String s) } and I want to pass in an instance of Foo and indicate that the bar function is to be called in the clojure world
one simple way to do that is to pass in both the instance of Foo and a java.lang.reflect.Method representing bar
I've also seen apis that pass strings of method names, in which case the clojure code would need to do the reflecting bits
yeah I would create an anonymous implementation of IFn and pass that. going to be a lot faster
if I recall java requires you to implement all the methods in an interface, and IFn has a lot of methods
would it be easier if the Java classes had to implement an Interface that my clojure lib was to expect?
what I might do is use https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html
so the the java code constructs a Function and the clojure code gets a Function and can directly call the method to apply it
well, then you can create your own function interface, or use one from one of java libraries that provide that sort of thing, I think guava has one
if you aren't already using guava, functionaljava is a library specifically for this kind of thing
yea we are already including guava I just looked at Guava's function, that may be Java 8 also, it looks like it expects a lambda
my understanding is he doesn't want to hardcode the method being called in the clojure code
he wants to hand the clojure code some kind of handle (a closure basically) that it can use to invoke a given method on a given object
https://google.github.io/guava/releases/snapshot/api/docs/com/google/common/base/Function.html the guava function interface should be easy to implement
an interface of IMethodToCall
and make each class you are going to pass implement this? and this is just a pass through?
we have some crazy complicated threading and the associated problems in our Java code. I'm trying to help out by writing a clojure library that handles things in a more sane way
hi, i'm playing around with some logical combinators. is there a library that can textually substitute function arguments into the body while observing shadowed variables?
hoff: If you use cider (emacs), you might try enabling the enlighten-mode
(http://cider.readthedocs.io/en/latest/miscellaneous_features/#enlighten-display-local-values)
or just use the debugger
( enlighten-mode is a feature that was inspired by http://lighttable.com/ , which also does this quite well, if you’re not an emacs person )
Is there a verion of map that stores the output in a map? i.e. that would perform the following: (special-map f [a b]) returns {a (f a) b (f b)}
(map vector coll (map f coll)) would work
the usual idiom for that is (into {} (map (juxt identity f) coll))
or (into {} (for [el coll] [el (f el)]))
specter is good for this. other than that there's about 4,127 utility libs that include a transform-keys and transform-vals variant
sorry my suggestion would not work
but the input isn’t a hash-map
but the input doesn’t have vals
Hello all,
(defmulti classifica
(fn [vl-jitter vl-perda vl-latencia]
(let [jitter 40
perda 6
latencia 150
(if (and (= -1 vl-jitter) (= -1 vl-perda) (= -1 vl-latencia))
[-1 -1 -1]
[(> vl-jitter jitter) (> vl-perda perda) (> vl-latencia latencia)]))))
(defmethod classifica [-1 -1 -1] [_ _ _] :comunicacao)
(classifica -1 -1 -1) => ArityException Wrong number of args (3) passed to ...
what I´m doing wrong?@fabrao dispatch isn’t on the vector of args, it’s on the single value returned by the dispatch function
I would put a (def classifica nil) right above your defmulti, and you should be fine
for historical reasons, recompiling a defmulti form won't change an existing defmulti, so if you have defined a defmulti in a repl with a dispatch function that takes 1 arg, and realize that is a mistake and go to fix it by redefining the defmulti with the correct dispatch function, that won't fix it
the dispatch function has to be able to handle however many args you pass, but each defmethod can handle just the args it needs to
it defs it as nil, so when the defmulti form is executed, it doesn't see an existing defmulti there
decim: I have a pedestal app that is built with boot. I’m not sure what you mean by running on top of boot.
is your app public on github by chance? I’m just looking for examples…i’ve gone through the few on http://pedestal.io but I’d like to see more fleshed out applications to get a grasp of things
Alas, it is not
if you dig back 7-8 years in the mailing list you will find some people complaining about defmulti's behavior back when it behaved more like def, so it was changed, and then shortly after that people started complaining about the defonce behavior, and it has acted like defonce ever since
hey y’all i’m trying to figure out something with clojure.spec/explain-data
Here’s what I’ve got:
(s/explain-data (s/map-of keyword? keyword?) {:x 0})
#:clojure.spec{:problems ({:path [1], :pred keyword?, :val 0, :via [], :in [:x 1]})}
what’s with the 1
in :in [:x 1]
?
I can’t figure out why that would be part of the path
I’m not following 😢 I’m not even sure how to ask for clarification
I’m thinking of :in
as something you could pass to get-in
yeah... it almost seems like a bug? but I think at some point the keypair [:x 0] was checked against the predicates, and in that temporary tuple the number 0 is in position 1
it’s element 1 of a map-entry composed of a key and a val
yeah that makes sense
aaaah ok so if the problem were with the keyword
then it’d probably be :in [:x 0]
ok there we are
(s/explain-data (s/map-of keyword? keyword?) {0 :x})
#:clojure.spec{:problems ({:path [0], :pred keyword?, :val 0, :via [], :in [0 0]})}
interesting that it indexes the hash map as if it was ordered key/val tupls
weird actually
oh, right
ahh, it’s not get-in it’s find-in
ultimately I’d like be able to use explain-data
and get-in
to get some value from the value I’m trying to conform… is there some way to do that?
(if such a thing existed)
I actually want the parent value
haha, find-in is impossible because (find x 1) has a two element vector fixed point
well, not impossible, but useless
peregrine.circle=> (defn find-in [m [k & nks :as ks]] (if (empty? ks) m (find-in (find m k) nks)))
#'peregrine.circle/find-in
peregrine.circle=> (find-in {:a {{:b 0} 1}} [:a])
[:a {{:b 0} 1}]
peregrine.circle=> (find-in {:a {{:b 0} 1}} [:a 1])
[1 {{:b 0} 1}]
peregrine.circle=> (find-in {:a {{:b 0} 1}} [:a 1 1])
[1 {{:b 0} 1}]
peregrine.circle=> (find-in {:a {{:b 0} 1}} [:a 1 1 1])
[1 {{:b 0} 1}]
it looks to me like you could write a version of get-in
that uses find
for maps and get
for other colls
yes, that would resolve the issue
for some reason I found the problem of using find recursively amusing
and then for @nonrecursive's use case, just call my-get-in
on (butlast (:in explain-data))
I can imagine a function that would do the thing, but why would I use find
?
the output looked like find’s output
perhaps I was mistaken
aaaaaah ok
I didn’t realize that, that’s awesome
thanks!
(defn get-in-spec-path
[val spec-path]
(if (empty? spec-path)
val
(get-in-spec-path
((if (map? val) find get) val (first spec-path))
(rest spec-path))))
thanks @bfabry and @noisesmith!
Can anyone tell me when a map switches from PersistentArrayMap to PersistentTreeMap in the case where it's heavily nested? I know normally it's >8 elements (which doesn't entirely make sense to me when switching to a 32-ary trie), but I'm working with a map that nests many levels and almost never has that many on the top one. It exceeds eight elements in the first level of nesting, but I wouldn't expect any implementation to be counting elements due to it possibly taking linear time. From playing around with it, I've found it switches when it reaches >8 levels of nesting, although I haven't tested to see if this is true with empty maps. Can anyone confirm and/or shed light on this? Thanks
the only checks are within the implementation of pam, and they only kick in if you assoc more than HASHTABLE_THRESHOLD, where HASHTABLE_THRESHOLD is 16. since the array map stores items in pairs 16 / 2 = 8
user=> (into (new clojure.lang.PersistentTreeMap) {:a 1 :b 2})
{:a 1, :b 2}
user=> (type *1)
clojure.lang.PersistentTreeMap
user=> (assoc *2 :c 3)
{:a 1, :b 2, :c 3}
user=> (type *1)
clojure.lang.PersistentTreeMap
@hiredman with regards to your first comment, if that was the case I'd expect a top level map map holding three maps with >8 kv pairs to always be a PAM and that's not the behavior I'm seeing once the nested maps reach a certain level of depth
it has nothing to do with nested totals, it’s the number of elements at that one level
the code is pretty clear, that is what it does, if you are seeing something else, then you are doing something else
https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/PersistentArrayMap.java
user=> (type {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8})
clojure.lang.PersistentArrayMap
user=> (type {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9})
clojure.lang.PersistentHashMap
I'm also using an int-map
although I'm pretty sure it implements the same semantics regarding that
what is an int-map?
would suspect https://github.com/clojure/data.int-map
based on what evidence?
the only mechanism that switches from pams to phms at a threshold is built in to pams, and hard coded to result in a phm
if you don't start with a pam, or you don't end up with a phm, then you are looking at something entirely and completely unreleated
Ah, ok. So I was mistaken about PersistentIntMap
. It stays the same (which makes me wonder whether it's okay to reuse the same transient
with it?). But I do see the behavior I was mentioning wrt to regular hash-maps. I'm going to examine the latter a bit further to make sure I'm not missing something.
why does the hashmap / arraymap distinction matter here anyway?
my guess is you are running some other bit of code you are failing to mention which is resulting in maps being transformed
I was under the assumption the reason it's advised to never reuse transients is because of the switching of coll types as they grow
it accidentally coincides - because the problem hits when reallocs / resize happen inside the map coll impl
which is one thing that will happen with the arraymap hashmap transition
but the real problem is not using the return value of transient ops
@noisesmith thanks for clearing that up. so even inside the same coll type it can create problems? Or are you saying there are additional reasons on top of that?
yes - because every persistent type eventually needs to alloc more space, and that’s when bashing transients in place tends to stop accidentally seeming correct
there might be other gotchas too, you avoid all of them when you use the return value like the docs tell you to
@joshjones in general, I would suggest not using something unless you can make a cogent argument for using it
@tbaldridge hi , did you published any other video on Pedestal series ?
@hiredman what do you think about creating a custom data structure, and to maintain persistent immutability, implementing functions that take the immutable structure, make it transient, modify it, and then return a persistent version? I'm doing this now. The transients guide seems to indicate that this is the primary use case for them. thoughts?
since going to and from a transient structure is O(1) both ways, it seems worthwhile
most people don't read the docs, don't understand the rationale, and just want a mutable collection with clojure magic sprinkled on it
here are some methods that look like a mutable collection, I'll reify that interface
@sophiago on the other hand, likes to live dangerously sometimes, iirc 😉 so you never know what she's up to 😄
I wasn't referring to anything odder than what @joshjones said above
I have a question about the performance of calling Java funcgtions. I'm seeing really poor performance when calling a simple csv parsing library. Specifically I'm trying to reproduce the results here: https://github.com/uniVocity/csv-parsers-comparison In particular I'm calling uniVocity CSV parser. On my 2016 Macbook Pro I'm getting timings that are almost 10x slower than they should be. Behold:
(time (count (.parseAll (CsvParser. (CsvParserSettings. )) (io/reader "/tmp/csv-parsers-comparison/src/main/resources/worldcitiespop.txt"))))
returns:
"Elapsed time: 19995.803153 msecs"
3173959
It varies but the fastest I saw it was about 10 secondswhat's going on?
am I doing something dumb in the way I'm calling the java code?
I ran the benchmark on my laptop and it indeed parses the 3million line CSV in less than 2 seconds.
Anyway, I'm trying to test my claim about nested maps switching to PHMs but am running into the whole "nested coll problem" (my phrasing). I think I knew how to work around stuff like this at one point, but forgot. Essentially these maps are way too large for me to even analyze visually after dumping into a text file, so I'd like to strip away everything that's not in the top level. It would be enough to filter for collections and then empty then out.
@husain.mohssen all the time there is in the java library, which is going to have nothing to do with clojure. my guess is you are doing something slightly different from what the benchmark is doing, so you should double check
(map #(if (coll? %) (empty %) %) my-map)
reveals I was wrong, as expected from all the comments and my code appears to be leaving one more kv pair at the top level for every time I nest another level.
either you aren't setting the same options, or you are doing more process before or after invoking the java
@sophiago why would any key/value pair in a hash-map ever be empty?
I know people do it, and it has been great for spectr that people do use these big nested structures
because that’s what that code is mapping over, is key/value pairs (if my-map is a map)
@noisesmith it's not testing for empty. It's setting them to empty so I can see what's going on.
@sophiago correcting myself: why would any map entry not be a coll?
according to clojure at least
interesting that empty on a map-entry is nil
but your code doesn’t check any of that
But it should return an empty coll of the type it's called on. I assume that's what you're questioning?
right, but I guess an empty map-entry is invalid, and the vector -> map-entry implicit coercion is one way