This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-08-21
Channels
- # announcements (20)
- # beginners (31)
- # biff (8)
- # cherry (5)
- # cider (4)
- # cljs-dev (1)
- # clojure (26)
- # clojure-australia (2)
- # clojure-europe (16)
- # clojure-spec (10)
- # community-development (8)
- # conjure (1)
- # core-async (1)
- # data-oriented-programming (1)
- # data-science (54)
- # datascript (10)
- # fulcro (1)
- # graalvm (2)
- # malli (5)
- # off-topic (3)
- # pathom (23)
- # rdf (1)
- # re-frame (6)
- # reitit (11)
- # shadow-cljs (6)
- # squint (2)
- # xtdb (33)
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!
Thanks!
Is it possible to do this only with tools of clojure.core?
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)
.
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))
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)))
in theory n
would be the counter but I suppose if I implemented this properly it would be superfluous
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))
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]))
4
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?
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
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?
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?)
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. hahaI am just starting with luminus and clojure, as I am working on a litlle colledge project
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?
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
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 http://clojars.org and/or in the readme of a project's public git repo.