concept side effect in category clojure

appears as: side effect, side effects, side effect, side effects, The side effect, The side effects
Clojure: The Essential Reference

This is an excerpt from Manning's book Clojure: The Essential Reference.

  • fn is used under the hood by defn to generate the body of the function and implement destructuring. Differently from defn, fn does not create a var object or alter the current namespace as a side effect. Thus fn is a better choice for local use of functions without the need for an external name. fn is often used with sequential operations such as reduce to create an anonymous function of two arguments.
  • The presence of & and | in Java has mainly to do with the possibility of executing side effecting expressions independently from the result of the previous evaluations. Clojure is definitely pushing toward a more pure approach trying to isolate side effects, so it comes to no surprise that there is no such operator in Clojure.

    Outside the REPL use, dotimes is often connected to the execution of side effects. The locally bound variable provided with dotimes is a perfect fit for array access operations. The following example shows a faster version of the fizz-buzz game presented in the “condp” chapter:

    (require '[criterium.core :refer [quick-bench]])
    
    (defn fizz-buzz-for [n]                     ; #1
      (condp #(zero? (mod %2 %1)) n
        15 "fizzbuzz"
        3 "fizz"
        5 "buzz"
        n))
    
    (defn fizz-buzz-slow [n]                    ; #2
      (doall (map fizz-buzz-for (range n))))
    
    (defn fizz-buzz [n]
      (let [res (transient [])]                 ; #3
        (dotimes [i n]
          (assoc! res i (fizz-buzz-for i)))     ; #4
        (persistent! res)))                     ; #5
    
    (quick-bench (fizz-buzz-slow 1000))
    ;; Execution time mean : 34.320885 µs
    
    (quick-bench (fizz-buzz 1000))
    ;; Execution time mean : 28.308923 µs

    As you can see from the benchmark, there is a noticeable speed up by using a transient [76]. dotimes is a perfect choice to perform the side effect of adding elements to the vector, including the necessary incrementing index.

    Get Programming with Clojure

    This is an excerpt from Manning's book Get Programming with Clojure.

    sidebar

    When we introduce side effects in the next Unit, we will see that when a function has several side effects, it sometimes makes sense to have several expressions inside the body of a function.

    Figure 13.4 A box diagram for impure functions. Arrows that are directed from the outside toward the the box indicate a factor that influences the behaviour of the function. Arrows that are directed from the box toward the outside indicates an influence of the function. Impure functions either have side effects or depend on external factors.

    In order to understand why nil is displays by the REPL when we evaluate (println "Hello"), we need to clarify the distinction between the side effects of an expression and the return value of an expression. The distinction between side effects and return value is a key idea of functional programming.

  • The side effect of (println "Hello") is to display "Hello" to the console with an end of line character