Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Learning Aids/Tools 2 years ago
Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
I'm new to Clojure, and I've been translating some data manipulation work I did recently as an aid to learning. I've got a function translation that works fine, and is shorter, but feels much less readable. Can anyone suggest a more readable and/or more idiomatic way to handle this?
In Python:
def createDifferenceVector(v,startWithZero=True): deltas = [] for i in range(len(v)): if i == 0: if startWithZero: deltas.append(0.0) else: deltas.append(v[0]) else: deltas.append(v[i] - v[i-1]) return deltas
My attempt at a Clojure translation:
(defn create-diff-vector [v start-zero] (let [ext-v (if start-zero (cons (first v) v) (cons 0 v))] (for [i (range 1 (count ext-v))] (- (nth ext-v i) (nth ext-v (- i 1))))))
It could be that it's less readable just because of my inexperience with Clojure, but in particular, the trick of prepending an element to the input vector feels to me like it obscures the intention. All the solutions I tried which didn't use the prepending trick were much longer and uglier.
Many sequence transformations are incredibly elegant in Clojure, but the ones that I find challenging so far are ones like this one, which a) lend themselves to manipulation by index rather than by element, and/or b) require special handling for certain elements.
Thanks for any suggestions.
Idiomatic Clojure tends to manipulate the sequences as a whole, rather than individual elements. You could define create-diff-vector in English as:
create-diff-vector
The result is a vector consisting of:
start-zero
The second part can be illustrated thusly: for the input (31 41 59 26 53), we have
(31 41 59 26 53)
input without the first element: (41 59 26 53) - input without the last element: (31 41 59 26) =================================================== result: (10 18 -33 27)
Which, translated to Clojure, becomes remarkably concise:
(defn diff-vector [v start-zero?] (into [(if start-zero? 0 (first v))] (map - (rest v) v))))
A few points to note:
start-zero?
map
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.