This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-12-01
Channels
- # adventofcode (170)
- # announcements (3)
- # babashka (1)
- # beginners (25)
- # cherry (1)
- # cider (3)
- # clj-kondo (5)
- # cljsrn (9)
- # clojure (27)
- # clojure-art (2)
- # clojure-europe (11)
- # clojure-nl (1)
- # clojure-norway (26)
- # clojure-sweden (2)
- # clojure-uk (5)
- # code-reviews (12)
- # component (8)
- # conjure (1)
- # data-science (2)
- # hyperfiddle (6)
- # malli (5)
- # off-topic (65)
- # overtone (34)
- # polylith (3)
- # re-frame (2)
- # reagent (2)
- # releases (3)
- # rum (1)
- # shadow-cljs (2)
- # slack-help (8)
- # sql (8)
- # squint (100)
- # thejaloniki (3)
- # tools-build (16)
- # vim (7)
- # yamlscript (1)
Hello, I’ve been doing some Clojure and now feel that some feedback would really help me learn some more.
(ns exercise-6.core
(:gen-class))
(require '[tick.core :as tick])
(defn year-format [date]
(tick/format (tick/formatter "yyyy") date))
(defn parse-year [date]
(Integer/parseInt (year-format date)))
(defn today []
(tick/date (tick/instant)))
(defn ask [text]
(println text)
(flush)
(read-string (read-line)))
(defn retirement-year [this-year years-left]
(str
"You have "
years-left
" years left to retirement"
"/n"
"It's "
this-year
", so you can retire in "
(+ this-year years-left)))
(def retirement-age-reached "You can already retire")
(defn retire [retirement-age current-age this-year]
(let [years-left (- retirement-age current-age)]
(if (>= 0 years-left)
retirement-age-reached
(retirement-year this-year years-left))))
(defn -main "Retirement" [& args]
(let [current-age (ask "What is your current age?")
retirement-age (ask "When would you like to retire?")
this-year (parse-year (today))]
(println (retire retirement-age current-age this-year))))
For context, the problem i’ve solved:
Your computer knows what the current year is, which means you can incorporate that into your programs. You just have to figure out how your programming language can provide you with that information.
Create a program that determines how many years you have left until retirement and the year you can retire. It should prompt for your current age and the age you want to retire and display the output as shown in the example that follows.
**Example Output**
What is your current age? 25
At what age would you like to retire? 65
You have 40 years left until you can retire.
It's 2015, so you can retire in 2055.
**Constraints**
- Again, be sure to convert the input to numerical data before doing any math.
- Don't hard-code the current year into your program. Get it from the system time via your programming language.
**Challenge**
- Handlesituationswheretheprogramreturnsanegative number by stating that the user can already retire.
I wouldn't reach for a time library for this. You can get the current year like this:
user=> (.getYear (java.time.LocalDate/now))
2023
But otherwise it's all nice and clear.
The require
would normally be inside the ns
:
(ns exercise-6.core
(:require [tick.core :as tick])
(:gen-class))
yea, looks pretty good. One very minor improvement might be to change
(>= 0 years-left)
to
(neg? years-left)
which tests if a number is negative.
(>= 0 years-left)
allows for zero?
or neg?
oh, right.
You could use pos?
and swap the conditions.
Honestly, I would write it as (<= years-left 0)
, but that's just a personal preference and I don't see anything wrong with the way it's currently written.
Some other thoughts that are also pretty minor:
• I would consider extract-year
over parse-year
since parse
makes me think that the function expects a string
• I might change the name of ask
to ask!
to just highlight that the function has I/O and is impure
• I might change retirement-year
to something like retirement-year-response
or similar to show make it apparent that it's not returning a year (ie. number)
• I might change retirement-age-reached
to retirement-age-reached-response
or similar to match
• same with retire
to retire-response
• Doc strings could be helpful.
If you wanted to get fancy you could add some validation and error handling on the responses to ask
.
Wow, thanks so much for all that feedback! exactly what I needed, really good advice from all of you