Fork me on GitHub
#beginners
<
2022-10-20
>
popeye09:10:46

anyone using https://github.com/plumatic/schema ? I have few question on it

pavlosmelissinos09:10:34

Ι 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
skylize13:10:51

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

2
1
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?

seancorfield20:10:18

Data in Clojure is immutable, so concat returns a new list -- it doesn't modify an existing one.

seancorfield20:10:44

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.

seancorfield20:10:54

Hope that helps point you in the right direction?

pppaul20:10:36

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

pppaul20:10:43

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

craftybones02:10:05

Also, it is considered poor form to use def as you've done so in line 14

craftybones02:10:15

You can use a let binding there instead

1
V21:10:59

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?

hiredman21:10:19

this isn't really oop, just imperative code

💯 2
respatialized21:10:20

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

hiredman21:10:42

you don't actually need the index at all

hiredman21:10:36

all you need is the previous value which the accumulator argument to reduce gives you

hiredman21:10:37

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

hiredman21:10:19

looping with a counter is fine too

MegaMatt21:10:57

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

hiredman21:10:37

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

V21:10:23

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

hiredman21:10:36

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

ahungry21:10:32

is that an imperative factorial I see there?

ahungry21:10:40

oh no, that'd be the products

rolt21:10:41

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