Yes, this is little bit painful. I solved without clj-time.. (I hope tick will have better documentation later.)
clj-time is so cool though... ;_;
How do we interop following Java Class in Clojure?
GoogleIdTokenVerifier verifier =
new GoogleIdTokenVerifier.Builder(new NetHttpTransport(), new JacksonFactory());
I tried following which does not work.
(. GoogleIdTokenVerifier/Builder (NetHttpTransport.) (JacksonFactory.))
From the source code here https://github.com/googleapis/google-api-java-client/blob/master/google-api-client/src/main/java/com/google/api/client/googleapis/auth/oauth2/GoogleIdTokenVerifier.java
I can see Builder
is a class in that file.
It looks like Builder
is in the namespace GoogleIdTokenVerifier
.@suren maybe Im too late, but that is useful (google api) https://gist.github.com/arohner/8d94ee5704b1c0c1b206186525d9f7a7
I did came across the gist. But I reckon its better to use the SDKs instead of apis.
hey I noticed when you started this, I think I got wind of it via the reddit/clojure channel... Love the ambition. Glad you're doing it and streaming it! š
Hmm I think you are very close...
@sova Seriously? I mean, I'm one of the maintainers and I don't think there's anything "cool" about it...
yes i think it's awesome ... but the latest state-of-the-art recommendation is to use java-time via interop?
What's your import line like ?
maybe this will help https://livebook.manning.com/book/clojure-in-action-second-edition/chapter-5/73 maybe 20% down the page... hopefully someone more experienced with interop can chime in ^.^
https://clojure.org/reference/java_interop#_class_access
you can see how to access nested classes here. I would try
(GoogleIdTokenVerifier$Builder. (NetHttpTransport.) (JacksonFactory.))
Or cljc.java-time
or tick
(although we use clojure.java-time
at work because it was available first). Mostly we use interop.
updated after reading it incorrectly
@sova The import statements are given below
(:import com.google.api.client.http.javanet.NetHttpTransport
com.google.api.client.json.jackson2.JacksonFactory
com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier))
@dpsutton I have tried it before but unfortunately does not work It throws following error
Caused by: java.lang.IllegalArgumentException: Unable to resolve classname: GoogleIdTokenVerifier$Builder
You need to import GoogleIdTokenVerifier$Builder
or use it fully-qualified
@seancorfield Wow it worked.
Thank you guys a lot.
Nested classes in Java aren't really "nested", they're more like "siblings".
Hmm got it.
Hello again fellow beginners! Yesterday I started my journey into learning Clojure Spec, which is a really interesting tool for making assertions about data. I don't know so much about it yet, but I've gotten my feet wet with it. In my livestream today we're going to be continuing with the "name validator" coding challenge from Eric Normand's newsletter, which seems like a great use case for Spec and a good way to learn it: https://purelyfunctional.tv/issues/purelyfunctional-tv-newsletter-412-module-depth-is-bogus/ If you'd like to watch some guy with about four weeks of Clojure under his belt bungle his way through and learn a few things along the way, feel free to join! Coding challenge should start around 9:40-ish AM EST: https://www.twitch.tv/a_fry_
Maybe this is a stupid question, but I was wondering how much of the syntax in Clojure is set in stone.
(defn testfunc1 [a b] (+ a b))
(defn testfunc2 (vec a b) (+ a b))
The second one does not work, I thought the [a b] is just another way to say (vec ab). Is this because def is a special form?defn
is a macro and expects certain literal syntax
its looking for a vector which is how arguments are supplied. macros takes forms and return forms. so there's a difference between the literal [a b]
and a form that will evaluate to the same form (vec a b)
You can write a macro that would contain an expression like (vec a b)
that takes some expression as input, and constructs and returns an expression that looks like your first expression, but it would operate upon the symbols and other syntactic elements of the input expression given to the macro, not the "run-time" values of a and b.
Hi there. I have a bit of a question regarding how to do things the "Clojure" way. So let's say I have a Java class Foo
with a bunch of methods that return void
like setBar
or calculateBaz
. Is there a way where I can do something like this:
(-> newFoo
.setBar
.setBar
.calculateBaz)
and return newFoo
by the end?doto
is your friend here
I thought doto
would only be your friend if you have methods that return the object they mutate?
Oh, never mind me. Forgetting things.
From the clojuredoc
;; Note that even though println returns nil, doto still returns the HashMap object
(doto (java.util.HashMap.)
(.put "a" 1)
(.put "b" 2)
(println))
doto doesn't thread the object i don't believe
So yeah it is my new friend
Thanks a lot š
Also to add another question on top of that, I've seen a utility library floating around somewhere which allows me to ->
but with certain position within each form. Can anyone remember what it is?
there's a library <https://github.com/rplevy/swiss-arrows>
that provides lots of arrow or threading functions. you might be able to use as->
. Or I find it often better to figure out why the "position" changes and build forms that acknowledge that. a let binding or perhaps (->> (-> x foo bar) ...)
can solve the "mismatch" problem many times
Exactly what I need. Thanks again š
as a correction, ->
needs to be on the outside, all the other arrows work inside it by design
that's valid there. its threading that single form through the thread last versions. You are talking about a different kinda pattern
ahh - I see what you are doing OK
that said, (-> x foo bar (->> ...))
does the same thing, and is more flexible
I'd call it the "canonical" version
fair. when the thread last form does the majority of the work or is longer i prefer my version as its a bit more clear to me
it makes it feel more contained. but its just pure personal preference
DAE use babashka? I know borkdude is in here. Iām looking for some reason to use clojure more (get more practice day to day doing real things) and I am pretty good at python/bash one liners, so this might be it
@grazfather There's also a #babashka channel with 400+ people
Yep, I just joined š
after I wrote that I figured Iād check š
Hi, iam trying to find date difference in days, currently using [java-time :as jt] namespace. I have something which yields me difference between two dates in months. (.toTotalMonths(jt/period ?start-date ?end-date)) I want to extend this further and see what is the difference in days between these two dates. Is there any toDays method which will yield difference in days ?
(ns
(:import (java.time LocalDate)
(java.time.temporal ChronoUnit)))
(let [d1 (LocalDate/parse "2016-04-02")
d2 (LocalDate/parse "2016-05-25")]
(-> (ChronoUnit/DAYS) (.between d1 d2)))
=> 53
I think this works to get total number of days.
Or with ..
(let [d1 (LocalDate/parse "2016-04-02")
d2 (LocalDate/parse "2016-05-25")]
(.. ChronoUnit/DAYS (between d1 d2)))
=> 53
this worked, thank you !!!
You'll probably have to fix the ns
form
Yeah, I just wanted to show where ChronoUnit was coming from
(a lot of times it's not worth using a wrapper for Java Time stuff, although clojure.java-time
does make some type conversions easier)