Function Composition
We have discussed how functional programs are essentially a number of functions that work together, this is called composition (functional composition).
(let [calculated-value (* 10 (reduce + (map inc (range 5))))]
calculated-value)
This expression is common in the Lisp & Clojure languages. Occasionally the created expressions can becomes challenging to read. To overcome this parsing complexity, developers often break down a more complex expression into its parts, extracting code into its own function.
Note Brake down the above example into each expression that gives a value
(range 5)
(map inc (range 5))
(reduce + (map inc (range 5)))
(* 10 (reduce + (map inc (range 5))))
;; Additional examples
;; Use a let expression for code that is used more than once in a function
(let [calculated-value (* 10 (reduce + (map inc (range 5))))]
calculated-value)
;; Use defn to define a function for code that multiple functions will call
;; and generalise the function with arguments
(defn common-data-calculation
[certanty-factor scope]
(* certanty-factor (reduce + (map inc (range scope)))))
当前内容版权归 practicalli 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 practicalli .