Outros links aqui
Last active
November 1, 2019 16:52
-
-
Save adamatti/af7770d8f5edabc199a6b418f6631de9 to your computer and use it in GitHub Desktop.
Exemplos usados no dojo de clojure - 27/04/2017 #dojo #clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) | |
) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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") )) | |
) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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