This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-06-29
Channels
- # announcements (1)
- # beginners (40)
- # calva (30)
- # cider (3)
- # clj-kondo (1)
- # cljdoc (12)
- # clojure (79)
- # clojure-dev (7)
- # clojure-france (4)
- # clojure-spec (10)
- # clojure-uk (13)
- # clojurescript (19)
- # core-async (1)
- # core-typed (1)
- # cursive (2)
- # datomic (2)
- # emacs (3)
- # events (1)
- # kaocha (2)
- # klipse (14)
- # off-topic (26)
- # pathom (1)
- # reagent (8)
- # rewrite-clj (8)
- # shadow-cljs (22)
- # tools-deps (18)
(defn partition-by-comp [f [x & xs]]
(let [[_ acc result]
(reduce (fn [[prev acc result] curr]
(if (f curr prev)
[curr (conj acc curr) result]
[curr [curr] (conj result acc)]))
[x [x] []]
xs)]
(if (not-every? seq [xs acc]) result (conj result acc))))
(defn f53 [xs]
(->> (conj (partition-by-comp #(= %1 (inc %2)) xs) [])
(remove #(= 1 (count %)))
(sort-by #(- (count %)))
(first)))
(deftest- f53t
(is (= (f53 [1 0 1 2 3 0 4 5]) [0 1 2 3]))
(is (= (f53 [5 6 1 3 2 7]) [5 6]))
(is (= (f53 [2 3 3 4 5]) [3 4 5]))
(is (= (f53 [7 6 5 4]) [])))
Very interesting. Much nicer than my loop recur approach
I had a play with this too…
(defn partition-when [f coll]
;; Pattern copied from `clojure.core/partition-by`.
(lazy-seq
(when-let [s (seq coll)]
(let [run (cons (first s)
(take-while (comp not f)
(next s)))]
(cons run
(partition-when f (lazy-seq (drop (count run) s))))))))
(defn longest-increasing-sub-seq [s]
(let [dummy-small-number (- (first s) 1)
pairs (partition 2
1
(cons dummy-small-number s))
subseqs-of-pairs (partition-when #(apply >= %)
pairs)
subseqs (map #(map second %)
subseqs-of-pairs)
longest-subseq (reduce (fn [sofar v]
(if (> (count v) (count sofar))
v
sofar))
[]
subseqs)]
(if (>= (count longest-subseq) 2)
longest-subseq
[])))
Nice! I like how you made your partition lazy. “Read through stdlib sources” very much on my todo list for learning patterns :)
The reduce approach very much inspired by advice I got on the channel recently when I was trying to solve a similar issue. This slack is awesome ❤️
Hey! Apparently today is meant to be one of those unbritish days ... undecided if I want to risk getting fried to a crisp outside
We're trying to get out the door for a run before it gets too hot
I have put thermal blankets on all the windows to keep out the heat, it’s going to be 32 degrees today in London