This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-10-06
Channels
- # announcements (1)
- # babashka (118)
- # beginners (108)
- # calva (20)
- # chlorine-clover (3)
- # clara (10)
- # clj-kondo (47)
- # cljsrn (10)
- # clojure (144)
- # clojure-australia (5)
- # clojure-czech (2)
- # clojure-dev (11)
- # clojure-europe (94)
- # clojure-france (4)
- # clojure-nl (5)
- # clojure-spec (3)
- # clojure-sweden (2)
- # clojure-uk (11)
- # clojuredesign-podcast (5)
- # clojurescript (56)
- # cloverage (1)
- # code-reviews (6)
- # conjure (1)
- # cursive (13)
- # datascript (1)
- # datomic (16)
- # defnpodcast (1)
- # emacs (1)
- # etaoin (1)
- # events (7)
- # fulcro (21)
- # graalvm (3)
- # helix (17)
- # jackdaw (3)
- # jobs-discuss (2)
- # lambdaisland (2)
- # london-clojurians (2)
- # meander (2)
- # mid-cities-meetup (2)
- # midje (1)
- # off-topic (1)
- # pathom (3)
- # pedestal (12)
- # reagent (7)
- # reitit (15)
- # reveal (12)
- # shadow-cljs (6)
- # sql (14)
- # test-check (3)
- # vim (13)
I listened to the partition-by
& group-by
episode and one thing you mentioned is using partition-by
for "splitting" a sequence into two portions.
Wouldn't split-with
be a more suitable for that purpose? https://stackoverflow.com/questions/64208708/replicate-java-peek-stream-method-in-clojure
Yes, if you know that you will only get two portions, then split-with would work as well
For split-with
, you pass a predicate, and the collection is cut when that predicate goes from true to false
For partition-by
, you pass a function and a new partition is made every time the function returns a different value
These can behave the same, if the partition-by
function returns true
and then false
and the data is sorted conveniently:
(split-with #(< % 2) (range 5)) => [(0 1) (2 3 4)]
(partition-by #(< % 2) (range 5)) => [(0 1) (2 3 4)]
However, when the data isn't sorted, you get different results:
(split-with #(< % 2) [1 4 3 0 2]) => [(1) (4 3 0 2)]
(partition-by #(< % 2) [1 4 3 0 2]) => ((1) (4 3) (0) (2))
Looks like it depends on which data structure you're using, and laziness. Stackoverflow question was specifically about a side effect, so I think maybe they could use a queue instead of a list. I'm curious as well about the difference.
For split-with
, you pass a predicate, and the collection is cut when that predicate goes from true to false
For partition-by
, you pass a function and a new partition is made every time the function returns a different value
These can behave the same, if the partition-by
function returns true
and then false
and the data is sorted conveniently:
(split-with #(< % 2) (range 5)) => [(0 1) (2 3 4)]
(partition-by #(< % 2) (range 5)) => [(0 1) (2 3 4)]
However, when the data isn't sorted, you get different results:
(split-with #(< % 2) [1 4 3 0 2]) => [(1) (4 3 0 2)]
(partition-by #(< % 2) [1 4 3 0 2]) => ((1) (4 3) (0) (2))