This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-08-30
Channels
- # aleph (15)
- # announcements (4)
- # aws (2)
- # bangalore-clj (7)
- # beginners (236)
- # calva (24)
- # cider (11)
- # cljs-dev (63)
- # clojure (141)
- # clojure-europe (3)
- # clojure-india (2)
- # clojure-italy (8)
- # clojure-nl (3)
- # clojure-spec (8)
- # clojure-uk (52)
- # clojured (1)
- # clojuredesign-podcast (4)
- # clojurescript (35)
- # clojutre (3)
- # community-development (1)
- # cursive (77)
- # data-science (1)
- # datomic (3)
- # emacs (13)
- # fulcro (7)
- # graalvm (78)
- # graphql (2)
- # nrepl (7)
- # off-topic (18)
- # pathom (25)
- # reagent (12)
- # reitit (31)
- # shadow-cljs (178)
- # spacemacs (7)
- # tools-deps (32)
- # xtdb (10)
- # yada (3)
If you are querying elements for each item in list, but you want the first element that matches a predicate. What would you use? I could do this with map, filter, and first but that means every element would be processed.
@jayzawrotny If you really want the first matching element and want no others inspected, I'd probably reduce
and stop with reduced
.
map
/`filter` won't process "every" element but would likely process up to 32 more than you needed (chunked sequences).
I didn't realized you could break out of a reduce
early with reduced
... that's kind of a game-changer
Since Clojure 1.5 (March 2013).
Realized I was over-engineering it and could use a take-while
instead. But seems to be working!
Often simple is best. Until you know you have a performance issue. Then optimize 🙂 @jayzawrotny
True, I realized the heavy lifting can be done in the predicate which should make it decently performant. Technically it will be doing more work as you scroll down but it should be fine for a first draft prototype with < 10 items. If it does slow down, then perhaps my approach here itself is wrong.
@jayzawrotny there's a function in medley for this that is well optimized
and if elements in your coll
can be falsey, you can write it as (first (some #(when (pred %) [%]) coll))
Interesting. I did not realize some would return anything other than true or false. Now I understand why it doesn’t end with a ?
Thanks!
Is (eval (apply list [+ 1 1]))
supposed to work? Storing the Form in a vec instead of a list makes it so that the + function isn't just '+, but actually the function Object. My eval works like this.... Until i include spec instrumentation anywhere. Then it Breaks, saying something like "no ctor found for spec..." is this a Bug in spec or am i using unstable behaviour of eval?
@lkowarschick don't know about spec, but I think it should be (eval (apply list ['+ 1 1]))
(notice quote)
yea quoting the function makes sense i guess ^^ but it should somehow be possible to use it without, seeing that i.e. reagent uses [function args] in its hiccup-style html syntax,...
@lkowarschick as an aside to all the above, I'd never use (apply list [x y z])
instead of (list x y z)
- the result will always be the same
and with eval, as with macros, ` is likely more useful
(ins)user=> (let [x 1] (eval '(+ x x)))
Syntax error compiling at (REPL:1:19).
Unable to resolve symbol: x in this context
(cmd)user=> (let [x 1] (eval `(+ ~x ~x)))
2
alternatively
user=> (let [x 1] ((eval '(fn [y] (+ y y))) x))
2
Can someone help me to use generator for multi-spec .. I have looked all over .. but couldn't find anything so far
@nav301186 does the example here help? https://clojuredocs.org/clojure.spec.alpha/multi-spec
@noisesmith following those examples generates spec ony with the tag key
when I run the last gen/sample repeatedly, I see both variants, :example-key and :different-key
which means it's generating both methods
it provides the :tag because that's part of the spec
perhaps I am misunderstanding
@noisesmith thanks ... I had an issue with spec itself .. I used ::req-un
and after changing it to :req-un
(with one less colon) it works
I'm curious... why do some libraries use the com.orgname.somethingelse naming scheme, and some just use projectname? Is one preferable over the other for some reason?
one convention is more correct according to the host ecosystem, the other is preferred by people who think the host ecosystem are overengineering things
I see. I found this after looking for a minute: https://central.sonatype.org/pages/choosing-your-coordinates.html
in most languages you can call your project "foo" and publish it with that name, it's more of a java thing to expect everyone to namespace projects based on a unique URI they own
sonatype coordinates are a different issue - I thought you were talking about package names
(the underlying concerns are of course similar)
leiningen encourages the looser convention, even allowing you to call a project "foo", and automatcally publishing the artifact as "foo/foo" and generating a "foo.core" ns
where if you use lein new org.noisesmith.foo
it would do the correct thing and publish as "org.noisesmith/foo" and create the "org.noisesmith.foo" ns
(but few people use that style)
https://clojars.org/search?q=group-id%3Acom.* seems like many folks use this scheme. Is this mostly a convention inherited from java?
that's what I mean by "the host ecosystem" - we use jvm deps, and publish jars
yes, it's the standard java convention
I think you'll find that most clojure libs don't use that convention though (they just don't all get packed into one directory like com / org / net do)
Alright, I've been using the looser convention, and will continue to do so. (e.g. https://github.com/escherize/tracks )
thanks for the chat @noisesmith
right, there's no way to use two namespaces called tracks.core, one wins, the other is unavailable
and if you leave your project.clj as is, the dep manager will ensure only one tracks/tracks is pulled in (a single segment project name is repeated twice by lein to generate a group/artifact pair)
my perspective is that the prefix of your group-id should be something you "control", either by dns, trademark, or otherwise
I've seen people recommend that you use a domain you control, or your GitHub username. My GitHub username is just zane
but using that as the first segment of my group-id feels a bit presumptuous.
yes, it does :)
Thanks for the external validation. 🙂 I have a library I've been holding off on publishing because I've been anxious about the finality of decisions around things like naming and versioning.
I was just agreeing that deciding on naming and versioning sucks :)
and is often a hard gate in the way of actually getting things done :)
The other is getting the name you want on maven central and others and ensuring its unique. The dns system is a good way to go about this.
Things get a bit more ambiguous for applications, I think you still want a good bit of prefix though.
if you're not publishing something to a public repo, do whatever you want
the common practice on clojars of using same group-id and project-id is not a good practice per standard Maven practices (they won't let you do that in Maven central, although they've grandfathered in older things)
I wish we had a more established convention of "borrowing" identity from 3rd parties (in addition to dns/trademark stuff)
so for example, github identity could confer sufficient uniqueness
so like a project with coordinate github.puredanger/foo
(note that this then conflicts with github itself publishing its own things to maven central)
I'm not going to solve that problem today :)
I do receive reluctance to use the top level domain on things, but less so trademarks. Company names seem to be considered unique enough, and not so tedious to type that it's okay.
How would I decode a b64-encoded binary input stream to a plaintext string? I tried data.codec
but I can't seem to get it to work. Here's what I'm trying to do:
(let [r (aws-api/invoke c {:op :GenerateDataKey
:request {:KeyId cmk-arn
:KeySpec "AES_256"}})
plaintext (:Plaintext r)]
(prn plaintext)
(with-open [p plaintext
out (io/output-stream "test.txt")]
(clojure.data.codec.base64/decoding-transfer p out)))
#object[java.io.BufferedInputStream 0x477586db "java.io.BufferedInputStream@477586db"]
java.lang.ArrayIndexOutOfBoundsException: Index -92 out of bounds for length 123
oh, I see you already tried data.codec, I would look at test.txt and see if actually is base64 encoded
base64 encoding, basically the reason it was created, was to encode arbitrary binary as printable ascii characters
so if you are getting arbitrary binary data and not printable ascii it is not base64 encoded
I think something else is going on because I doubt the AWS docs would be totally wrong. It says plaintext
is
> Base64-encoded binary data object
Maybe you have some middleware that’s automatically converting it from B64 to binary before you get it?
Asked in #aws since it may be an aws-api lib issue. It would surprise me though. I'm guessing that weird string needs to be converted from whatever that is to regular b64.
The source code does define a base62-decode
function https://github.com/cognitect-labs/aws-api/blob/master/src/cognitect/aws/util.clj#L225
Just scanning through the github repo I can’t work out exactly how that’s used, but it’s there.
for a moment I wondered if base62 existed :D
We should MAKE one!!
whoops typo
reminds me of a very silly thing I made - base-;lkjfdsa https://gist.github.com/noisesmith/561c215ffa39f71645b5
user> (base-_SEMI_lkjfdsa-encode "hello")
";ddks;jjfddas;;;"
user> (base-_SEMI_lkjfdsa-decode ";ddks;jjfddas;;;")
"hello"
That looks like fun
it was a cute exercise to figure out
“NSA AGENT: The suspect seems to be sending coded messages. OTHER NSA AGENT: Nah, that’s probably just line noise.”
in certain subcultures "alksdf;laksdjf;lkajsdfl;jasdl;" is equivalent to "LOL" combined with "I can't even"
so it hides in plain sight
txt-speak keyboard mashing steganography
Maybe that string is actually what I'm supposed to get returned. I was kinda expecting a regular rsa key or something of the likes.
@kenny it could be the raw version of the hash, where you can construct the normal printed representation via %02x format
eg. this one liner to get the sha-256 string
user=> (let [digest (java.security.MessageDigest/getInstance "sha-256") input (.getBytes "hello" "UTF-8") result (.digest digest input)] (->> result (map #(format "%02x" %)) (apply str)))
"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
you'd just need the last part (->> result (map ...) (apply str))
to get the "hex" output from the bytes of the string
oh, you'd want .getBytes before that
user=> (->> (.getBytes "hello" "UTF-8") (map #(format "%02x" %)) (apply str))
"68656c6c6f"
@noisesmith I did this:
(let [digest (java.security.MessageDigest/getInstance "sha-256")
r (aws-api/invoke c {:op :GenerateDataKey
:request {:KeyId key-arn
:KeySpec "AES_256"}})
plaintext (:Plaintext r)]
(->> (slurp plaintext) (map #(format "%02x" %)) (apply str)))
java.util.IllegalFormatConversionException: x != java.lang.Character
I'm now fairly certain that weird string is the expected return value I followed this guide (https://dev.to/matchilling/pragmatically-storing-security-sensitive-data-using-aws-kms-5e5b) to get the decoded, plaintext key and it looks very similar to what I received.sorry my example was lazy
you don't want to calculate the digest, you just want the last part of turning a byte-array into hex string - see my more recent example
or were you coincidentally already using sha-256...
Oh that did something interesting haha. I got "efbfbd2cefbfbd18efbfbd5f1b5956d1926b084e5befbfbd38efbfbdefbfbdefbfbdefbfbdefbfbdefbfbd30efbfbd40efbfbd0e77efbfbd13efbfbd"
I'm not sure if that's the form you need, but at least it is a format that round trips, as opposed to the weird string you had before
it could be you want the base64, so you could re-encode it to that format
it really depends on what you are trying to do with this payload I guess
can I use *in*
in combination with tools.reader?
(with-in-str "(+ 1 2 3)"
(edn/read *in*))
;;=> Pushback buffer overflow
If not, how can I get from *in*
to something that tools.reader can work with?the use case for me is that clojure.server rebinds *in*
to a reader that reads from a socket and I want to do my own thing with this. clojure.server doesn't expose the connection directly
@hiredman are you suggesting the answer is indeed in the README? because I had skimmed it before asking the question here 🙂
(require '[clojure.tools.reader.reader-types :as t])
;=> nil
(def reader (t/string-push-back-reader "1"))
(from the readme)
Are you trying to read a string with pushback?
(t/push-back-reader *in*)
maybe?
There's a whole bunch of push back readers in that ns.