-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
globals_definition.txt
61 lines (50 loc) · 1.41 KB
/
globals_definition.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(ns playground
(:require
[clojure.string :as str]))
; function
(defn clojure-function [args]
(let [string "multiline\nstring"
regexp #"regexp"
number 100,000
booleans [false true]
keyword ::the-keyword]
;; this is comment
(if true
(->>
(list [vector] {:map map} #{'set})))))
; global
(def some-var)
; another one
(def alternative-var "132")
; defonce
(defonce ^:private another-var #"foo")
; private function
(defn- add [x y] (+ x y))
; protocols
(defprotocol Fly
"A simple protocol for flying"
(fly [this] "Method to fly"))
(defrecord Bird [name species]
Fly
(fly [this] (str (:name this) " flies...")))
; multimethods
(defmulti service-charge (fn [acct] [(account-level acct) (:tag acct)]))
(defmethod service-charge [::acc/Basic ::acc/Checking] [_] 25)
(defmethod service-charge [::acc/Basic ::acc/Savings] [_] 10)
(defmethod service-charge [::acc/Premium ::acc/Account] [_] 0)
; macros
(defmacro unless [pred a b]
`(if (not ~pred) ~a ~b))
(unless false (println "Will print") (println "Will not print"))
; types
(deftype Circle [radius])
(deftype Square [length width])
;; multimethods again
(defmulti area class)
(defmethod area Circle [c]
(* Math/PI (* (.radius c) (.radius c))))
(defmethod area Square [s]
(* (.length s) (.width s)))
;; create a couple shapes and get their area
(def myCircle (Circle. 10))
(def mySquare (Square. 5 11))