Fork me on GitHub
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
            n) n coll))


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

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

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


do you care about "increases" or just changes?

(count (partition-by identity [1 1 2 2 3 3 3 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


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))
          (partition 2 1 coll)))

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

👀 1

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.


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 😅


Another cool solution to this is:

(defn count-inc [coll]                                                                           
  (->> (map < coll (rest coll))                                                                  
       (filter true?)                                                                            
I saw that from this fun video: 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

hello everyone


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


I was wondering about couple of things


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


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


second how can I add those after project creation?


to check what's added, you can click of the "diff" link of your feature: (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


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.


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