Fork me on GitHub
#beginners
<
2022-08-21
>
Eugene Mosh10:08:50

Hello friends! Form (range) generate lazy sequence 0 1 2 3 4 5 6 ... How to generate sequence where removed all products of 5? 0 1 2 3 4 6 7 8 9 11 12 ... Thanks!

Ben Sless10:08:40

`(filter pred (range))`

Ben Sless10:08:06

Where `pred` is `#(zero? (mod % 5))`

Eugene Mosh10:08:58

Is it possible to do this only with tools of clojure.core?

Lidor Cohen11:08:03

I think the proposed solution uses only clojure.core

minor correction, filter works the other way. Either use `pos?` not `zero?` , or change `filter` to `remove` , depending on readability for the problem. Then look at `(source remove)` .

Ben Lieberman18:08:27

I'm trying to traverse a sequence of integers and count the number of times the successive integer increases. I have this but I'm missing something I can't quite figure out

``````(defn count-inc [coll n]
(reduce (fn [line nxt]
(if-not (zero? (- nxt line))
(inc n) ;; I want to increment n if change
nxt)
n) n coll))``````

Chase18:08:51

Hmmm, maybe you are getting false negatives for when the successive integer is actually lower (so therefore also not zero)? Not sure what `n` is supposed to represent either. Is it the running count? My quick solution (maybe super inefficient) :

``````(defn count-inc [coll]
(->> (partition 2 1 coll)
(filter (fn [[a b]]
(> b a)))
(count)))  ``````

Ben Lieberman18:08:46

in theory `n` would be the counter but I suppose if I implemented this properly it would be superfluous

Martin Půda18:08:08

And you can remove parentheses from `count`- this also works:

``````(defn count-inc [coll]
(->> (partition 2 1 coll)
(filter (fn [[a b]]
(> b a)))
count)) ``````

Ben Lieberman18:08:50

that's a cool solution but I'm not wrong for assuming there is a way to do this with `reduce` right? The docs do say it can do pairwise operations on a sequence, so it seemed like a good fit. but the way I have it set up it returns 0. so I'll tinker with it regardless

dpsutton18:08:20

do you care about "increases" or just changes?

``````(count (partition-by identity [1 1 2 2 3 3 3 4]))
4``````

Ben Lieberman18:08:25

increases. (it's the first AoC 2021 problem fwiw). I've been struggling with sequences and laziness so I am trying to do stuff as fundamentally as I can

dpsutton18:08:38

have the other solutions posted here helped your understanding? do you understand why your original attempt did not work?

Martin Půda18:08:34

You can also do this:

``````(defn count-inc [coll]
(reduce (fn [n [a b]]
(if (> b a) (inc n) n))
0
(partition 2 1 coll)))

(count-inc [0 2 4 6])
=> 3``````

👀 1
Chase18:08:00

Nice. I was struggling coming up with a reduce solution. I was trying to do it without `partition` to simulate if you didn't know that existed.

Chase18:08:29

It helps to remember the first variable in the 2 variable function passed to reduce should be the "accumulator" so if you eventually want `n` than that should be the first variable and then proceed from there. Is that a good way to think about it?

Ben Lieberman18:08:07

Ah yes thank you for that reminder @U9J50BY4C, I can't believe I overlooked that part. I use reduce all the time in Python and JS and somehow forgot that that must be true 😅

Chase20:08:58

Another cool solution to this is:

``````(defn count-inc [coll]
(->> (map < coll (rest coll))
(filter true?)
count))``````
I saw that from this fun video: https://www.youtube.com/watch?v=dFaEUefIDJQ which also goes into some more advanced stuff with transducers. AOC is so fun and I always learn neat things from it. I was wondering how I kind of immediately knew how to solve this problem and of course it was because I had already done so. haha

👀 1
cvetan19:08:36

hello everyone

cvetan19:08:38

I am just starting with luminus and clojure, as I am working on a litlle colledge project

cvetan19:08:48

I was wondering about couple of things

cvetan19:08:55

first when you are creating luminus project, you are adding those dependencies with +mysql + swagger and so on

cvetan19:08:06

how can I check what that actually brings to project in terms of what is added in dependencies file?

cvetan19:08:14

second how can I add those after project creation?

rolt23:08:46

to check what's added, you can click of the "diff" link of your feature: https://github.com/luminus-framework/luminus-template/blob/master/README.md#usage (or manually run with and without the feature and run the diff locally) to add those after project creation, you have to do it manually as far as i know, copy paste

skylize01:08:01

Doesn't Luminus use Leiningen? So dependencies will be recorded in `project.clj` in the root directory of your project. If you were not using Lein, then you would probably use tools.deps which tracks dependencies in `deps.edn` file.

skylize01:08:37

You can generally find what text to add to that file by searching for a library on http://clojars.org and/or in the readme of a project's public git repo.