Fork me on GitHub

anyone using ? 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!

🙌 1
☝️ 2

Feels like I just got transported to IRC. Are you a bot, @UEQPKG7HQ? 😁

Annaia Danvers14:10:55

I used it for many years and still honestly prefer it. what's your question?

Damian Wilding20:10:44

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 i haven't read user input using the command line, but i remember that JDK has some scan API that makes that easy


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

Damian Wilding21:10:25

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


You can use a let binding there instead


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?


this isn't really oop, just imperative code

💯 2

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


you don't actually need the index at all


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


looping with a counter is fine too


(let [l [3 5 3 2 1 2]]
  (reduce #(conj %1 (-> %1 last (or 0) (+ %2))) [] l))
reduce as an example


the 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


Ah I see. Thats extremely helpfull. Thank you all 🙂


so you can write a loop that iterates over a counter, but you also need to keep the state of the new structure you are building up (because you aren't mutating an existing array), and possible a sort of cache of the running total


is that an imperative factorial I see there?


oh no, that'd be the products


(reductions + [3 5 3 2 1 2]) (i'm providing this solution for the sake of completion but as a beginner the reflex should be trying to solve it with reduce or loop/recur)