This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-02-26
Channels
- # asami (5)
- # babashka (9)
- # beginners (19)
- # calva (14)
- # cider (13)
- # clj-kondo (9)
- # cljfx (2)
- # clojure (51)
- # clojure-bay-area (1)
- # clojure-europe (26)
- # clojurescript (23)
- # code-reviews (7)
- # core-async (1)
- # datahike (4)
- # datalevin (3)
- # emacs (2)
- # kaocha (2)
- # malli (6)
- # nrepl (3)
- # polylith (3)
- # re-frame (15)
- # reveal (1)
- # shadow-cljs (13)
- # tools-deps (6)
Seeking advice on whether I should exploit the symmetry in the following code:
swapped ((enigma-updated :plugboard) input-code)
forward-passed
(loop [index-from-right right-most-index
translated swapped]
(if (< index-from-right 0)
translated
(recur (dec index-from-right)
(pass-rotor (nth rotors index-from-right) :forward translated))))
reflected ((enigma-updated :reflector) forward-passed)
backward-passed
(loop [index-from-left 0
translated reflected]
(if (< right-most-index index-from-left)
translated
(recur (inc index-from-left)
(pass-rotor (nth rotors index-from-left) :backward translated))))
I want to iterate over a sequence of rotors, once forward, and once backward, applying transformation with each rotor (pass-rotor) with the transformed outcome fed into the next iteration's rotor.
Depending on the direction, the transformation is different.
The two loops are symmetry, one forward, another backward, I wonder if it's a good idea to abstract them to make the code more compact.
But I'm afraid that it might be harder to read.
Maybe, there is already abstraction, or idiom to handle such symmetry?
My current idea is that I might introduce a concept of the direction of iteration, then using the direction as parameter,
to compute the operations related the direction, such as index comparison, etc.
Maybe, I'm over thinking, and there might be already better solution.You can also split up into adjacent pairs (using (partition)
, then map over the pairs, but this won't work for your use-case because the previous element is transformed, and paired with the next element.
try to frame your approach in terms of simpler abstractions like sequential lists, rather than collections + indexes that happen to be sequential
;; ---->
(reduce (fn [transformed cur] (pass-rotor transformed cur :backward)) swapped rotors)
;; <----
(reduce (fn [transformed cur] (pass-rotor transformed cur :backward)) result-of->pass (rseq rotors))
user=> (def coll [:a :b :c :d])
#'user/coll
user=> (seq coll)
(:a :b :c :d)
user=> (rseq coll)
(:d :c :b :a)
It's so elegant! Here is the resulted improvement (the commented code is the old/bad one):
forward-passed
(reduce (fn [transformed curr] (pass-rotor curr alphabets-length :forward transformed)) swapped (rseq rotors))
;; (loop [index-from-right right-most-index
;; translated swapped]
;; (if (< index-from-right 0)
;; translated
;; (recur (dec index-from-right)
;; (pass-rotor (nth rotors index-from-right) alphabets-length :forward translated))))
reflected (get
(get-in enigma-updated [:reflector :map])
forward-passed forward-passed)
backward-passed
(reduce (fn [transformed curr] (pass-rotor curr alphabets-length :backward transformed)) reflected rotors)
;; (loop [index-from-left 0
;; translated reflected]
;; (if (< right-most-index index-from-left)
;; translated
;; (recur (inc index-from-left)
;; (pass-rotor (nth rotors index-from-left) alphabets-length :backward translated))))
Thanks for the teaching!Up to what point is it ok to go with just plain maps over records?
I'm not sure that you ever have to stop using maps. There are performance advantages for protocol implementations on records, but I've never needed to swap for these reasons. What are your concerns?
Nothing much, just wondering what general style and conventions are
it's lovely to me that Clojure is so much data oriented
Yeah, it's my preference too. I've very rarely needed records and types in my time with Clojure.
https://cemerick.com/blog/2011/07/05/flowchart-for-choosing-the-right-clojure-type-definition-form.html also speaks to the strengths and stability of the language that this flowchart from over a decade ago is still relevant and useful đŻ
I'm struggling to find the right words but it's bonkers how "useable" Clojure is