5 dicas para ajudal-o a programmar in clojure sem stress
Konrad Scorciapino
@konr
0. ambiente de desenvolvimento ligado ao REPL
funcção não funcciona
↓
*mudança*
↓
feedback immediato
rodar hua só funcção
(defn novo-pedido [pedido]
(let [campos (extrair-campos-uteis pedido)
adequada (adequar campos)]
(salvar adequada))
user> (novo-pedido amostra)
..alguma coisa..
user>
o velho println
(defn novo-pedido [pedido]
(let [campos (extrair-campos-uteis pedido)
_ (println campos) ;; <--------------
adequada (adequar campos)]
(println adequada) ;; <---------------
(salvar adequada))
user> (novo-pedido amostra)
..CAMPOS..
..ADEQUADA..
..alguma coisa..
user>
exploração no repl
user> (extrair-campos-uteis pedido)
..CAMPOS..
user> (adequar (extrair-campos-uteis pedido))
..ADEQUADA..
user> (salvar (adequar (extrair-campos-uteis pedido)))
..RESULTADO..
user> (-> pedido)
..PEDIDO..
user> (-> pedido extrair-campos-uteis)
..CAMPOS..
user> (-> pedido extrair-campos-uteis adequar)
.. ADEQUADA..
user> (-> pedido extrair-campos-uteis adequar salvar)
..RESULTADO..
tools.namespace
user=> (use '[clojure.tools.namespace.repl :only (refresh)])
nil
user=> (refresh)
:reloading (com.example.util com.example.app com.example.app-test)
:ok
emacs + lein
> cd /tmp
> lein new meu-projecto
...
>
1. abrir o archivo no Emacs
2. nrepl-jack-in
o repl abriu!
3. C-c C-k
4. Rodar
5. Voltar pra janella, alterar
6. C-c C-k
7. Rodar!
1. recursos do editor de texto
Emacs
(defvar elpa-packages '(starter-kit ido starter-kit-lisp starter-kit-bindings
erc-hl-nicks ercn yaml-mode dired+ ;; clj
clojure-mode nrepl evil evil-leader evil-paredit ac-nrepl paredit rainbow-delimiters auto-complete))
clojure-mode
- Cores,
-
Indentação
nrepl.el
- jack-in
- restart
-
eval-buffer
rainbow-delimiters
auto-complete & ac-nrepl
...
paredit
evil
2. clojure.repl
use
user> (use 'clojure.repl)
nil
user> (use 'clojure.pprint)
nil
pst
user> (/ 1 0)
ArithmeticException Divide by zero clojure.lang.Numbers.divide (Numbers.java:156)
user> (pst)
ArithmeticException Divide by zero
clojure.lang.Numbers.divide (Numbers.java:156)
clojure.lang.Numbers.divide (Numbers.java:3691)
user/eval2826 (NO_SOURCE_FILE:1)
clojure.lang.Compiler.eval (Compiler.java:6619)
...
nil
user>
apropos
user> (apropos "def")
(deftest- deftest default-handler default-middlewares defmethod defstruct defrecord with-redefs definline default-data-readers defn- *verbose-defrecords* with-redefs-fn defn defmacro *default-data-reader-fn* deftype definterface defmulti defonce defprotocol default-streams-impl)
user>
doc
user> (doc defmulti)
-------------------------
clojure.core/defmulti
([name docstring? attr-map? dispatch-fn & options])
Macro
Creates a new multimethod with the associated dispatch function.
The docstring and attribute-map are optional.
Options are key-value pairs and may be one of:
:default the default dispatch value, defaults to :default
:hierarchy the isa? hierarchy to use for dispatching
defaults to the global hierarchy
nil
user>
source
user> (source ->)
(defmacro ->
"Threads the expr through the forms. Inserts x as the
second item in the first form, making a list of it if it is not a
list already. If there are more forms, inserts the first form as the
second item in second form, etc."
{:added "1.0"}
([x] x)
([x form] (if (seq? form)
(with-meta `(~(first form) ~x ~@(next form)) (meta form))
(list form x)))
([x form & more] `(-> (-> ~x ~form) ~@more)))
nil
user>
pprint
user> (pprint '(ns meu-projecto.test (:require [midje.sweet :refer :all] [clojure.pprint :refer :all] [meu-projecto.utils :refer :all] [meu-projecto.core :refer :all ])))
(ns meu-projecto.test (:require [midje.sweet :refer :all] [clojure.pprint :refer :all] [meu-projecto.utils :refer :all] [meu-projecto.core :refer :all])) nil user>
print-table
user> (print-table [{:nome "Konrad" :altura "2m"} {:nome "Pedro" :altura "1.75m"} {:nome "Erick" :altura "1.62m"}])
| :altura | :nome |
|---------+--------|
| 2m | Konrad |
| 1.75m | Pedro |
| 1.62m | Erick |
nil
user>
3. seqüestrar variaveis
Truque super util
(def hijacked (ref []))
(defn hijack [thing]
(dosync (alter hijacked conj thing))
thing)
No meio de ũa requisição
(defn nuclear-bomb-access-code [context]
(hijack context)
(let [blablabla ...
inner-thing (hijack ...)
...]
(hijack (final-function final-result)))
N`hum loop
(doseq [bla blablabla]
(hijack bla)
...)
4. ecosystema das interwebs
clojars
clojuredocs
#clojure @ freenode
DuckDuckGo
obvio que o stackoverflow tambem
Dê ũa duckada:
!so clojure android
:)
dicas
By Konrad Scorciapino
dicas
- 1,295