This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-12-10
Channels
- # adventofcode (68)
- # babashka (6)
- # babashka-sci-dev (3)
- # beginners (11)
- # biff (8)
- # circleci (13)
- # clj-kondo (1)
- # clojure (17)
- # clojure-europe (9)
- # clojurescript (12)
- # core-async (17)
- # cursive (9)
- # data-science (1)
- # datomic (3)
- # emacs (10)
- # fulcro (4)
- # off-topic (3)
- # practicalli (1)
- # releases (3)
- # sql (1)
- # xtdb (30)
Hi all, I just started learning Clojure and Iām wondering if wrapping a function, in order to rethread the received argument is bad practice?
(->> ["This" "is" "," "," "," "my" "example" "vector" "."]
(map (fn [x] (if (or (= x ",") (= x ".")) x (str " " x))))
(apply str)
;; Here I wrap the clojure.string/replace function, so I can pass the
;; argument in from the front. Is this a bad thing to do?
((fn [x] (clojure.string/replace x #",{2,}" ""))))
I would go even one step further: Pull that function out and give it a name so You-in-6-months can still figure out what it is supposed to do, without having to be a human compiler.
(let [remove-2+-commas (fn [x] (str/replace x #",{2,}" ""))
add-spaces (fn [x]
(if (or (= x ",") (= x ".")) x
(str " " x)))]
(->> ["This" "is" "," "my" "example" "vector" "."]
(map add-spaces)
(apply str)
remove-2+-commas))
@U90R0EPHA Thats what I was thinking. Thanks for the example!
@UP82LQR9N. A "top level" as->
could work here
(as-> m x
(map (fn [x] ...) x)
(apply str x)
(clojure.string/replace x #",{2,}" ""))
But large portions of the community would tag that as "not idiomatic"; and I don't think it really improves anything over the anonymous function anyway.
The more common usage, where at->
is used inline, would not work here because of using ->>
instead of ->
, which does not thread into the correct slot of the at->
macro..
(->> []
(map (fn [x]))
(apply str)
;; š£ Unresolved symbol x, because trying to thread
;; in at the end, when value needs to go in at beginning.
(as-> x (clojure.string/replace x #",{2,}" "")))
Another possibility @U04D4J04PFW:
(-> ["This" "is" "," "," "," "my" "example" "vector" "."]
(->> (map (fn [x] (if (or (= x ",") (= x ".")) x (str " " x))))
(apply str))
(clojure.string/replace #",{2,}" ""))
But I agree with @U90R0EPHA that naming those subfunctions is a much clearer approach.Noted, thanks @U04V70XH6 š
Does into
return a lazy sequence? I am pretty sure I read that somewhere but I can't find any info about it when searching -- modified example from the clojure.data.csv
docs,. I'd like to turn each column into a lazy set
(defn read-column
[reader column-index]
(let [data (csv/read-csv reader)]
(->> data
(map #(parse-long (nth % column-index)))
(into #{}))))
well first, sets aren't lazy and second, into puts elements into the collection you give it, which here is a set