This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
- # announcements (10)
- # aws (38)
- # beginners (220)
- # calva (2)
- # cider (26)
- # clj-kondo (194)
- # cljs-dev (4)
- # clojure (190)
- # clojure-dev (7)
- # clojure-europe (3)
- # clojure-italy (6)
- # clojure-nl (4)
- # clojure-uk (8)
- # clojured (1)
- # clojurescript (29)
- # code-reviews (31)
- # community-development (9)
- # core-async (24)
- # cursive (38)
- # data-science (51)
- # datomic (52)
- # dirac (2)
- # emacs (3)
- # events (1)
- # figwheel-main (4)
- # fulcro (49)
- # graphql (13)
- # heroku (1)
- # hoplon (19)
- # immutant (3)
- # leiningen (1)
- # nrepl (59)
- # off-topic (12)
- # onyx (2)
- # pathom (51)
- # reitit (15)
- # shadow-cljs (88)
- # spacemacs (6)
- # sql (3)
- # tools-deps (107)
- # xtdb (11)
I would love to get some feedback on my
run length encoding solution from exercism if possible. It passes all tests but doesn't seem very elegant. I feel like a lot of the "hackiness" comes from dealing with the edge case where you only have one char. https://pastebin.com/4ZFw5dQC
I also find myself wanting to put detailed expected shape of input data and output data to make it clearer but when trying to put examples in the doc strings I'm getting a lot of push back from the repl. How do you folks deal with that?
also not sure if there is a way to link to the generic exercism page showing the problem statement if that helps with advice.
when I try to put stuff like `Example input (\A \A) -> output "2A") it yells at the use of the back slash and quotes.
so then I'm trying to throw in more back slashes as escape thingies but it turns into an unreadable mess
yeah. I would like if anything in the doc string is just ignored but it seems when I evaluate the function in my repl if I've added doc strings like the example above it no longer works.
There's some tickets around for heredoc, but I think they're unlikely to happen. I've never found it so much of a problem though. Characters are an unfortunate edge case.
ok cool. So one thing I've found with reading clojure (including stuff I wrote a week ago) is not understanding what the expected data looks like. Maybe I'm making too big a deal out of that though.
lol, right?! That's how I feel reading everyone's clojure code. Maybe that's how I feel reading any code though, I don't know
at least my confidence is growing that I'll be able to hack up a solution to a problem now. I'm just not so sure I'll understand my own solution after a little time has passed. baby steps I guess
I keep thinking if I could just see the shape of expected data for input and output I would be in a good place. Haven't figured out the best way to solve that desire. I guess if I find some code in the wild I could try and get it in the repl and play around with it.
Anyways, thank you for the chat! I'm still open on suggestions for how to improve this solution too
yeah, it doesn't sit right with me. The thing is input can be like "AAABCCC" but the output can't be "3A1B3C"
Your code fails if something appears more than 9 times. This would require the length to be 2 digits, and you're only ever considering one.
You can use the fact that
mapcat treats nils as empty seqs to improve the special case a bit, and also, regex groups to help you separate the count from the element instead of using
butlast eliminating the bug I mentioned above. Some destructuring later, here's what I ended up with:
Sorry if irrelevant or too late now 😀
(defn encode [txt] (->> (partition-by identity txt) (mapcat #(let [[n x] ((juxt count first) %)] [(when (> n 1) n) x])) (apply str))) (defn decode [encoded] (->> (re-seq #"(\d+)?([a-zA-Z\s])" encoded) (mapcat (fn [[_ n x]] (repeat (Long/parseUnsignedLong (or n "1")) x))) (apply str)))
juxt might be going too overboard and into golfing land. You could do:
(defn encode [txt] (->> (partition-by identity txt) (mapcat #(let [n (count %) x (first %)] [(when (> n 1) n) x])) (apply str)))
thanks for the advice! I got some feedback from the exercism mentor too so will be doing some refactoring with both your suggestions in mind.
My solution does work for occurrences more than 9 times though. I remember having to go back and tweak it when getting that failed test. But now it passes. I think that's what the
+ after my
\d regex covers.