Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Would rebinding be objectionable?

Tags:

clojure

Given is this s-exp:

(let [temp 30
      temp (* temp 9/5)
      temp (+ temp 32)]
  temp)

Ignoring the fact that the arithmetic could be inlined, would rebinding temp like this be objectionable in Clojure? Why/Why not?

like image 477
jjpe Avatar asked May 24 '13 19:05

jjpe


2 Answers

This is used in core.clj in the definition of the conditional threading macros for an example. If splitting the construction of a value into segments makes it clear for human consumption then I would confidently say that this is a fine use of this pattern.

It's worth noting that many Clojurians will interpret the word 'rebinding' to mean using the bind function. You are using it accurately, though it has another meaning as well.

like image 123
Arthur Ulfeldt Avatar answered Sep 24 '22 17:09

Arthur Ulfeldt


That's not very functional--in a let every variable should mean something. I would refactor it to this:

(let [celsius 30
      fahrenheit-unnormalized (* celsius 9/5)
      fahrenheit (+ fahrenheit 32)
  fahrenheit)

(I'm ignoring the better solution:)

(+ (* temp 9/5) 32)
like image 27
tsm Avatar answered Sep 26 '22 17:09

tsm