This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-10-20
Channels
- # announcements (10)
- # aws (4)
- # babashka (71)
- # beginners (30)
- # calva (61)
- # cherry (1)
- # cider (16)
- # clj-kondo (3)
- # clj-on-windows (4)
- # cljsrn (1)
- # clojure (28)
- # clojure-austin (2)
- # clojure-bay-area (1)
- # clojure-europe (45)
- # clojure-hungary (1)
- # clojure-nl (1)
- # clojure-norway (26)
- # clojure-sweden (14)
- # clojure-uk (11)
- # clojurescript (39)
- # core-async (3)
- # core-typed (11)
- # datomic (68)
- # fulcro (7)
- # keechma (1)
- # lsp (29)
- # malli (5)
- # off-topic (57)
- # other-languages (13)
- # pathom (4)
- # rdf (7)
- # reagent (7)
- # reitit (6)
- # releases (1)
- # schema (8)
- # shadow-cljs (86)
- # sql (22)
- # squint (1)
- # vim (8)
- # xtdb (12)
anyone using https://github.com/plumatic/schema ? I have few question on it
Ι think most people have moved on to clojure.spec.alpha and malli, so you might get better answers at #C1923ED97. And it's better to just ask the questions, don't ask if you can ask them!
I used it for many years and still honestly prefer it. what's your question?
Thanks for response @U03TPPC0R0T I posted question here https://clojurians.slack.com/archives/C1923ED97/p1666262764849469
Hi there. I'm trying to make a Clojure program that takes a users input and adds it to a list then returns the average of all the floats in the list. The issue I'm having right now is that It won't take user input or add it to the list. Any suggestions?
Data in Clojure is immutable, so concat
returns a new list -- it doesn't modify an existing one.
You're already using loop
in average
-- you'll want a loop
in -main
that accumulates the times. You'll want to turn the input from a string into a number, as well. parse-long
if you're on Clojure 1.11. You don't need the iterator stuff: you can use first
/`rest` in the loop
and (seq l)
to check for empty list of times.
Hope that helps point you in the right direction?
i think average is typically implemented using reduce https://clojuredocs.org/clojure.core/reduce i haven't read user input using the command line, but i remember that JDK has some scan API that makes that easy https://www.programiz.com/java-programming/scanner
you may benefit from reading through the collections API examples on clojure docs, also there are a lot of clojure books that should help starting off. you are writing clojure as if it's java, and clojure does things a bit differently from java, which are a little challenging to explain in a context like a slack Q/A
https://www.youtube.com/watch?v=P76Vbsk_3J0 this may help as well
Thank you pppaul and seancorfield! I don't have time to really look into these at the moment, but I appreciate the help! I'll definitely look at these when I get a minute
Also, it is considered poor form to use def
as you've done so in line 14
Hi all, I am currently trying to "translate" problems that I've solved in oop to Clojure. Just to get an understanding of how these are solved with functional thinking. However, I am off to a rough start. I have a vector of numbers [3 5 3 2 1 2] and I want to return a list with the value of each index being the value of the index + the values of the previous indexes. So my example [3 5 3 2 1 2] will become [3 8 11 13 14 16]. In oop it is possible to solve this in O(n) time. Just using a for loop
int[] nums = new int {3, 5, 3, 2, 1, 2}
int[] resultList = new int[nums.Length];
resultList[0] = nums[0];
for(int i = 1; i<nums.Length; i++){
resultList[i] = resultList[i-1]+nums[i];
}
One could translate this to clojure directly using another forloop and a (range 0 (len nums)) to get the index, but it feels weird to use forloops in clojrue. I wondered if there was another approach maybe by using highorder functions?map-indexed
gets you a sequence and its indices, so you can map over both with a single function, but it sounds like you want map-indexed
+ reduce
here
all you need is the previous value which the accumulator argument to reduce gives you
but there is also a built in function that basically will does this all for you, you can find it using (apropos 'reduc)
in the repl
(let [l [3 5 3 2 1 2]]
(reduce #(conj %1 (-> %1 last (or 0) (+ %2))) [] l))
reduce as an examplethe sort of defining difference between imperative code and functional isn't so much the shape of it, different control structures or whatever, but the use of immutable values