Clojure lein use notes

Submitted by code_admin on Wed, 07/25/2018 - 15:52

Creating a new project

  1. lein new app my-app

Test

https://github.com/jakemcc/lein-test-refresh

  1. lein test-refresh

Test using spies to check for side affects

https://engineering.fundingcircle.com/blog/2016/01/11/tdd-in-clojure/

  1. (deftest crappy-function
  2.   (let [calls (atom [])]
  3.     (with-redefs [some-namespace/some-function (fn [arg] (swap! calls conj arg) "I am really crappy")]
  4.       (is (= "I am really crappy" (crappy-function mock-function 3 5)))
  5.       (is (= @calls [{:a-key 3 :another-key 5}]))
  6.     )
  7.   )
  8. )

Lein Try

https://til.hashrocket.com/posts/3081d5943b-try-a-clojure-project-in-th…

add plugin to ~/.lein/profiles.clj
My file is:

  1. {:user {:plugins [[lein-try "0.4.3"]]}}

type lein try PLUGIN
e.g.

  1. lein try seesaw

seesaw list events

Run the repl in a seesaw project

  1. (use 'seesaw.dev)
  2. (show-events (combobox))

Memorize example

  1. (ns try.core
  2.   (:gen-class)
  3.   (:require [clojure.string :as str] )
  4. )
  5.  
  6.  
  7. (def stringA "all abc")
  8. (def stringB "sadsdadsa abc")
  9. (def stringC "@p1 abc")
  10. (def stringD "@p2 abc")
  11.  
  12. (defn remove-particular-start-DONOTUSE
  13.   [input-string prefix-to-remove]
  14.   (do
  15.     (println (str "CALL function:" input-string ":" prefix-to-remove))
  16.     (if (str/starts-with? (str/lower-case input-string) (str/lower-case (str prefix-to-remove " ")))
  17.       (subs input-string (+ 1 (count prefix-to-remove)))
  18.       input-string
  19.     )
  20.   )
  21. )
  22. (def remove-particular-start
  23.   (memoize remove-particular-start-DONOTUSE)
  24. ;  remove-particular-start-DONOTUSE
  25. )
  26.  
  27. (defn -main
  28.   "I don't do a whole lot ... yet."
  29.   [& args]
  30.   (do
  31.     (println "Start of app")
  32.     (println (remove-particular-start stringA "all"))
  33.     (println (remove-particular-start stringB "all"))
  34.     (println (remove-particular-start stringC "all"))
  35.     (println (remove-particular-start stringD "all"))
  36.     (println (remove-particular-start stringA "p1"))
  37.     (println (remove-particular-start stringB "p1"))
  38.     (println (remove-particular-start stringC "p1"))
  39.     (println (remove-particular-start stringD "p1"))
  40.     (println "SECOND CALL")
  41.     (println (remove-particular-start stringA "all"))
  42.     (println (remove-particular-start stringB "all"))
  43.     (println (remove-particular-start stringC "all"))
  44.     (println (remove-particular-start stringD "all"))
  45.     (println (remove-particular-start stringA "p1"))
  46.     (println (remove-particular-start stringB "p1"))
  47.     (println (remove-particular-start stringC "p1"))
  48.     (println (remove-particular-start stringD "p1"))
  49.     (println "End of app")
  50.   )
  51. ) ;end of main function

Tags

RJM Article Type
Quick Reference