Fork me on GitHub

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, 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.


I can write it using split-with but it’s a bit ugly.


@korny it has been proposed to add take-until to core:


you could borrow the implementation from the patch in JIRA


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?


names are not wrapped by a Var around them so no indirection required


Why don’t expressions like (def now (LocalDateTime/now)) automatically add the type metadata though?


because Vars are mutable and might be altered to some other type?


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)
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)
user=> (alter-var-root #'now (constantly "aaaa"))
user=> (f)
IllegalArgumentException No matching method found

👍 16

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?


@reborg Another way to get the behavior I want is ^:const


which is actually what I intended, since this is a constant


beware of using ^:const for anything other than printable clojure values


@borkdude not sure what behaviour you need, but yeah, large ^:const objects can produce huge .class files


@reborg I mean to prevent reflection


can you post a snippet? It should throw if you const on something like a date


it’s not just about producing huge class files, they might even cause compilation to fail in certain cases


I might revert my last commit then. Where can I read more about this?


I don’t think there’s any doc on :const


so only use :const for “small” things like numbers, strings? why not date?


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


@reborg are you using java.time.LocalDateTime?


I am indeed


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))”


works for me


usage time is the issue


not definition


Try to use it in an expression


clj -e "(do (def ^:const d (java.time.LocalDateTime/now)) (fn [] d))"


$ clj -e “(def ^:const d (java.time.LocalDateTime/now)) d”
#object[java.time.LocalDateTime 0x52815fa3 “2018-05-22T14:59:16.834"]


that’s not even good enough


the compiler will evaluate d in interpreter mode there


ok, I see the issue


I’ll use a type hint 🙂


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 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?


@fabrao I think this is more suited for the #off-topic channel


ok, sorry about that


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:


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


“im a macro that takes a binding, option, & body OR a binding, & body”


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:

👍 4

@lwhorton Parsing stuff like that is one of the usecases for spec, btw, (s/cat :options (s/? map?) :body (s/* any?))


@pyr We have been using Sentry for over a year now, very pleased. Also using your lib.

❤️ 4

Hey, all. Anyone know of where I can get some parens stickers? I'm fresh out and need to deck out my laptop.


(Actually, what's worse, is I have one left. Can't leave myself hanging!)


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"

👍 4

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}


I need the keys in earlier if the keys are similar


that's what merge already does

👍 4

unless you really mean "earlier" as in the order of the map? (can't really be done without a different datatype)


thanks merge-with worked