This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-05-22
Channels
- # architecture (1)
- # aws (3)
- # beginners (78)
- # boot (33)
- # cider (49)
- # cljs-dev (3)
- # clojure (82)
- # clojure-berlin (2)
- # clojure-dusseldorf (14)
- # clojure-gamedev (75)
- # clojure-italy (15)
- # clojure-nl (2)
- # clojure-poland (9)
- # clojure-russia (1)
- # clojure-spec (11)
- # clojure-uk (91)
- # clojurescript (17)
- # core-async (2)
- # cursive (1)
- # data-science (3)
- # datascript (34)
- # datomic (13)
- # docs (2)
- # duct (32)
- # emacs (8)
- # fulcro (95)
- # instaparse (17)
- # jobs (2)
- # jobs-discuss (1)
- # jobs-rus (4)
- # leiningen (1)
- # luminus (1)
- # lumo (4)
- # mount (1)
- # nrepl (1)
- # off-topic (98)
- # onyx (13)
- # portkey (12)
- # re-frame (10)
- # reagent (11)
- # remote-jobs (4)
- # rum (3)
- # shadow-cljs (34)
- # specter (7)
- # sql (1)
- # tools-deps (8)
hello, is there a way to handle unencodable values during a transit write? my case is that I'm sending a lot of arbitrary data encoded in transit, but sometimes there pieces on it that transit can't encode (like some react classes), the problem is that currently the entire encoding fails when that happens, so I wonder if there is a way to efficiently catch that transit can't encode a value and handle it by returning something else (maybe just a placeholder like "cant encode", in my case is ok to drop those values, it's more important that the rest goes though). is there a way to do that with transit?
You may be happiest either writing custom encoders for your unencodable types (that presumably would simply note the type) or pre-processing the data before encoding to remove such types. If the former route interests, http://blog.klipse.tech/clojure/2016/09/22/transit-clojure-2.html may be of help
thanks @U04V4HWQ4, I understand that, the problem is about performance. the data structures I'm sending can get quite big (they are client-side fulcro app states, they can get very large), so if I walk the structure is already a bad thing for me, also the only reliable way to detect that I can't encode something is trying by trying to encode it. most of the times I don't have any bad data, so doesn't worth trying to sanitise every time, what I'm currently doing is putting the encode in a try/catch, and when it fails I encode again but running the sanitiser first. it works, but I end up having to encode everything 3+ times (considering post-walk will encode multiple times as it walks), this could be much more efficient if transit provided some hook for the user to handle cases where it can't encode something, do you think is there something like that available?
why not define a custom encoder for each unencodable type? if you run into more than a few that sounds like a design problem
@U051SS2EU it's out of my control, I'm creating a developer tool, I can't predict everything that might happen in that state, it's user controlled.
Then why not document the types it accepts? You could even define a container type they can keep in the state to hold things that can't be encoded, and wire a serializer that ignores its contents
that could be, but why should we require that? wouldn't be possible for transit to provide a escape callback for those? this would make a much better interface that doesn't require any adaptation, if I could just use the pr-str
of the value instead of the original that would solve the problem
Same problem though, what if the type doesn’t support pr-str
@U07TDTQNL then we could have a default fallback, the main point is not getting a good rep of it, is just not breaking the encode of a big value because a few items can't be encoded, makes sense?
this is interesting speculation, but transit lacks any such feature
hey folks - is there an easy way to do take-while
but also return the first non-matching sequence entry? Kind of a take-until
?
e.g. (take-until even? [1 1 3 4 5 6])
would return (1 1 3 4)
- stopping at the first even entry.
@korny it has been proposed to add take-until
to core: https://dev.clojure.org/jira/browse/CLJ-1451
handy 🙂 thanks! That also pointed me to halt-when
: (defn take-until [p s] (transduce (halt-when p (fn [r h] (conj r h))) conj [] s))
Why do I have to provide the type hint on the var here, but not inside a let to prevent reflection?
(def dtf (DateTimeFormatter/ofPattern "yyyy-MM-dd"))
(def ^LocalDateTime now (LocalDateTime/now))
(.format now dtf)
(let [now (LocalDateTime/now)] (.format now dtf))
@borkdude when (.format now dtf)
is compiled into a class, it gets access to the Var from an instance variable to invoke with getRawRoot()
, which returns an Object. The info available at that point (with no type hints) is: I have an Object, I need to call "format" on it, then use reflection. If you attach a type hint to the var (or during the call with (.format ^LocalDateTime now dtf)
) the object from the var can be cast to LocalDateTime
before .format
call, hence no reflection. In the let
block there is no var access and the compiled class has all the info it needs straight away.
ok, because in the let block the names are just compiled away to one direct expression?
Why don’t expressions like (def now (LocalDateTime/now))
automatically add the type metadata though?
Yes, the Var can be redefined anytime and the object in it can change to a different type. All functions compiled at that point would have a bogus cast. You can actually see that in action comparing the error messages with and without type hinting:
user=> (def ^LocalDateTime now (LocalDateTime/now))
user=> (def f #(.format now dtf))
user=> (f)
"2018-05-22"
user=> (alter-var-root #'now (constantly "aaaa"))
user=> (f)
ClassCastException java.lang.String cannot be cast to java.time.LocalDateTime
user=> (def now (LocalDateTime/now))
user=> (def f #(.format now dtf))
user=> (f)
"2018-05-22"
user=> (alter-var-root #'now (constantly "aaaa"))
user=> (f)
IllegalArgumentException No matching method found
in clojure.spec, is there a way to define a map spec where unqualified key has a different spec than that of the qualified keyword? ie. say I want to have, in one ns, specs for 2 entities (maps), each having a status
key, each with different spec
lein removes the stack trace after some steps and says n items more. Is there a way to show the complete stack trace in lein run or other commands?
@troglotit thanks, I found out thanks to this article https://spin.atomicobject.com/2017/06/27/clojure-spec-json-data/
@borkdude not sure what behaviour you need, but yeah, large ^:const objects can produce huge .class files
it’s not just about producing huge class files, they might even cause compilation to fail in certain cases
date could be ok, but for it throws for me if I define it const, because there is no print-dup override for printing it. So wondering why it's working for you
no, that’s exactly what I’m talking about, :const
is only for things that the compiler can roundtrip from value to string and from string to value again
You must have an override of print-dup
somewhere, but yeah, all to avoid a type hint? Probably not worth it
$ clj -e “(def ^:const d (java.time.LocalDateTime/now)) d”
#’user/d
#object[java.time.LocalDateTime 0x52815fa3 “2018-05-22T14:59:16.834"]
exactly, if you close it in a function def, it can't generate the class because it doesn't know how to convert it into a string
Hello all, I´m starting a project that it has to be scalabe itself. I´m researching about BaaS providers and I saw some that closed already, even http://parse.com from Facebook. I saw others that is "vendor lock-in", and others that is limited by APIs for 3rd party integration libs. So, is it worth to use BaaS? This project will integrate with credit cards payments, chatbots, machine learning, file storing, and so one ... Could anyone give me an advice what starting way do I have to go throuth?
Hey! If you run backend code, I strongly suggest looking at Sentry to centralize error reports, it's available either self-hosted or as a service. We rely on it and we just updated our client library for it at: https://github.com/exoscale/raven
is there a way to integrate variadic & body
functions with a multy-arity definition ([a & b] ...) ([a b c] ...)
? Seems fishy, but i’m not sure how else to get at a macro definition i’m trying to write
i guess i could sniff the body, but that seemed wonkier to me:
(let [options? (map? (first body))
opt (if options? (first body) {})]
...
@lwhorton You'll need to do the wonkier option. You can document the intended way to call the macro by using :arglists
such as done here: https://github.com/clojure/clojure/blob/08e592f4decbaa08de570ded9ac169785b1608f9/src/clj/clojure/core.clj#L449
@lwhorton Parsing stuff like that is one of the usecases for spec, btw, (s/cat :options (s/? map?) :body (s/* any?))
+1 (use s/conform)
@pyr We have been using Sentry for over a year now, very pleased. Also using your lib.
Hey, all. Anyone know of where I can get some parens stickers? I'm fresh out and need to deck out my laptop.
hello guys how do I merge two maps and take the key in the source if the key exists in both maps. merge take the key in the source if the keys are similar
I am not sure I understand your question, but you might want merge-with
You can merge them in the other order, or write your own variant of merge.
Oh yeah, merge-with could be used to prefer keeping the value in the earlier map, rather than preferring keeping the value in the later map.
I think you understood the question better than I and my answer coincidentally applies
I am answering the question as if the last sentence was changed to "merge take the key in the later map if the keys are similar"
merge behavior: (merge {:a 1 :b 2} {:c 3 :a -1})
=> {:a -1, :b 2, :c 3}
one possible merge-with behavior: (merge-with (fn [earlier later] earlier) {:a 1 :b 2} {:c 3 :a -1})
=> {:a 1, :b 2, :c 3}
unless you really mean "earlier" as in the order of the map? (can't really be done without a different datatype)