This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-02-14
Channels
- # beginners (19)
- # boot (11)
- # cider (59)
- # cljs-dev (292)
- # cljsrn (2)
- # clojure (121)
- # clojure-brasil (19)
- # clojure-canada (2)
- # clojure-france (2)
- # clojure-italy (57)
- # clojure-spec (54)
- # clojure-uk (20)
- # clojurescript (83)
- # core-async (20)
- # cursive (5)
- # datascript (2)
- # datomic (10)
- # duct (25)
- # editors (4)
- # emacs (2)
- # fulcro (5)
- # funcool (1)
- # graphql (2)
- # immutant (8)
- # java (1)
- # jobs (4)
- # jvm (1)
- # keechma (5)
- # luminus (10)
- # off-topic (113)
- # om (36)
- # onyx (11)
- # parinfer (55)
- # pedestal (7)
- # protorepl (28)
- # re-frame (25)
- # reagent (6)
- # ring-swagger (1)
- # shadow-cljs (113)
- # spacemacs (1)
- # specter (23)
- # unrepl (8)
- # yada (8)
is there a clojure builtin for binary-slurp for dealing with binary files (and returhing a byte []) ?
you can use Files.readAllBytes(Path)
that requires some into-array and such in clojure, but it’s minimal, it works, and it uses stuff built into the vm
@noisesmith: thanks; I've actually reailzed I should load it in chunks, as I'm sha-512-ing:
(defn sha-512 [fname]
(let [md (MessageDigest/getInstance "SHA-512")
ba (byte-array (* 1024 1024))
fis (q/io-fis fname)]
(loop []
(let [len (.read fis ba)]
(when (>= len 0)
(.digest md ba 0 len)
(recur))))
(seq (.digest md))))
user=> (java.nio.file.Files/readAllBytes (java.nio.file.Paths/get (java.net.URI. "file:///Users/justin/.profile")))
#object["[B" 0x531f4093 "[B@531f4093"]
speaking of which, the loop/when/recur looks very ugly to me; suggestions to make taht more elegant welcome
I’ve wanted a do/while version of loop (where the binding is created inside the “recur”, even on the first iteration)
I guess that’s an easy macro to write
@qqq am I reading that correctly, it only recurs if len is 0 or less? never mind - when not if
@qqq Isn't that just a while
loop? So something like:
(defn sha-512 [fname]
(let [md (MessageDigest/getInstance "SHA-512")
ba (byte-array (* 1024 1024))
fis (q/io-fis fname)]
(while (pos? (.read fis ba))
(.digest md ba 0 len))
(seq (.digest md))))
well, len has to come from .read
which is what made me think of my do/while idea
(ins)user=> (defmacro loop-while
[bind condition & body]
`(loop ~bind
(when ~condition ~@body
(recur ~(second bind)))))
#'user/loop-while
(ins)user=> (loop-while [t (java.util.Date.)] (> (rand) 0.1) (println t))
nil
(ins)user=> (loop-while [t (java.util.Date.)] (> (rand) 0.1) (println t))
#inst "2018-02-14T01:01:07.730-00:00"
#inst "2018-02-14T01:01:07.730-00:00"
#inst "2018-02-14T01:01:07.730-00:00"
#inst "2018-02-14T01:01:07.730-00:00"
#inst "2018-02-14T01:01:07.731-00:00"
#inst "2018-02-14T01:01:07.731-00:00"
#inst "2018-02-14T01:01:07.731-00:00"
#inst "2018-02-14T01:01:07.731-00:00"
nil
of course that binding would usually reference t (and that actually works - just didn’t make sense in this silly example)
i like the inclusion of loop in the macro name. you can't nest loops right? recur calls would be ambiguous?
better example
user=> (loop-while [r (rand)] (> r 0.1) (println r))
0.7289088591417161
0.770206007468366
nil
loop-while does! lets you use an arbitrary binding and condition
@noisesmith: are you releasing loop-while
into the public domain? 🙂
more general version
(ins)user=> (defmacro loop-while
[bind condition & body]
`(loop ~bind
(when ~condition
~@body
(recur ~@(map second (partition 2 bind))))))
#'user/loop-while
(ins)user=> (loop-while [r (rand) s (rand)] (> r s) (println r s))
0.6569131819243965 0.2807053168342115
nil
(ins)user=> (loop-while [r (rand) s (rand)] (> r s) (println r s))
0.706471848833365 0.40927277078473123
0.23989683003342344 0.11729876666618255
0.9669516458014131 0.07665936063939205
nil
the partition / map lets you put multiple bindings in the loop (as should be allowed)
I’ve wanted this in a few places (usually where IO of some sort is involved) - glad to finally be motivated to write it
recur is always to the innermost loop
(or fn, - recurrence target in general)
perhaps it should be allowed to return a non-nil value, not sure where to put that in the syntax though
i think i remember seeing a proposition for recur-to
that looked interesting and went nowhere
the issue here is that each function has its own invoke method, and you can’t do recur (a goto) across method boundaries
perhaps there would be a way to do it with nested loops if they were all in the same method though
yeah, notice there none of their potential solutions is tail call optimized
Is there a 🐛 ticket to resolve this, or is this just how ’s going to work?
(let [{:keys [foo/a foo/b]} {:foo/a 1 :foo/b 2}] [a b]) ;; => [1 2] 👌
(require '[clojure.string :as str])
(let [{:keys [::str/a ::str/b]} {::str/a 1 ::str/b 2}] [a b]) ;; => [1 2] 👍
(let [{:keys [clojure.string/a clojure.string/b]} {::str/a 1 ::str/b 2}] [a b]) ;; => [1 2] 👍
(let [{:keys [str/a str/b]} {::str/a 1 ::str/b 2}] [a b]) ;; => [nil nil] 😦
B/c now there’s one scenario where you must destructure w/ keywords instead of symbols.
Then you could use {::str/keys [a b], :foo/keys [c]}
. Kind of cumbersome though. I agree with you in that I'd expect all you examples to work...
@U064X3EF3 Should the case I noted work? If so, I’ll create a JIRA ticket.
Maybe not. I guess if str/a
matched ::str/a
, it’d be indistinguishable from :str/a
, which it’d also match.
This doesn't look like a bug to me. It seems the double-colon is shorthand for namespace expansion, such that ::str
is expanded to :clojure.string
only in the context of there being an alias. Given that, ::str/a
matches :clojure.string/a
because it is expanded before checking the match, but str/a
should not match :clojure.string/a
.
Right. This is correct behavior.
@qqq Given that it's normally referred to as SHA-512 out there in the literature, I'd go with sha-512
in Clojure.
hi, this is monger docs :connections-per-host (default: 10) The maximum number of connections allowed per host for this Mongo instance. Those connections will be kept in a pool when idle. Once the pool is exhausted, any operation requiring a connection will block waiting for an available connection.
i found here https://stackoverflow.com/questions/34885628/what-is-connections-per-host-mongodb
Hi! I'm confused by uberjar. When I run ring lein uberjar
, I get a NullPointerException in some part of my code. I get why it's nil (because I read an envvar that's not set while I execute this command), but I don't get why my code is executed in the first place during compilation
are you using def
for that?
don't
wrap that in a function
not an expert in compilation but, yes, the System/getenv call will be done at compile time
all the code is run but with defn
it's not a problem because the functions are not run, only the definition of them is run
that's why you should wrap it
same would happen if you did anything directly in the ns, try adding a println, you'll see
is there a cli tool for putting the latest version of a dependency into project.clj?
https://github.com/xsc/lein-ancient lein-ancient will happily check if your deps are out of date, and update them for you
that's actually pretty awesome.
getting them in there in the first place is my main concern atm...
tired of having to loop over to clojars each time... I'm thinking of something like npm install --save which I've gotten used to.
looping over to the browser and clojars each time is a subtle flow breaker.
the same for figuring out the exact string to require each library in a ns, which vary from the name used to pull it into the project in project.clj.
I'd suggest you to build your own lein plugin that uses the clojars api:
curl -H "Accept: application/edn"
get the latest version from there and add it to project.clj
take a look at lein-ancient source code for inspiration
that would be a good project, actually.
I'd learn a lot as well.
lein plugins are easy to develop, and you already have one that does half of what you want 🙂
great suggestion... I'll take a look.
does anyone have any advice on using clojure for cloud / serverless (either aws or azure) ? a google search turns up links that are a year or two old. thanks!
Check also https://github.com/portkey-cloud/portkey and if ClojureScript is ok for you, this is awesome https://github.com/nervous-systems/cljs-lambda
would be great if I could find examples of using clojure / azure functions as I could use it (sneak it in) at work
i take it you have seen https://github.com/uswitch/lambada ?
I did find this, which makes me think I can use clojure to write azure functions, https://azure.microsoft.com/en-us/blog/announcing-the-preview-of-java-support-for-azure-functions/
yeah it seems like there was a craze a few years ago where everyone was writing serverless apps and libraries, which appears to have cooled down a bit now
amazonica has support for lambda as well: https://github.com/mcohen01/amazonica#lambda
if i would want to start out with clojure + aws lambda, that would be my starting point
https://gist.github.com/alandipert/d2cb38ee869448182c4b also has some interesting stuff
Hi all, i am going to start a new project and I was going to pick pedestal, but seems that the last commits were made 4 months ago, is this project still active?
Projects in Clojure tend to be more stable, so don't read into (and I emphasize) only 4 months.
@genec If you do go serverless with Clojure (and it’s definitely possible) with AWS lambda, one important trick is to use a CloudWatch event to keep the lambda warm (e.g. invoke the lambda every five minutes to keep its response times < 1 second). I do this with Terraform (which I also recommend), working example here: https://github.com/eigenhombre/trav/blob/master/tf/terraform.tf#L152
@eigenhombre thanks for the advice
@eigenhombre: how is this beneficial in practice, in my understanding (from talking to aws business support); this would only keep one lambda instance alive, and other lambda requests (when all isting capacity is used) w9ould still get delays, i.e. lambda doesn't 'prewarm' due to load
I've heard the term "specificity" in a couple of talks and it seems to be a commonly used in the clojure community. I'm not sure what it's referring to though, what are they talking about? 🙂
@oliver089 depends on the topic, can you be a bit more specific 😄
I've seen it discussed in the context of clojure-spec and also in random dicsussions like this one https://www.reddit.com/r/programming/comments/3bnt8m/rich_hickey_javaone_presentation_clojure_made/
In general it just means that you can (or have to) be very specific about what you want. In the case of this reddit article they're talking about OOP methods being specific to the object on which they are defined. In Clojure conj
works on a lot of different types. In general .put
in java is specific to the type its defined on.
But it can be a good or bad thing, depending on the context or speaker.
Someone could say that spec is better than Java types because it gives more specificity via stuff like pos-int?
vs Integer
yes, I can see that it would provide far greater constraints than the java type system (not particularly hard 😉 )
and I see your point about having more general functions to work on more widely used data structures
@oliver089 Did you watch the video recording starting at the point given in the link by the first commentor in that Reddit thread? The example of httpServletRequest's Java API vs. Ring that begins around time 47:50 with the slide titled "(Program) Size Matters" seemed fairly clear to me on how he meant specificity there. Transcript is available here if you prefer reading to listening: https://github.com/jafingerhut/talk-transcripts/blob/master/Hickey_Rich/ClojureMadeSimple.md
@andy.fingerhut yeah I watched it after I posted the question, it got to the punchline very quickly and yes it makes perfect sense.
(It's a long time since I've worked in Java so the HttpServletRequest example brought back memories 😉 )
Hi folks, is there a graphql/lacinia channel?
Or is this a good place to ask?
I'll just shoot.... I'm developing a schema.edn and my return type is a 2D array that can be any json type (e.g. String, Int, Double). Anyone know if this is possible to specify with lacinia/gql? Closest attempt:
{
:unions
;Note that this doesn't work. Unions must be objects.
{:anything {:members [String Int]}}
:table
{:fields {:data {:type (list (list :anything))}}}
}
Ah, I think I got it. http://lacinia.readthedocs.io/en/latest/custom-scalars.html. I think I could create a custom scalar type for that.
Not sure if this is the best way to do it, but something like this works:
(def schema
{:scalars
{:Date
{:parse (schema/as-conformer #(.parse date-formatter %))
:serialize (schema/as-conformer #(.format date-formatter %))}
:anything
{:parse (schema/as-conformer edn/read-string)
:serialize (schema/as-conformer identity)}}})
Hey moderators (and clojureians) is it ok to publish clojure projects on this channel?
I doubt anyone would mind, but there is #announcements channel specifically for that and other similar announcements
Cool ill use that channel
@narkisr And when you post in #announcements it's a good idea to also tell folks which channel to use for any follow-up
Ok, thanks for the info