Side Effects in Lisp / Clojure

My question is about structuring lisp code with side effects. The specific example that I have in mind relates to Clojure, but I think it can be applied to any lisp.

In this case, I am interacting with an existing library that requires some functions to be called in a specific order. The final function call creates the value that I need for the rest of the procedure.

The code is as follows:

(defn foo [] 
    (let [_ procedure-with-side-effect
          __ another-procedure-with-side-effect
          value procedure-that-creates-the-value]
        (do-something value))) 

This works, and everything is fine, except that the let block looks disgusting. Is there a better way to do this?

+4
source share
3 answers

Lisp . () . , a LET . procedure-that-creates-the-value , , LET .

, Lisp :

(defun foo ()
  (procedure-with-side-effect)
  (another-procedure-with-side-effect)
  (do-something (procedure-that-creates-the-value)))
+5

, defn:

(defn foo [] 
  (procedure-with-side-effect)
  (another-procedure-with-side-effect)
  (do-something (procedure-that-creates-the-value)))

, . let:

(let [val 3]
     (fun-call-1)
     (fun-call-2)
     (fun-call-3 val))

- , do:

(do (fun-call-1)
    (fun-call-2)
    (fun-call-3))
+9

, :

(defn foo [] 
  (procedure-with-side-effect)
  (another-procedure-with-side-effect)
  (let [value (procedure-that-creates-the-value)]
    (do-something value)))

(defn foo [] 
  (procedure-with-side-effect)
  (another-procedure-with-side-effect)
  (-> (procedure-that-creates-the-value)
      do-something))

(defn foo [] 
  (procedure-with-side-effect)
  (another-procedure-with-side-effect)
  (do-something (procedure-that-creates-the-value)))

Edit: expressions defnare implicit do.

+5
source

Source: https://habr.com/ru/post/1530535/


All Articles