beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
sb 2021-02-03T02:09:43.415900Z

Yes, this is little bit painful. I solved without clj-time.. (I hope tick will have better documentation later.)

sova-soars-the-sora 2021-02-03T04:59:00.419300Z

clj-time is so cool though... ;_;

suren 2021-02-03T05:00:46.420700Z

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 .

sb 2021-02-03T16:36:12.432600Z

@suren maybe Im too late, but that is useful (google api) https://gist.github.com/arohner/8d94ee5704b1c0c1b206186525d9f7a7

suren 2021-02-05T06:58:55.019800Z

I did came across the gist. But I reckon its better to use the SDKs instead of apis.

sova-soars-the-sora 2021-02-03T05:01:26.421Z

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! šŸ˜ƒ

sova-soars-the-sora 2021-02-03T05:02:35.421300Z

Hmm I think you are very close...

seancorfield 2021-02-03T05:03:30.421400Z

@sova Seriously? I mean, I'm one of the maintainers and I don't think there's anything "cool" about it...

sova-soars-the-sora 2021-02-03T05:04:35.421600Z

yes i think it's awesome ... but the latest state-of-the-art recommendation is to use java-time via interop?

šŸ‘ 1
sova-soars-the-sora 2021-02-03T05:05:24.421900Z

What's your import line like ?

sova-soars-the-sora 2021-02-03T05:08:24.422100Z

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 ^.^

dpsutton 2021-02-03T05:09:30.422400Z

https://clojure.org/reference/java_interop#_class_access you can see how to access nested classes here. I would try (GoogleIdTokenVerifier$Builder. (NetHttpTransport.) (JacksonFactory.))

seancorfield 2021-02-03T05:10:12.422700Z

Or cljc.java-time or tick (although we use clojure.java-time at work because it was available first). Mostly we use interop.

dpsutton 2021-02-03T05:11:40.423Z

updated after reading it incorrectly

suren 2021-02-03T05:11:49.423200Z

@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))

suren 2021-02-03T05:13:25.423400Z

@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

seancorfield 2021-02-03T05:14:10.423600Z

You need to import GoogleIdTokenVerifier$Builder

seancorfield 2021-02-03T05:14:16.423800Z

or use it fully-qualified

suren 2021-02-03T05:15:35.424Z

@seancorfield Wow it worked.

suren 2021-02-03T05:15:44.424200Z

Thank you guys a lot.

seancorfield 2021-02-03T05:16:11.424400Z

Nested classes in Java aren't really "nested", they're more like "siblings".

suren 2021-02-03T05:17:23.424600Z

Hmm got it.

2021-02-03T14:28:24.428500Z

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_

Christian 2021-02-03T16:23:54.431Z

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?

dpsutton 2021-02-03T16:25:10.431300Z

defn is a macro and expects certain literal syntax

dpsutton 2021-02-03T16:27:06.432500Z

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)

2021-02-03T16:48:09.434200Z

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.

aratare 2021-02-03T16:54:18.436500Z

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?

dpsutton 2021-02-03T16:55:31.436900Z

doto is your friend here

2021-02-03T16:55:57.437300Z

I thought doto would only be your friend if you have methods that return the object they mutate?

2021-02-03T16:56:46.438200Z

Oh, never mind me. Forgetting things.

aratare 2021-02-03T16:56:47.438300Z

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))

šŸ‘ 1
dpsutton 2021-02-03T16:56:54.438600Z

doto doesn't thread the object i don't believe

aratare 2021-02-03T16:56:56.438800Z

So yeah it is my new friend

aratare 2021-02-03T16:57:07.439100Z

Thanks a lot šŸ™‚

aratare 2021-02-03T16:58:15.440200Z

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?

dpsutton 2021-02-03T17:00:53.441800Z

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-&gt;. Or I find it often better to figure out why the "position" changes and build forms that acknowledge that. a let binding or perhaps (-&gt;&gt; (-&gt; x foo bar) ...) can solve the "mismatch" problem many times

aratare 2021-02-03T17:02:09.442100Z

Exactly what I need. Thanks again šŸ™

2021-02-03T17:02:20.442200Z

as a correction, -&gt; needs to be on the outside, all the other arrows work inside it by design

dpsutton 2021-02-03T17:03:07.442400Z

that's valid there. its threading that single form through the thread last versions. You are talking about a different kinda pattern

2021-02-03T17:03:31.442600Z

ahh - I see what you are doing OK

2021-02-03T17:04:06.442800Z

that said, (-&gt; x foo bar (-&gt;&gt; ...)) does the same thing, and is more flexible

2021-02-03T17:04:16.443Z

I'd call it the "canonical" version

dpsutton 2021-02-03T17:05:41.443200Z

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

dpsutton 2021-02-03T17:06:20.443400Z

it makes it feel more contained. but its just pure personal preference

grazfather 2021-02-03T17:17:20.444200Z

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

borkdude 2021-02-03T17:19:37.444600Z

@grazfather There's also a #babashka channel with 400+ people

grazfather 2021-02-03T17:19:46.444800Z

Yep, I just joined šŸ™‚

grazfather 2021-02-03T17:19:53.445100Z

after I wrote that I figured Iā€™d check šŸ˜›

Pravin 2021-02-03T22:38:02.451200Z

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 ?

2021-02-03T23:05:23.452500Z

(ns 
  (:import (java.time LocalDate)
           (java.time.temporal ChronoUnit)))

(let [d1 (LocalDate/parse "2016-04-02")
      d2 (LocalDate/parse "2016-05-25")]
  (-&gt; (ChronoUnit/DAYS) (.between d1 d2)))
=&gt; 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)))
=&gt; 53

šŸ‘ 1
Pravin 2021-02-04T15:23:02.471700Z

this worked, thank you !!!

borkdude 2021-02-03T23:08:05.453Z

You'll probably have to fix the ns form

2021-02-03T23:08:28.453300Z

Yeah, I just wanted to show where ChronoUnit was coming from

šŸ‘ 1
seancorfield 2021-02-03T23:24:10.454400Z

(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)