This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-03-19
Channels
- # alda (1)
- # announcements (2)
- # babashka (14)
- # beginners (30)
- # biff (12)
- # clerk (2)
- # clj-kondo (18)
- # clj-on-windows (1)
- # clojure (98)
- # clojure-europe (9)
- # clojure-gamedev (4)
- # clojurescript (39)
- # conjure (1)
- # data-science (1)
- # emacs (25)
- # events (1)
- # fulcro (1)
- # hyperfiddle (13)
- # lsp (3)
- # malli (1)
- # membrane (10)
- # off-topic (12)
- # reagent (7)
- # scittle (21)
- # shadow-cljs (10)
Shower thought: Do you get higher quality large language model code completions when you put ;; SPDX-License-Identifier: GPL-3.0-or-later
at the top of the file?
I asked ChatGPT 4 (bing), to translate Java to Clojure.
class P {
private String name;
private Date dob;
public String getName(){ return name; }
public String getDob(){ return dob; }
public int getAge(){ ... } // I was too lazy to think about how to do this
P(String name, Date dob){ this.dob = dob; this.name=name; }
}
ChatGT4 said,
(defrecord P [name dob])
(defn create-p [name dob]
(->P name dob))
(defn get-name [p]
(:name p))
(defn get-dob [p]
(:dob p))
(defn get-age [p]
(let [now (java.util.Date.)
dob (:dob p)
age (/ (- (.getTime now) (.getTime dob)) (* 1000 60 60 24 365))]
(int age)))
(def p (create-p "John" (java.util.Date. 1980 1 1)))
(get-name p) ; returns "John"
(get-dob p) ; returns #inst "1980-01-01T00:00:00.000-00:00"
(get-age p) ; returns 43
In this example, we define a new record type P using the defrecord macro. We then define a function create-p that creates a new instance of P with the given name and dob. We also define two accessor functions get-name and get-dob that retrieve the name and dob fields of a P instance.
Finally, we define a function get-age that calculates the age of a P instance based on the current date and the dob field. We create a new instance of P with the name "John" and a date of birth of January 1, 1980, and use the get-name, get-dob, and get-age functions to retrieve the values of the name, dob, and age fields.
I hope this helps! Let me know if you have any other questions.
Seems ok to me.> Seems ok to me. I’m gonna disagree on that one. Maybe a technically correct translation (mod the age which I’m not considering) but hardly idiomatic. TBH I’d probably write it as something more like
user> (import '[java.util Date])
java.util.Date
user> (defn get-age [dob-inst] (-> (/ (- (.getTime (java.util.Date.)) (.getTime dob-inst)) (* 1000 60 60 24 365)) int)) ; ~verbatim, not improving
#'user/get-age
user> (def p {:name "John", :dob #inst "1980-01-01"})
#'user/p
user> (:name p)
"John"
user> (get-age (:dob p))
43
I tried to make it write more idiomatic code.
It just refuses to get rid of that extra get-age
function or field, so it always ends up with some variation of
(defn get-age [dob]
; implementation for getAge goes here
(...))
(defn make-P [name dob]
{:name name
:dob dob
:get-age (partial get-age dob)})
@U2FRKM4TW would you be inclined to omit the get-age
function entirely? That's the one bit in all of it that I would actually tend to abstract..
I've found it's not particularly easy to get code that works no matter how much back and forth. Though v4 seems better than three. It is pretty frequently still useful to ask though, particularly if you have an implementation in one lang you just want to understand, or get some skeleton code to then make work. I've been giving it big gobs of rust and asking for varying combinations of protocols/functions/specs. It's also sort of good at some tedious stuff, like just asking it for code to take a bunch of maria I give it and print me a csv string (using data.csv) Hasn't really led to any less manual coding, but it's starting to change the way I think about things.
> Hasn’t really led to any less manual coding, but it’s starting to change the way I think about things. @U064UGEUQ can you talk a bit more about that part? I’m really curious.
Maybe change the way I think is a bit to strong, more like starting to displace some mindless googling about coding topics, or where before I might have some question I'm starting to form that never quite becomes slack worthy, it's now a place to air all that stuff. That and the whole ability to explain or translate anything to semi acceptable clojure, which is just more pleasant for me to comprehend even when there are some gaps. Basically it's a new tool that I or probably anyone doesn't understand quite what to do with yet, but it's certainly non correlated enough to any existing tool to be really interesting
@U077BEWNQ I'd leave the function itself. But ChatGPT either adds that :get-age
field or another get-age
function. It refused to leave just one copy of it, without any closures and whatnot, until I explicitly told it to "please remove the last function."
that age calc doesn't account for leap years, but I guess it's ok since most humans don't live pass 100 years