This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-12-28
Channels
- # ai (1)
- # beginners (190)
- # boot (24)
- # cider (43)
- # cljsjs (3)
- # cljsrn (29)
- # clojars (6)
- # clojure (310)
- # clojure-dev (6)
- # clojure-nl (6)
- # clojure-russia (11)
- # clojure-spec (66)
- # clojure-uk (95)
- # clojurescript (103)
- # clojurewerkz (2)
- # core-async (9)
- # cursive (4)
- # datomic (5)
- # hoplon (163)
- # lein-figwheel (52)
- # off-topic (6)
- # om (6)
- # onyx (42)
- # perun (8)
- # re-frame (16)
- # reagent (10)
- # ring (7)
- # ring-swagger (1)
- # rum (1)
- # slack-help (2)
- # uncomplicate (1)
- # untangled (80)
(cond p1 e1 p2 e2 p3 e3 .. ); is there a "cond let" where the let is over the p1/e1, then another one over p2/e2, then a third over the p3/e3 ?
basically there is some sub expressions that I need to calcualte for both p1 and e1, for both p2 and e2, for both p3 and e3
yeah I can't think of anything that does that, also worth pointing out cond
only chooses a single branch
when it's eval ing the predicates, I'd like to 'cache' some of the intermediate computations
cond evals predicates until one is true. oh you want the value of the predicate result, not the expression?
it's just "if the predicate is true, when evaling the expression, there's stuff that is cached"
so I'm (attempgint to) destructure the argument, then eval the predate, then, if it's true, eval the expressioni
you could use plumatic's graph library http://plumatic.github.io/prismatics-graph-at-strange-loop/ to cache intermediate computations only if they're used
(my-cond [:rectangle a b] (< a b) (* a b) [:rectangle a b] (> a b) (+ a b) [:circle r] true (* pi r r))
so I have a thing where there's three parts, a "destructure", a condition, and an expression
if the destructure matches, then eval the condition, and if the condition is true, eval the expression
now, the bindings from the destructure needs to be "over" both the pred and the expr for them to make any sense at all
ah yeah ok. no there's nothing like that in core. destructurings are assumed to succeed. perhaps core.match has something but I've never used it
from http://clojure.org/reference/reader > The form following #_ is completely skipped by the reader. This makes it sound like invalid forms are ok?
hi guys, I'm looking for a good clojure client for google datastore, is there any recommendation ?
Okay, what's the right way to handle complex and optional arguments in Clojure? I've got a function that takes a shift
(as an integer ID), an optional count of that shift (defaulting to 1
), and the possibility of the string of shifts being followed by one or more of another shift (which could be any shift ID, a seq of IDs, or a shift-shorthand keyword (`:other`, :out
or :off
). What's the clearest and most straightforward way to do this?
@seylerius, I think you're looking for multi-arity
and variadic
functions
@seylerius After the mandatory inputs, take a map as an input that can contain any extra optional arguments. Do your logic to interpret that map. If it's really complicated to analyze the map, you can define a spec for the map and that will help with figuring out which of several cases the map conforms to.
Hmm... Yeah, map destructuring might do the trick, especially with type hinting or specs.
Except map destructuring doesn't do conditional arguments. Damnit. I was hoping I wouldn't have to just kwargs
does anyone have experience running elastisch against up-to-date elasticsearch versions?
current releases of elastisch only supports es 1.7 - what should I use to work with elasticsearch 5? build my own jar of elastisch master branch or use something else?
does anyone knows how to preallocate a hashmap with a given size? equivalent of new HashMap<String, Object>(1000000);
@mping There isn't a way, and given the nature of persistent hash maps there's little reason to do so. But what are you trying to accomplish?
@tbaldridge I’m trying to optimize a clojure fn
I have just published a new architecture that allows you to modularize a system into services and run it in the development environment as a single project without restarting the REPL: https://medium.com/@joakimtengstrand/the-micro-monolith-architecture-d135d9cafbe#.6bi8wpstd
@joost-diepenmaat imho the best option atm is to use the new rest client they provide
Its quite simple and fast. We started wrapping it for clj before xmas but didnt use/test it yet.
@joost-diepenmaat Yes, I’m using elastisch 3.0.0-beta1 on our production servers, connecting to elasticsearch 2.3 running on aws. Version 5.0, no, but 2.3.x is the most stable and established right now anyway.
The article that motivated us to give it a try: https://www.elastic.co/blog/benchmarking-rest-client-transport-client
Yeah, I use the rest API through elastisch, not the native transport — can’t use 5.0 anyway yet, and did not want to write a clojure API to wrap a java API, since one already exists (elastisch)
There’s no way to enumerate resources, right? Especially not if they’re in jar files? (I know about the indirect route of listing jar files, and then listing files in them, and extracting the filenames that way)
I think you probably can
not sure if it's possible to have multiple resource paths tho, which would make it brittle
@gfredericks by listing things on classpath and then parsing the jar files, or some other thing
@lvh is "listing things on classpath" different from "enumerating resources"?
if you're hoping there's a way to differentiate "resources" from "source code" then I don't think that will work
@gfredericks no; I don’t think I particularly care about that
I can check that the paths have a particular shape; e.g. I know they’ll match a certain regex
any of the namespace discovery tools should have solved this problem
@mping got some source we can look at?
the documentation for the ignore next form, from states http://clojure.org/reference/reader > Ignore next form (#_) > The form following #_ is completely skipped by the reader. (This is a more complete removal than the comment macro which yields nil).
but i'm not sure if lein repl is ignoring it or if it is correctly passing stuff to nrepl and the guts of clojure and there the reader is ignoring it
and this is coming up because someone is doing #_ (defn foo [x] ::ns/blah)
and it is throwing a reader error
@dpsutton yes, it will be "ignored"
I say "ignored" since the invalid syntax of the defn is only invalid at compile time, not reader time
This however will cause an error #_(]
right. That will throw assuming the that symbol is valid
the code behind #_ is literally:
data = rdr.read()
return nil
https://github.com/yogthos/json-html is wonderful, but is there a version that allows you to collapse/expand elements?
and I couldn't find in their code where they dictate this, but I was assuming they were just not sending it into nrepl
so in clojure 1.8, where ::
is not defined, #_ (defn foo [x] ::ns/symbol)
should throw an error?
@pesterhazy take a look at https://github.com/Odinodin/data-frisk-reagent or https://github.com/flexsurfer/re-frisk if you’re using reagent/re-frame
@dpsutton ::
has been around for years way before 1.8
@yogthos I'm using reagent (and data-frisk), but for a different project
my use case is actually from clojure code
super simple other than that
@dpsutton yep ::
on keywords has existed since 2009: https://github.com/clojure/clojure/blame/master/src/jvm/clojure/lang/LispReader.java#L407
I use edn->html all the time by the way, @yogthos, thanks so much for that
@pesterhazy you’re welcome 🙂
I suppose I could build something similar using data-fristk
hm....
thanks @tbaldridge
so, if a namespace ns
does not exist, should the following code throw a reader error or not?
I'm trying to figure out if we've got a serialization problem in CIDER's bencode or if the error is expected
@dpsutton right, where you are reading that code, "ns" would need to exist
but it's even worse. Whatever namespace is reading that keyword, would need to have a compiler alias for that namespace. That code I linked above shows that the reader calls into the compiler.
yes. In short, ::ns/symbol shouldn't be used outside of the context of compiling.
ok, so the issue raised at https://github.com/clojure-emacs/cider/issues/1890 is not an issue with CIDER?
But I'm not even suer why CIDER is doing anything with this at all. It should be moving strings around, not data, or if it does move data it should be moving fully qualified data.
but why encode it at all, why not just ship raw strings over, that's the thing I don't get.
and why i was hoping that an error was to be expected, since we didn't seem to be touching anything at all
in clojure, am I free to use symbol/variable names that start with , like a, _b, or are they reserved?
you can even use __-_-____---
i'm currently using print-methods to define writer syntax for several records, but when i nest records it displays the inner ones as object code. is this just not possible to do this?
@tbaldridge are you planning to get back to pixie at some point?
if i do that i get: ArityException Wrong number of args (3) passed to: core/fn--7124/fn--7125 clojure.lang.AFn.throwArity (AFn.java:429)
(.write w (print-method (:real v) w))
(.write w " + i")
(.write w (print-method (:imag v) w))
@sophiago You can yes, I'd say it's more idiomatic than first stringing together pieces and putting it into the writer. The writer already does the stringing together efficiently
ok, i think i'm almost there... the output for the same example now looks like this: 1null" + i"null1nullnull"/"null2null
err actually i was just getting the quotes because of an extraneous call to print-method when i'm just writing the strings
So the print-method writes into the writer, you don't need to deal with converting it to a string, the recursive call will do that for you
ah ok. thanks. this is nearly undocumented so i had to ask someone on here to begin with and seems got it slightly off
@sophiago Yes that's true. A good idea can give you the usage of it in clojure's own code
somewhat tangentially related...i've been trying to write specs for some protocol functions that use the same records and am having trouble creating generators for the arguments i pass to their constructors. i have to use s/with-gen
and gen/fmap
, similarly undocumented as of yet, so i just figured out the syntax last week. but i'm getting various errors, even when the arguments are primitives. i'll throw a paste up
Might want to take it to #clojure-spec , but I don’t think you understand s/gen
.. this function retrieves the generator for a spec. You are doing (s/gen Rational)
, and Rational
is not a spec.
type (s/gen number?)
and you get the generator for the (coerced) spec given by the predicate number?
. type (s/gen Rational)
and you get an error telling you it cannot construct the generator. yes?
fmap
says: give me a generator, generate some values, apply a function to those values, and make this output also a generator
that makes sense, but then what about the ones where i'm only generating primitive numbers?
and then from there i should be able to make a generator from that definition for the others, right?
you mean to pass two arguments: a function that modifies two rational numbers, and then a generator. for a generator like this, gen/tuple
may be helpful
well, no. for that one Rational is a record that takes two arguments and they're both just numbers. but i suppose same idea?
(gen/fmap (fn [[...]] ...) (gen/tuple ...))
is pretty useful
in the next test.check release it'll be easier to do that via (gen/let {...} ...)
sophiago, consider:
(defrecord Rational [x y])
(gen/generate
(gen/fmap
#(->Rational (first %) (second %))
(gen/tuple (s/gen int?) (s/gen int?))))
you’re not giving (s/gen number?)
arguments to ->Rational
, you’re giving them to fmap
so you want:
(s/def ::rational
(s/with-gen #(instance? Rational %)
(fn [] (gen/fmap #(->Rational (first %) (second %))
(gen/tuple
(s/gen number?)
(s/gen number?))))))
=> :user/rational
(gen/generate (s/gen ::rational))
=> #user.Rational{:x 1651113, :y 81.4404296875}
yes. (s/gen ::rational)
gives you the generator for the spec defined by ::rational
. (you can always test whether you have a Generator
by doing a (type …)
on it, or by calling clojure.test.check.generators/generator?
on it).
Given the generator, you can give that to fmap
as you’re doing here to apply a function to it again, or you can add constraints to it with such-that
, etc. (alex’s list of combinators for generators is: bind cat choose elements fmap list map not-empty one-of return set such-that tuple vector vector-distinct)
so lastly i'm just confused about how to go about speccing out the actual functions with these
i was under the assumption i passed generators for the args and exercise doesn't really check the return values unless i provide it with a function to. so like this:
(s/fdef add
:args (s/cat :a (s/gen ::rational)
:b (s/gen ::rational))
:ret (s/gen ::rational))
(s/exercise-fn `add)
stest/instrument
checks :args
only. stest/check
generates args, and checks your :ret
and :fn
.
(defn mul
[^Rational a ^Rational b]
(->Rational
(*’ (:x a) (:x b))
(*' (:y a) (:y b))))
(s/fdef mul
:args (s/cat :a ::rational :b ::rational)
:ret ::rational)
(stest/summarize-results (stest/check `mul))
{:sym user/mul}
=> {:total 1, :check-passed 1}
also, If I recal correctly, you actually pass a spec for both :args
and :ret
. The args spec should have a generator associated with it, but you don't pass the generator itself.
right, your example is good. I was speaking to @sophiago original example of the similar spec
ok, i think i got it. i wanted s/exercise-fn
though. but now i'm getting divide by zero errors...which is good i suppose because i didn't even think about checking for that 😛
Well, exercise-fn
should work great now too. Curious what you will use the result for. On the divide by zero error, congrats, spec is already doing its job! :thumbsup::skin-tone-2:
but curiously, i'm feeding it ints right now and they're only between -1 and 1. whereas if i use gen/sample
i get a full range
well, stest/check
will generate by default 1000 arguments and test your :ret
and :fn
also. can specify more or fewer than 1000. it is very exhuastive, and requires no manual inspection
exercise-fn
does not check :ret
and :fn
, so it’s only useful for actually running the function
well i figured actually eyeballing the output would help having already determined it was far too complex to write fns to match the args to return values
i figured exercise-fn
would use gen-sample
to produce the args...but it seems almost as if with ints it's going through them sequentially?
Is there a built in parser for the result of calling str
on an instant? For example,
(str #inst"2015-06-01T07:05:27.000-00:00")
=> "Mon Jun 01 00:05:27 PDT 2015”
I want to parse "Mon Jun 01 00:05:27 PDT 2015”
back into an instant.@sophiago you might consider adding a constructor function to prevent zero from ever being the denominator https://stuartsierra.com/2015/05/17/clojure-record-constructors
as far as i see it, it really only matters in terms of complicating my ability to spec it out... otherwise it's up to the user to know not to divide by zero. none of the functions would output that
other than that, the only last thing i was going to ask about this is creating lists of generators to have it randomly sample from. i assumed i could just do it like this, but am getting a compile time error:
(s/def ::sym (s/or ::rational
::rational-rational
::rational-complex-numer
::rational-complex-denom
::rational-complex
::complex
::complex-rational-real
::complex-rational-imag
::complex-rational))
I think s/or
needs each option to be labeled with a keyword so that s/conform
can tell you which spec it matched
So something like:
(s/def ::sym (s/or :rational ::rational
:rational-rational ::rational-rational
...
))
right. that does seem to be what the compiler is telling me. i think having the double bar for specs was a bit confusing to read over in the guide. even just now i thought that wasn't necessary
or wait...what do we call the double colon? i went to a talk by rich in november on spec and he was referencing it a bunch
all specs must be namespace-qualified, and ::
in the guide is to save space while still showing that
it’s resolves keywords to the current namespace so that are fully qualified, and also expands aliases. so
(alias 'cstc 'clojure.spec.test.check)
=> nil
::cstc/my-kw
=> :clojure.spec.test.check/my-kw
i just meant how he was verbally referring to it. i could just look up the talk on youtube
regarding a non-zero number generator, here’s one:
(def non-zero-number-gen
(gen/such-that (complement zero?)
(s/gen number?)))
another option is instead of number?
in the above, use this, which eliminates values you wouldn’t want such as +/-`Infinity` and NaN
:
(s/double-in :infinite? true :NaN? false)
technically it'll take any type of numbers, but i just made it that way because clojure ints are really longs
ok — well then you have an easy non-zero-number-gen above (just replace (s/gen number?)
with (s/gen int?)
and you’re good to go)
sorry, one more thing...i'm realizing gen/tuple
will just output a list if you feed it more than two generators (so not really accurately named), but i get an error trying to add something like (third %) to a variadic type constructor
gen/tuple
yields a generator which will output a single vector, with the contents being the generated values of the generators used to compose it
i think you get it, but just to be clear:
(gen/generate
(gen/tuple
(gen/char-alpha)
(gen/string-alphanumeric)
(gen/boolean)
(gen/char-alphanumeric)))
=> [\A "Ux4FZWxIQzRPRadK8t1shzYZA96i3" false \2]
(nth *1 3)
=> \2
your non-zero generator doesn't seem to be working, though... not sure why. the code seems correct
sorry, should be:
(def non-zero-number-gen
(gen/such-that (complement zero?)
(s/gen int?)))
my constructor is like this: (gen/fmap #(->Poly 'x '((5 (nth % 0)) (4 (nth % 1)) (3 (nth % 2)) (0 (nth % 3))))
so the second arg is a list of tuples where the first number i'd like to be a constant
and with my print-method
i get: x:((5 (nth p1__10256# 0)) (4 (nth p1__10256# 1)) (3 (nth p1__10256# 2)) (0 (nth p1__10256# 3)))
i have no idea what you’re doing there — your parens do not match in what you pasted either (missing a )
)
well, it's just printing the actual (nth x) with some object code in between. i'd prefer all those to be actual instances produced by my generators
I see what you're getting at. Your proximate issue is that you have quoted that whole list, so the calls to nth
aren't actually happening. Maybe use vectors instead of a quoted list of lists
to think...i figured speccing this project would be an easy way to learn spec...like it's actually organized in a way i'm able to manually debug
anyway, thanks for all your help @joshjones and @geoffs!
nope, there's no equivalent to ruby's find
in core. presumably because (first (filter is not particularly less readable
I suppose because of chunking you could have a situation where that's less efficient.. maybe. but in that case you could use the transducer version instead
@lvh @bfabry fyi here’s a recent ticket on Clojure’s JIRA to add that function to core: http://dev.clojure.org/jira/browse/CLJ-2056. You can vote on it if you’d like.
I believe you have to sign the contributor agreement and set up a JIRA account, but after that you can vote on a ticket.
Is there a transducer that captures applying a transducer to each element of a collection?
so you have a collection of collections?
or...a collection of reduction functions...?
I had a bunch of maps and mapcats; and then I decided that I wanted them grouped by some intermediate value fairly early in the chain
Now I have
(def ^:private json-models
"All of the JSON model files in the AWS SDKs."
(into
{}
(comp
(map (fn [fn-var] (-> fn-var meta :amazonica/client)))
(map (juxt identity src-location))
(filter (comp second cp/jar-file?))
(distinct)
(map (fn [[client location]]
[client
(->> location io/file JarFile. cp/filenames-in-jar
(filter (partial re-matches #"models/(\w+)-\d{4}-\d{2}-\d{2}-model.json"))
(map (comp json/decode-stream io/reader io/resource))
(map #(get % "shapes")))])))
safe-aws-fns))
something like that would be my guess
but I don't have a transducer certificate or anything like that