Skip to content

Instantly share code, notes, and snippets.

@adamatti
Last active Nov 1, 2019
Embed
What would you like to do?
Exemplos usados no dojo de clojure - 27/04/2017 #dojo #clojure
(ns dojo-27-04.core-test
(:require [clojure.test :refer :all]
[dojo-27-04.core :refer :all]))
(defn- test-fatorial [n]
(case n
0 1
1 1
(* n (test-fatorial (- n 1)))
)
)
(deftest test-fatorial-0
(is (= (test-fatorial 0) 1))
)
(deftest test-fatorial-1
(is (= (test-fatorial 1) 1))
)
(deftest test-fatorial-2
(is (= (test-fatorial 2) 2))
)
(deftest test-fatorial-2
(is (= (test-fatorial 3) 6))
)
(ns teste.core-test
(:require [clojure.test :refer :all]
[teste.core :refer :all]))
(defn juiz [var1 var2]
(cond
(and (= "papel" var1) (= "pedra" var2)) "papel"
(and (= "pedra" var1) (= "tesoura" var2)) "pedra"
(and (= "pedra" var1) (= "papel" var2)) "papel"
(and (= "tesoura" var1) (= "pedra" var2)) "pedra"
(= var1 var2 ) var1
:else "tesoura"
)
)
(deftest a-test
(testing "Papel ganha de pedra"
(is (= "papel" (juiz "papel" "pedra") ))
)
(testing "Pedra ganha de tesoura"
(is (= "pedra" (juiz "pedra" "tesoura") ))
)
(testing "Tesoura ganha de papel"
(is (= "tesoura" (juiz "tesoura" "papel") ))
)
(testing "Pedra perde de papel"
(is (= "papel" (juiz "pedra" "papel") ))
)
(testing "Tesoura perde de pedra"
(is (= "pedra" (juiz "tesoura" "pedra") ))
)
(testing "Tesoura empata com tesoura"
(is (= "tesoura" (juiz "tesoura" "tesoura") ))
)
(testing "Papel empata com papel"
(is (= "papel" (juiz "papel" "papel") ))
)
)
(use 'clojure.repl)
(println "abc")
(= 2 2.0)
(== 3 3.0 3/1)
(+ 1 2 3)
;;;;;;;;;;;;;;;;;;;;;;;;;;;; list
(def mylist (list 1 2 3))
(rest '(1 2 3))
(= (peek mylist) (first mylist))
(cons 4 mylist)
(conj mylist 4)
(count mylist)
(nth mylist 1)
(subvec (vec mylist) 0 2)
;;;;;;;;;;;;;;;;;;;;;;;;;;;; vector
(count [1 2 3])
(vec mylist)
;;;;;;;;;;;;;;;;;;;;;;;;;;;; try catch
(try (/ 1 0) (catch Exception e "Error"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;; set
#{0 1 2}
(set mylist)
;;;;;;;;;;;;;;;;;;;;;;;;;;;; map
(def mymap {:a 1 :b 2 :c 3})
(= mymap (hash-map :a 1))
(get mymap :a)
(:a mymap)
(get mymap :b :key-not-found)
;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions
(defn test-func [var]
(println (str "My var" var))
)
(test-func "x")
(= 4 ((fn [x] (* x x)) 2))
(= 4 ((fn [x] (x 2 2)) +))
(= 4 (#(* % %) 2))
;;;;;;;;;;;;;;;;;;;;;;;;;;;; conditionals
(if (false? (= 1 2 3)) "T" "F")
(if (nil? 0) "null")
; if-not / not
(== 1 1.0 (* 1 1))
(cond
(= 1 2) "first"
(= 1 3) "second"
:else "else"
)
(case "a"
"a" "é a"
"b" "é b"
"not found"
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; map / reduce / filter
; range / take / drop / repeat
(for [index (range 10) :when (< index 5)] (println index))
(.toUpperCase (String. "new string"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment