This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-06-02
Channels
- # admin-announcements (1)
- # alda (3)
- # beginners (28)
- # boot (155)
- # braid-chat (8)
- # cider (22)
- # cljs-dev (46)
- # cljsrn (24)
- # clojure (94)
- # clojure-belgium (23)
- # clojure-czech (2)
- # clojure-germany (7)
- # clojure-greece (12)
- # clojure-india (1)
- # clojure-japan (1)
- # clojure-russia (118)
- # clojure-spain (18)
- # clojure-spec (40)
- # clojure-sweden (19)
- # clojure-taiwan (4)
- # clojure-uk (5)
- # clojurescript (185)
- # clojutre (10)
- # community-development (5)
- # core-logic (11)
- # css (5)
- # cursive (7)
- # datomic (5)
- # dirac (6)
- # emacs (2)
- # euroclojure (1)
- # events (5)
- # hoplon (355)
- # instaparse (3)
- # jobs-rus (5)
- # lein-figwheel (2)
- # leiningen (5)
- # mount (11)
- # off-topic (8)
- # om (20)
- # onyx (10)
- # other-languages (87)
- # perun (9)
- # proton (1)
- # re-frame (21)
- # reagent (16)
- # specter (28)
- # tmp-json-parsing (5)
- # untangled (19)
- # yada (43)
Is there a way to specify jsonb operations in honeysql, or something similar? For example, can I represent this query? select * from my_table where json_col @> '{"type": "foo"}'
?
Is there a way to write a prismatic schema checker that checks if a map is a subset of a schema? Just make every key in the schema optional?
hey @danielcompton, something like a custom predicate?
(let [acceptable #{:key-1 :key-2}]
(s/validate (s/pred (fn [m]
(not-any? nil? (map acceptable (keys m)))))
{}))
=> {}
(let [acceptable #{:key-1 :key-2}]
(s/validate (s/pred (fn [m]
(not-any? nil? (map acceptable (keys m)))))
{:key-1 1}))
=> {:key-1 1}
(let [acceptable #{:key-1 :key-2}]
(s/validate (s/pred (fn [m]
(not-any? nil? (map acceptable (keys m)))))
{:key-1 1
:key-2 2}))
=> {:key-1 1, :key-2 2}
(let [acceptable #{:key-1 :key-2}]
(s/validate (s/pred (fn [m]
(not-any? nil? (map acceptable (keys m)))))
{:key-1 1
:key-2 2
:key-3 3}))
ExceptionInfo Value does not match schema: (not (user$eval5812/fn--5813 a-clojure.lang.PersistentArrayMap)) schema.core/validator/fn--2411 (core.clj:151)
(def HasKeys (s/pred (fn [m] (not-any? nil? (map #{:key-1 :key-2} (keys m))))))
=> #'user/HasKeys
(s/validate HasKeys {:key-1 1})
=> {:key-1 1}
(s/validate HasKeys {:key-1 1
:key-3 3})
ExceptionInfo Value does not match schema: (not (user/fn--5980 a-clojure.lang.PersistentArrayMap)) schema.core/validator/fn--2411 (core.clj:151)
Better, probably a terrible name though.
(def HasKeys (s/pred #(every? #{:key-1 :key-2} (keys %))))
@danielcompton: I wrote something like this in the past:
(defn validate-subset
[schema value]
(-> (select-keys schema (keys value))
(s/validate value)))
@d-t-w: thanks, I think @mpenet’s example is what I was after, as I want to validate the vals on the keys too
Thanks @mpenet!
bit late to the question but that's exactly what boot excels at/is made for.
to answer my own question @yogthos is compiling a catalogue of common clojure error messages so I will add it there https://github.com/yogthos/clojure-error-message-catalog
please support the effort!!
;;; so doall didn't trigger the exception here, because it's first
;;; level only. OK.
user> (def x (doall {:a {:b (cons 1 (take 10 (lazy-seq (repeat (/ 1 0)))))}}))
#'user/x
;;; Here the exception is also obvious. REPL is evaluating everything.
user> x
ArithmeticException Divide by zero clojure.lang.Numbers.divide (Numbers.java:158)
;;; What about this? After the first eval, x now doesn't throw an
;;; exception (cached value?). But how is the whole (take... ) form
;;; being replaced by nil?
user> x
{:a {:b (1)}}
user>
@bg You're right, lazy seq will cache the result after the first exception. The result is nil
. And (take 10 nil)
is the empty sequence
@rauh: can you point me to the source where that exception becomes nil upon evaluation? I couldn’t find the relevant part.
@bg not exactly caught, but you can understand what's going on here which is equivalent construct to your take (which again constructs a lazy-seq and thus a nested lazy seq):
The first run of the outer lazy-seq ONLY constructs the inner lazy seq and thus successfully returns and caches the value. Only printing the value on the repl will result in the full realization and the one time exception
The outer run, never throws and never sees the exception since the inner doesn't get evaluated until later.
Hello, I have this string :
port : 8888
load-balance-servers: 1
antispam-force-off : disable
antispam-cache : enable
antispam-cache-ttl : 6000
antispam-cache-mpercent: 2
antispam-license : Contract
antispam-expiration : Wed Dec 14 2016
antispam-timeout : 7
avquery-force-off : disable
avquery-cache : enable
avquery-cache-ttl : 1800
avquery-cache-mpercent: 2
avquery-license : Contract
avquery-expiration : Wed Dec 14 2016
avquery-timeout : 7
webfilter-force-off : disable
webfilter-cache : enable
webfilter-cache-ttl : 6000
webfilter-license : Contract
webfilter-expiration: Wed Dec 14 2016
webfilter-timeout : 15
webfilter-sdns-server-ip:
webfilter-sdns-server-port: 53
source-ip : 0.0.0.0
ddns-server-ip : 0.0.0.0
ddns-server-port : 443
How do I get all the *-expritation from all text using re-seq?I would probably try something like use clojure.string/split
to break it into a seq of lines, then a combination of filter
and re-matches
to get just the expiration values
Not actually using re-seq, but that's how I'd do it. I generally find using the clojure.string fns together with regex more readable than just one complicated regex, YMMV.
@seancorfield: What about something like this:
(mapv #(try
(run-scraper (:scraper scraper-data)
(:username %)
(:password %))
(catch Exception e (str "Error when parsing for " (:username %) " at " vendor)))
(:creds scraper-data))
ghadi: you probably know the answer to this one as well: when I create a record, the factory fn map->Foo contains ref to "user.Foo/create", where does this /create comes from?
nevermind: #object[java.lang.reflect.Method 0x159fa310 "public static user.Foo user.Foo.create(clojure.lang.IPersistentMap)"],
@mpenet: I believe that method is emitted during compilation, not defined in core, as it’s a static method on the generated class (`Object/method` is the interop syntax for invoking a static method)
mpenet: https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Compiler.java#L7955
@josh_tackett: What about it? Looks like it would work (but without knowing more about what the code does, hard to say).
@seancorfield: Yep it works well 🙂
Coincidentally, another question about records: suppose that I have a reference to a record class.
(defrecord foo [_])
(def foo-ref foo)
How can I construct an instance of the record? I imagined that (new foo-ref nil)
would work, but I get java.lang.IllegalArgumentException: Unable to resolve classname: foo-class
.Ah, sorry, should have been clearer: I don't actually have foo
at hand, only foo-ref
.
@cored http://www.braveclojure.com/ highly recommend
@eggsyntax: normally you would either import the actual record, or refer the constructor fn ->RecordName from the ns where the record is defined. is there some reason you can’t do that?
Ah, ok, wait. I think I see; my problem is that I'm defining foo-ref
to be the imported class instead the required constructor in the ns where I have to create an instance.
can anyone hint me how i can instantiate a Map<String, Object>
in clojure? I need that specific type.
hmm, I would think that {“foo” :anything-deriving-from-object}
or just an empty map would do the trick?
Would that match the type signature of a Java method that accepts a Map<String, Object>
?
It should since Clojure maps implement the Java Map interface. My (probably limited) understanding is that those type checks are done by the Java compiler and not the jvm runtime, so you don’t need to declare anything special from the Clojure side.
generics (or type parameters) exist in the java the language type system, but not on the jvm
Gotta love type erasure in generics… 🙂
Sorry if this is was in an obvious place, but I heard there was a proposed change to keyword syntax. (To make them more convenient to use with namespaces.) Does anyone know where I might read more on this?
it isn't so much a change to keyword syntax, as the addition of some map related syntax, that effects keyword literals in maps
Can someone explain why i must wright (map #(Character/toLowerCase %) str) but not just (map Character/toLowerCase str) ?
@ov Character/toLowerCase
is Java interop, not a function
Hello, is there any way to get all quoted elements in here:
(re-seq #"set member (.*)" "set member \"02-05-16\" \"06-05-16\" \"23-05-16\" \"30-05-2016\" \"HORARIO_CICLICO_TERCA_QUINTA\"")
?yeah, because of the way the clojure reader constructs regexes for regex lterals you need double the escaping