Advertisement
Guest User

little-clojure-demo

a guest
Feb 27th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defn twice
  2.   "make the number twice as large, have an ugly side effect of println, return it"
  3.   [x]
  4.     (println (str "Doubling the value of " x))
  5.     (* 2 x))
  6.  
  7. (defn do-stuff
  8.   "this is how i define a function in clojure"
  9.   [limit]
  10.   (let [all-numbers-starting-from-zero (range)
  11.         to-the-limit (take limit all-numbers-starting-from-zero)
  12.         multiplied-by-two (map twice to-the-limit)]
  13.  
  14.     (println (str "Input numbers " (clojure.string/join "," to-the-limit)))
  15.     (println (str "Smallest of twiced " (apply min multiplied-by-two)))
  16.     (println (str "Biggest of twiced " (apply max multiplied-by-two)))
  17.     (println (str "Sum of twiced " (reduce + multiplied-by-two)))
  18.  
  19.     ))
  20.  
  21.  
  22. (defn -main
  23.   "I don't do a whole lot ... yet."
  24.   [& args]
  25.   (do-stuff 5))
  26.  
  27. ;; Output of the code above
  28. ; Input numbers 0,1,2,3,4
  29. ; Doubling the value of 0
  30. ; Doubling the value of 1
  31. ; Doubling the value of 2
  32. ; Doubling the value of 3
  33. ; Doubling the value of 4
  34. ; Smallest of twiced 0
  35. ; Biggest of twiced 8
  36. ; Sum of twiced 20
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement