I would like to know how to increment by X amount a number, in other languages I used to do
foo += 0.1;
but I have not idea how it can be done in Clojure
I would like to know how to increment by X amount a number, in other languages I used to do
foo += 0.1;
but I have not idea how it can be done in Clojure
Blacksad's answer covers defining vars so I would just like to add the other scopes in which you may wish to have a value that is incremented from another value:
within a function's local scope:
and If you want to build a value incrementally to make the process more clear:
This can make the process more presentable, though it is not a "way to get variables back" and is really only useful in limited contexts. This pattern is used in clojure.core's threading macros.
Variables are immutable in Clojure. So you should not try to change the value of
foo
, but instead, "create" a new foo:...or, if in a loop, recur with a new value:
...or, if foo is an atom,
swap!
it with a new value:I recommend you start by reading the rationale of Clojure.