This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-09-18
Channels
- # announcements (1)
- # babashka (15)
- # beginners (152)
- # calva (28)
- # circleci (1)
- # clj-kondo (24)
- # cljsrn (8)
- # clojure (137)
- # clojure-berlin (3)
- # clojure-czech (2)
- # clojure-dev (20)
- # clojure-europe (69)
- # clojure-finland (5)
- # clojure-france (3)
- # clojure-italy (11)
- # clojure-my (1)
- # clojure-nl (4)
- # clojure-uk (15)
- # clojuredesign-podcast (1)
- # clojurescript (13)
- # conjure (15)
- # cursive (13)
- # datomic (41)
- # deps-new (50)
- # events (1)
- # fulcro (9)
- # graalvm (27)
- # joker (2)
- # kaocha (11)
- # off-topic (22)
- # pathom (48)
- # rdf (6)
- # reagent (25)
- # reitit (47)
- # reveal (10)
- # ring-swagger (1)
- # rum (4)
- # sci (27)
- # shadow-cljs (73)
- # tools-deps (49)
- # vrac (2)
- # xtdb (4)
God morgen! Since the defn-podcast people can’t get their act together and make podcasts at the rate which I consume them, I’ve been forced to search around for those ones. I found a couple of episodes with Michael Nugaard on The Idealcast that I really enjoyed. Also, most episodes of Corecursive are great too. But nothing beats defn.
I still don't feel that we've found a good way to tweak the last element of a vector https://clojurians.slack.com/archives/CBJ5CGE0G/p1600273110363900
and going back to other classics. Did we ever come to a conclusion wrt:
(filter (seq seqy-thing))
vs
(remove (empty? seqy-thing))
I like the latter.
(defn update-last [v f & args]
(conj (vec (butlast v))
(apply f (last v) args)))
(def my-vec [1 2 2])
(update-last my-vec + 2)
;; => [1 2 4]
(defn update-last [v f & args]
(let [cnt (dec (count v))]
(conj (subvec v 0 cnt)
(apply f (get v cnt) args))))
peek
is indeed O(1) on a vector (https://clojurians.slack.com/archives/CBJ5CGE0G/p1600420983007100)
peek it is!
(defn update-last [v f & args]
(let [cnt (dec (count v))]
(conj (subvec v 0 cnt)
(apply f (peek v) args))))
subvec should make a big difference here as it is really just a window onto the existing vector, but it will also prevent the original vector (and thus the previous version of that last element) to be garbage collected, so that could become a memory leak
@plexus I think I might go w/this
(defn update-last [v f & args]
(conj (pop v)
(apply f (peek v) args)))
conj/peek/pop all work on the cheap end of the data structure, which is what I want really
public IPersistentStack pop(){
if(end - 1 == start)
{
return PersistentVector.EMPTY;
}
return new SubVector(_meta, v, start, end - 1);
}
this is what I fumbled my way to previously: https://github.com/MastodonC/witan.cic.driver/blob/b8f5a6d4474b2d0bea23617b6cd9301e1e0fced0/src/witan/cic/episodes.clj#L322-L330
basically I have a yearly report, where the latest year might alter records in previous years
when I'm done, I should have a vector of records with no overlapping intervals (tho they don't have to meet each other)
This will probably break evertything https://github.com/pjstadig/deque-clojure
(let [v []
new-item 1]
(conj (or (and (seq v) (pop v))
[])
new-item))
=> [1]
(let [v [0 0 0 2]
new-item 1]
(conj (or (and (seq v) (pop v))
[])
new-item))
=> [0 0 0 1]
@raymcdermott ooh, I like (and (seq v) (pop v))
as an update-last tho:
(defn update-last [v f & args]
(if (seq v)
(conj
(pop v)
(apply f (peek v) args))
v))
so to answer @plexus's koan, updating the last element of an empty vector returns the empty vector?
@raymcdermott I couldn't figure out how to do the or
pun
I like that you say: the empty vector. It seems there is indeed only one.
user=> (identical? [] (subvec [1 2 3] 0 0))
true
It’s looking good @otfrom
I can't believe we've spent so much time on this. Surely this should be in core somewhere
there is an update-in
this sort of thing is sometimes a smell that you should be using a map rather than a vector
ah, I must have missed the starting point
@ben.hammond up here: https://clojurians.slack.com/archives/CBJ5CGE0G/p1600273110363900
like I said. I'm a bit stunned we've been talking about it so much. I was presuming that I was being a dunderhead (I usually am)
Would transients help?
Not sure if tansients and subvec would work together nicely.
but maybe (let [t (transient v)] (update t (dec (count v)) inc) (persistent! t))
would be faster?!
Ok it’s actually (let [t (transient v) i (dec (count v))] (assoc! t i (inc (get v i))) (persistent! t))
no, not nice
impressive, sure