This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-08-25
Channels
- # announcements (2)
- # babashka (35)
- # beginners (74)
- # calva (26)
- # cider (14)
- # clojure (74)
- # clojure-dev (27)
- # clojure-europe (9)
- # clojure-italy (2)
- # clojure-nl (2)
- # clojure-spec (10)
- # clojure-uk (9)
- # clojuredesign-podcast (6)
- # clojurescript (40)
- # data-science (1)
- # datalog (7)
- # events (1)
- # figwheel-main (13)
- # fulcro (11)
- # graalvm (58)
- # helix (4)
- # jobs (4)
- # jobs-discuss (9)
- # kaocha (23)
- # malli (5)
- # meander (112)
- # membrane (7)
- # off-topic (13)
- # pedestal (2)
- # re-frame (4)
- # reitit (1)
- # rewrite-clj (1)
- # rum (2)
- # sci (3)
- # shadow-cljs (79)
- # sql (12)
- # tools-deps (17)
- # vim (15)
- # vrac (11)
- # xtdb (6)
Okay, one more, trying to figure out what I need to typehint to get this one:
URL signUrl(BlobInfo var1, long var2, TimeUnit var4, Storage.SignUrlOption... var5);
I write:
(.signURL storage
^BlobInfo blob-info
15
^TimeUnit TimeUnit/DAYS
(Storage$SignUrlOption/withV4Signature))
And get:
No matching method signURL found taking 4 args for class com.google.cloud.storage.StorageImpl
My guess is that I am putting the last argument Storage.SignUrlOption... var5
incorrectly -- it requires an array.
If I try [(Storage$SignUrlOption/withV4Signature)]
have the same no matching method error. If I try
(into-array
Storage$SignUrlOption
(Storage$SignUrlOption/withV4Signature))
I get Don't know how to create ISeq from: com.google.cloud.storage.Storage$SignUrlOption
(may be misunderstanding what intro-array
is supposed to do, thought ^ would type a list of SignURLOption
s(Storage$SignUrlOption/withV4Signature)
is going to give you a single Storage.SignUrlOption
. Varargs in Java collapses to an array under the hood but it uses syntactic sugar to hide that.
So you may have success with (into-array Storage$SignUrlOption [ (Storage$SignUrlOption/withV4Signature) ])
Note the extra [
.. ]
to make an actual vector @stopachka
I'm not sure that will avoid the need for a type hint as well but it will correct the error you're getting.
Ah, thank you @seancorfield !!
@stopachka Did you have to type hint the array, or was that change enough?
Hmm, just tried...and it still seems to have trouble:
(.signURL storage
^BlobInfo blob-info
15
^TimeUnit TimeUnit/DAYS
(into-array
Storage$SignUrlOption
[(Storage$SignUrlOption/withV4Signature)]))
No matching method signURL found taking 4 args for class com.google.cloud.storage.StorageImpl
URL signUrl(BlobInfo var1, long var2, TimeUnit var4, Storage.SignUrlOption... var5);
:thinking_face:@stopachka Do you have a URL for the docs for this, so I can see what other overloads there are?
Thanks for the help Sean! https://googleapis.dev/java/google-cloud-storage/latest/com/google/cloud/storage/Storage.html --- It seems to have only one overload :thinking_face: --- To sanity check, here's what the repl tells me when i check for every type var:
storage
#object[com.google.cloud.storage.StorageImpl 0x2d9299b6 "com.google.cloud.storage.StorageImpl@2d9299b6"]
jt.core=> blob-info
#object[com.google.cloud.storage.BlobInfo 0x2ba685e "BlobInfo{bucket=, name=f86f25c2-9937-4891-ad4c-19d01358812f/image.png, generation=null, size=null, content-type=null, metadata=null}"]
jt.core=> TimeUnit/DAYS
#object[java.util.concurrent.TimeUnit 0x31329080 "DAYS"]
jt.core=> (into-array
Storage$SignUrlOption
[(Storage$SignUrlOption/withV4Signature)])
#object["[Lcom.google.cloud.storage.Storage$SignUrlOption;" 0x2521fe86 "[Lcom.google.cloud.storage.Storage$SignUrlOption;@2521fe86"]
Try hinting storage
as ^Storage storage
... that's about the only thing I can suggest.
Given there's only one .signUrl
in the interface, I'm a bit surprised that it is even needing reflection, but maybe the StorageImpl
class has multiple overloads for some reason (and that implementation class is not documented, of course).
(match [event auth]
["login" _] "LoginAttempt"
["login" "success"] "LoginSuccess"
["login" "failure"] "LoginFailure"
How can I coax clojure to return a list of matching results? For the above, ["login" "failure"], I'd want ("LoginAttempt" "LoginFailure") returned. I realize I could just make the result a list, but I'll be repeating this pattern, and for coding/readability, I'd prefer to be able to list each result separately.Pretty sure you can't do that with core.match
-- it just finds the best match I think? core.logic
would allow you to find all solutions that match.
@joel380 You're trying to use https://github.com/clojure/core.match right? Here's https://github.com/clojure/core.logic
i feel like with match i could apply somehow to each pattern result pair, with :else as maybe nil... then with map get rid of nils. but kind of vague how i could split up the structure in that way.
(defn matchlist [binding & rest] --- need to rip out the :else part and then for each pair in "rest", map match to each pair something like that.
it doesn't seem like you lose much by replacing with a case
statement:
(case event
"login"
(case auth
"success" "LoginSuccess"
"failure" "LoginFailure"
"LoginAttempt"))
actually this has similar problem where I would need to make everything a list:
(case event
"login"
(case auth
"success" ("LoginAttempt" "LoginSuccess")
"failure" ("LoginAttempt" "LoginFailure")
("LoginAttempt")))
if you want an open system (ie. the ability to dynamically add/change/modify responses), then defmulti
may be a better fit
let me ask this though: how do i split a list like this (a b c d e f ...) into pairs ([a b] [c d] [e f] ...)?
In reality, people rarely use core.match
, but I figured if you were already off into that sort of match/unification territory then core.logic
is the next more generic place to go.
user=> (partition 2 [:a :b :c :d :e :f])
((:a :b) (:c :d) (:e :f))
user=>
and if you need predicates, but the system is relatively closed, then you can look into cond
(cond
(or (= event "login")
(= event "authorize"))
(str event auth)
(and (= event "number")
(even? num))
(/ num 2))
(defn matchlist [x & rest] (filter some? (map (partition 2 rest :else nil ) (partial (match x)))) i feel like something like this though would let me use the match approach. ill give it a try.
Here's what it might look like in core.logic
(let [inp ["login" "failure"]]
(l/run* [res]
(l/matche [inp res]
([["login" _] "LoginAttempt"])
([["login" "success"] "LoginSuccess"])
([["login" "failure"] "LoginFailure"]))))
;; => ("LoginAttempt" "LoginFailure")
i will say match is overkill, i likely will only do use essentially equivalence or match anything.
Yeah, I haven't needed core.match
in a decade of production Clojure work, but I thought if you were already using that, you might be interested in core.logic
🙂 Thanks to @qythium for the example of how your problem would look with that (which, yeah, is overkill for most stuff -- and another thing I've never used in a decade of Clojure 🙂 ).
then core.logic is definitely more overkill, it's another totally different paradigm to learn
yeah, planning on using Clara, but for this particular use case i like the pattern matching (Elm-esque)
Yeah, I haven't needed core.match
in a decade of production Clojure work, but I thought if you were already using that, you might be interested in core.logic
🙂 Thanks to @qythium for the example of how your problem would look with that (which, yeah, is overkill for most stuff -- and another thing I've never used in a decade of Clojure 🙂 ).
Multimethods will get you most of the pattern matching you'll probably need -- and it's all built-in and easier to read/understand.
(but multimethods will also only provide a single match/dispatch)
Is it possible to enable/disable spec instrumentation on certain namespaces? [the sym-or-syms arg](https://clojure.github.io/spec.alpha/clojure.spec.test.alpha-api.html#clojure.spec.test.alpha/instrument) seems to require a collection of function names
You can use enumerate-ns to get the functions in an ns to instrument
I am having newb problems in cljs. I send an xhrio GET with:
(defn get-messages []
(GET "/api/messages" ; line 67
{:headers {"Accept" "application/transit+json"}
:handler #(rf/dispatch [:messages/set (:messages %)])}))
Still, in my developer console I see:
xhrio.js:627 GET 404 (Not Found)
From the stacktrace I know that it is line 67 which fails. Why does it try to get "/messages", not "/api/messages"? Where can I begin to debug this?I have tried deleting all the .js and recompiling 🙂 Might there be other compilation output somewhere?
@michael.e.loughlin I am using (ajax.core :refer [GET POST])
🙂 which is cljs-ajax :)
I don't know if my problem was due to it, but I was using the Chromium-based https://brave.com/ browser. Turning off shields/AdBlock for localhost made my error messages a lot easier to read.
Doesn't Brave automatically rewrite urls? The example I saw was adding their own referral querystring param to particular websites...
How can I define a macro that adds java annotations to a gen-class invocation? I tried playing around with vary-meta
but it seems to not work 😕 . My snippet is as follows:
(gen-class :name ^{Deprecated {}} ThisWorks)
(defmacro gen-class-with-Goal [class-name]
(let [cname (vary-meta (symbol class-name) merge {Deprecated {}})]
`(gen-class :name ~cname)))
(gen-class-with-Goal ThisDoesNotWork)
using cider-macroexpand-1
they seem identical where it matters, but I must be doing something stupid, as there is no annotation present on the ThisDoesNotWork
class 😕
I think you're evaluating that symbol in the syntax quote
you want to get the vary-meta in the expansion
I can kind of see what you mean, but the expanded call to gen-class-with-Goal is already: (gen-class :name ^{java.lang.Deprecated {}} ThisDoesNotWork)
running cider-macroexpand-1
after (setq cider-macroexpansion-print-metadata 1)
(in emacs) seems to indicate so, but perhaps I am not looking at things correctly
user=> (macroexpand-1 '(gen-class-with-Goal Foo))
(clojure.core/gen-class :name Foo)
you want that to instead expand to:
(clojure.core/gen-class :name (vary-meta Foo merge {Deprecated {}}))
that is, vary-meta at runtime, not at compile time
I don't think that works as gen-class seems to generate a (vary-meta <rest-of-my-classname>
class, even if I simply copy your code 😕
wow, I actually found a post that answers my specific issue: https://groups.google.com/g/clojure/c/Ee1bVwcUT-c . Thanks for putting me on the right track @U064X3EF3
common macro question
Hi undergrad and new to clojure , I recently had an interview and after the interview they sent me an assignment which a small program so that I do a code review, any hints on how to do this would be really helpful
When I am reviewing code I open it in a Clojure editor and run a repl, then evaluate the code, first the whole code then specific functions and expressions, until I know what each part is doing. If I am unsure of particular expressions or functions, I will run a debugger and step through the code. I will also pull apart expressions and evaluate then with different values. Experimenting with the code leads to ideas of alternative approaches and using the REPL will provide instant feedback as you try different ideas. Use a rich comment block or a separate design-journal namespace to collect your thoughts and code, so that others can do the same with your feedback.
Hello, :) I have a design question. I am writing a program for processing Ethereum transactions. The program processes block after block until it reaches current end of a block chain. Than it waits for a new block to appear. Do I stretch too much that I could model this using lazy sequence? Something like this in pseudo code:
(defn process-blockchain [blockchain]
(process-block (first blockchain))
(process-blockchain (rest blockchain)))
(defn create-blockchain [block-number]
(lazy-seq
if (next-block-available? block-number)
(fetch-block (inc block-number))
(wait-for-block (inc block-number))))
Thank you. It is my first Clojure program. :)generally, if you care much about the exact timing of realization (and I think this probably falls into that case), I would not use lazy seqs
and instead use loop/recur or a core.async channel, or something else
Thank you Alex, will have a look into those. :thumbsup:
the reason for this is that Clojure does not make guarantees about how lazy things are, and some sequence functions (map, filter, etc) amortize the cost of realization by realizing chunks at a time, usually 32 elements
and others are more or less lazy about the first item, etc
Understand. Thank you! 🙂