by Arne Brasseur

Wikipedia:
A WORKBENCH is a sturdy table at which manual work is done. They range from simple to complex designs that may be considered tools in themselves. Their size and design varies depending on the type of work that is being done.


Editor!
As a Clojure programmer, what’s the most important tool in your toolbox?
As a Clojure programmer, what’s the most important tool in your toolbox?
REPL
Because it’s a meta-tool.
It’s a tool that provides access to tools.
It’s a tool that lets you build your own tools.
(loop []
(-> (read *in*)
(eval)
(println))
(recur))
String -> Data
(read *in*)
;;=> [1 2 3]
(read-str "[1 2 3]")
;;=> [1 2 3]
Data -> Data
(eval '(+ 1 2))
;;=> 3
Data -> String
(prn [1 2 3])
;; *out*| [1 2 3]
(prn-str [1 2 3])
;;=> "[1 2 3]"
(-> "[1 2 3]"
read-string
prn-str
read-string
prn-str
(= "[1 2 3]"))
;;=> true
user=> (def identity (comp read-string prn-str))
user=> (def identity (comp read-string prn-str))
#'user/identity
user=> (def identity (comp read-string prn-str))
#'user/identity
user=> (identity 1)
user=> (def identity (comp read-string prn-str))
#'user/identity
user=> (identity 1)
1
user=> (def identity (comp read-string prn-str))
#'user/identity
user=> (identity 1)
1
user=> (identity [1 2 3])
user=> (def identity (comp read-string prn-str))
#'user/identity
user=> (identity 1)
1
user=> (identity [1 2 3])
[1 2 3]
#'user/identity
user=> (identity 1)
1
user=> (identity [1 2 3])
[1 2 3]
user=> (identity {:meetup :Taipei})
user=> (identity 1)
1
user=> (identity [1 2 3])
[1 2 3]
user=> (identity {:meetup :Taipei})
{:meetup :Taipei}

Explore 探索 Observe 觀察
Control 控制 Change 改變
user=> (require 'clj-slack.users)
user=> (require 'clj-slack.users)
nil
user=> (require 'clj-slack.users)
nil
user=> (def conn {:api-url "https://slack.com/api"})
user=> (require 'clj-slack.users)
nil
user=> (def conn {:api-url "https://slack.com/api"})
#'user/conn
user=> (require 'clj-slack.users)
nil
user=> (def conn {:api-url "https://slack.com/api"})
#'user/conn
user=> (def res (clj-slack.users/list conn))
user=> (require 'clj-slack.users)
nil
user=> (def conn {:api-url "https://slack.com/api"})
#'user/conn
user=> (def res (clj-slack.users/list conn))
#'user/res
nil
user=> (def conn {:api-url "https://slack.com/api"})
#'user/conn
user=> (def res (clj-slack.users/list conn))
#'user/res
user=> (type res)
user=> (def conn {:api-url "https://slack.com/api"})
#'user/conn
user=> (def res (clj-slack.users/list conn))
#'user/res
user=> (type res)
clojure.lang.PersistentList
#'user/conn
user=> (def res (clj-slack.users/list conn))
#'user/res
user=> (type res)
clojure.lang.PersistentList
user=> (length res)
user=> (def res (clj-slack.users/list conn))
#'user/res
user=> (type res)
clojure.lang.PersistentList
user=> (length res)
12532
#'user/res
user=> (type res)
clojure.lang.PersistentList
user=> (length res)
12532
user=> (type (first res))
user=> (type res)
clojure.lang.PersistentList
user=> (length res)
12532
user=> (type (first res))
clojure.lang.PersistentArrayMap
clojure.lang.PersistentList
user=> (length res)
12532
user=> (type (first res))
clojure.lang.PersistentArrayMap
user=> (keys (first res))
user=> (length res)
12532
user=> (type (first res))
clojure.lang.PersistentArrayMap
user=> (keys (first res))
(:id :name :real_name :is_admin :is_owner :profile)
user=> (keys reloaded.repl/system)
user=> (keys reloaded.repl/system)
(:http :datomic :handler :mail-queue)
user=> (keys reloaded.repl/system)
(:http :datomic :handler :mail-queue)
user=> (.totalMemory (java.lang.Runtime/getRuntime))
user=> (keys reloaded.repl/system)
(:http :datomic :handler :mail-queue)
user=> (.totalMemory (java.lang.Runtime/getRuntime))
315621376
user=> (keys reloaded.repl/system)
(:http :datomic :handler :mail-queue)
user=> (.totalMemory (java.lang.Runtime/getRuntime))
315621376
user=> (:roles (user-by-login (db) "plexus"))
user=> (keys reloaded.repl/system)
(:http :datomic :handler :mail-queue)
user=> (.totalMemory (java.lang.Runtime/getRuntime))
315621376
user=> (:roles (user-by-login (db) "plexus"))
(:admin :member)
user=> (go)
user=> (go)
Starting system...
user=> (go)
Starting system...
user=> (reset)
user=> (go)
Starting system...
user=> (reset)
Suspending...
user=> (go)
Starting system...
user=> (reset)
Suspending...
Reloading files...
user=> (go)
Starting system...
user=> (reset)
Suspending...
Reloading files...
Restarting...
Starting system...
user=> (reset)
Suspending...
Reloading files...
Restarting...
user=> (add-workers! 5)
user=> (reset)
Suspending...
Reloading files...
Restarting...
user=> (add-workers! 5)
nil
Suspending...
Reloading files...
Restarting...
user=> (add-workers! 5)
nil
user=> (enable-maintenance-mode!)
Reloading files...
Restarting...
user=> (add-workers! 5)
nil
user=> (enable-maintenance-mode!)
:enabled
user=> (alter-var-root! application
user=> (alter-var-root! application
assoc-in [:api :token] "new-token")
user=> (alter-var-root! application
assoc-in [:api :token] "new-token")
#'application
user=> (alter-var-root! application
assoc-in [:api :token] "new-token")
#'application
user=> (defn previously-buggy-function [& args]
user=> (alter-var-root! application
assoc-in [:api :token] "new-token")
#'application
user=> (defn previously-buggy-function [& args]
new-definition-that-fixes-the-bug)
user=> (alter-var-root! application
assoc-in [:api :token] "new-token")
#'application
user=> (defn previously-buggy-function [& args]
new-definition-that-fixes-the-bug)
#'previously-buggy-function
Explore 探索 Observe 觀察
Control 控制 Change 改變
All of these combined are used to create (production code, tests, data)
Your “workbench” should provide handy access to your “tools”.
Don’t walk to the other side of the room each time you need a screwdriver.
Get yourself a Connected REPL.

Basic operation: eval form in source file
*in* how do you select a form?*out* where does the result go?*in* how do you select a form?(defn ,,,)*out* where does the output go?By inserting the REPL result back into the buffer, it can be used again as data or code.
We have come full circle, the printed form can be read again.


§
user.clj *REPL helpers that should always be available.
lein repl automatically loads this file.
Remember: a workbench needs to be convenient, comfortable, easy to use.
* may also be called something else
user.clj ideas(add-dependency [some-jar "0.1.0"])(db), (conn)(last-request), (last-response)user.clj Reloaded WorkflowCombination of code reloading (clojure.tools.namespace), and some lifecycle manager (Component, Integrant, Mount)
Provides a way to get to a clean slate
Why: because you don’t want to have to restart your REPL process!
(start) (stop) (reset)
user.clj Pomegranate / AlembicHot load dependencies
Good for quickly trying out dependencies, or for adding new libraries to the project.
Again: prevent having to restart the process
(add-dependency '[some-jar "0.1.0"])
(load-project)
user.clj Data helpersCreate helpers for things that play a prominent role in your app
(last-request)
(conn)
(db)
Store request/response history in memory, so you can have
user=> (request)
Store request/response history in memory, so you can have
user=> (request)
{:uri "/" :request-method :get ,,,}
Store request/response history in memory, so you can have
user=> (request)
{:uri "/" :request-method :get ,,,}
user=> (response)
Store request/response history in memory, so you can have
user=> (request)
{:uri "/" :request-method :get ,,,}
user=> (response)
{:status 200 :body ,,,}
Store request/response history in memory, so you can have
user=> (request)
{:uri "/" :request-method :get ,,,}
user=> (response)
{:status 200 :body ,,,}
user=> (session-id)
Store request/response history in memory, so you can have
user=> (request)
{:uri "/" :request-method :get ,,,}
user=> (response)
{:status 200 :body ,,,}
user=> (session-id)
"abcxyz"
Store request/response history in memory, so you can have
{:uri "/" :request-method :get ,,,}
user=> (response)
{:status 200 :body ,,,}
user=> (session-id)
"abcxyz"
user=> (session)
Store request/response history in memory, so you can have
user=> (response)
{:status 200 :body ,,,}
user=> (session-id)
"abcxyz"
user=> (session)
{,,,}
When debugging you might want to evaluate forms inside a function, but local variables will be a problem.
letdef the valuesvvvvalvalval/scope-captureuser=> (require 'sc.api)
user=> (require 'sc.api)
nil
user=> (require 'sc.api)
nil
user=> (defn sum [a b] (sc.api/spy (+ a b)))
user=> (require 'sc.api)
nil
user=> (defn sum [a b] (sc.api/spy (+ a b)))
SPY <-1> :1
user=> (require 'sc.api)
nil
user=> (defn sum [a b] (sc.api/spy (+ a b)))
SPY <-1> :1
At Code Site -1, will save scope with locals [a b]
user=> (require 'sc.api)
nil
user=> (defn sum [a b] (sc.api/spy (+ a b)))
SPY <-1> :1
At Code Site -1, will save scope with locals [a b]
#'user/sum
nil
user=> (defn sum [a b] (sc.api/spy (+ a b)))
SPY <-1> :1
At Code Site -1, will save scope with locals [a b]
#'user/sum
user=> (sum 10 20)
user=> (defn sum [a b] (sc.api/spy (+ a b)))
SPY <-1> :1
At Code Site -1, will save scope with locals [a b]
#'user/sum
user=> (sum 10 20)
SPY [1 -1] :1
SPY <-1> :1
At Code Site -1, will save scope with locals [a b]
#'user/sum
user=> (sum 10 20)
SPY [1 -1] :1
At Execution Point 1, Code Site -1, saved locals [a b]
At Code Site -1, will save scope with locals [a b]
#'user/sum
user=> (sum 10 20)
SPY [1 -1] :1
At Execution Point 1, Code Site -1, saved locals [a b]
SPY [1 -1] :1
#'user/sum
user=> (sum 10 20)
SPY [1 -1] :1
At Execution Point 1, Code Site -1, saved locals [a b]
SPY [1 -1] :1
(+ a b) => 30
user=> (sum 10 20)
SPY [1 -1] :1
At Execution Point 1, Code Site -1, saved locals [a b]
SPY [1 -1] :1
(+ a b) => 30
30
SPY [1 -1] :1
At Execution Point 1, Code Site -1, saved locals [a b]
SPY [1 -1] :1
(+ a b) => 30
30
user=> (sc.api/letsc 1 (+ a b))
At Execution Point 1, Code Site -1, saved locals [a b]
SPY [1 -1] :1
(+ a b) => 30
30
user=> (sc.api/letsc 1 (+ a b))
30
SPY [1 -1] :1
(+ a b) => 30
30
user=> (sc.api/letsc 1 (+ a b))
30
user=> (sum 99 42)
(+ a b) => 30
30
user=> (sc.api/letsc 1 (+ a b))
30
user=> (sum 99 42)
SPY [2 -1] :1
30
user=> (sc.api/letsc 1 (+ a b))
30
user=> (sum 99 42)
SPY [2 -1] :1
At Execution Point 2, Code Site -1, saved locals [a b]
user=> (sc.api/letsc 1 (+ a b))
30
user=> (sum 99 42)
SPY [2 -1] :1
At Execution Point 2, Code Site -1, saved locals [a b]
SPY [2 -1] :1
30
user=> (sum 99 42)
SPY [2 -1] :1
At Execution Point 2, Code Site -1, saved locals [a b]
SPY [2 -1] :1
(+ a b) => 141
user=> (sum 99 42)
SPY [2 -1] :1
At Execution Point 2, Code Site -1, saved locals [a b]
SPY [2 -1] :1
(+ a b) => 141
141
SPY [2 -1] :1
At Execution Point 2, Code Site -1, saved locals [a b]
SPY [2 -1] :1
(+ a b) => 141
141
user=> (sc.api/letsc 2 a)
At Execution Point 2, Code Site -1, saved locals [a b]
SPY [2 -1] :1
(+ a b) => 141
141
user=> (sc.api/letsc 2 a)
99
Create a repl/ or scratch/ directory, with per-topic “repl files”
Encourages experimentation (go ahead, make a mess)
Explicitly keeps history
Shows thought process
Demonstrates usage
Provides a home for useful snippets
Using a REPL does not absolve you of writing tests
Using a REPL is in many ways like writing tests, call a function with various inputs
→ Make a habit of using eval-and-insert, then later copy to test namespace
REPL/scratch file can also be a good source for test inspiration
The End
有問題嗎?
討論一下!
lambdaisland.com/coupon/CLJTPE2018
33% off - forever