Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(clojure) Add support for global definitions
Adds support for Clojure global definitions with class name `title`, including: * def * defonce * defprotocol * defstruct * defmulti * defmethod * defn * defn- * defmacro * deftype * defrecord
- Loading branch information
Showing
6 changed files
with
170 additions
and
32 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
(<span class="hljs-name"><span class="hljs-builtin-name">ns</span></span> playground | ||
(<span class="hljs-symbol">:require</span> | ||
[clojure.string <span class="hljs-symbol">:as</span> str])) | ||
|
||
<span class="hljs-comment">; function</span> | ||
(<span class="hljs-keyword">defn</span> <span class="hljs-title">clojure-function</span> [args] | ||
(<span class="hljs-name"><span class="hljs-builtin-name">let</span></span> [string <span class="hljs-string">"multiline\nstring"</span> | ||
regexp #<span class="hljs-string">"regexp"</span> | ||
number <span class="hljs-number">100</span>,<span class="hljs-number">000</span> | ||
booleans [<span class="hljs-literal">false</span> <span class="hljs-literal">true</span>] | ||
keyword <span class="hljs-symbol">::the-keyword</span>] | ||
<span class="hljs-comment">;; this is comment</span> | ||
(<span class="hljs-name"><span class="hljs-builtin-name">if</span></span> <span class="hljs-literal">true</span> | ||
(<span class="hljs-name"><span class="hljs-builtin-name">->></span></span> | ||
(<span class="hljs-name"><span class="hljs-builtin-name">list</span></span> [vector] {<span class="hljs-symbol">:map</span> map} #{'set}))))) | ||
|
||
<span class="hljs-comment">; global</span> | ||
(<span class="hljs-keyword">def</span> <span class="hljs-title">some-var</span>) | ||
<span class="hljs-comment">; another one</span> | ||
(<span class="hljs-keyword">def</span> <span class="hljs-title">alternative-var</span> <span class="hljs-string">"132"</span>) | ||
<span class="hljs-comment">; defonce</span> | ||
(<span class="hljs-keyword">defonce</span> ^<span class="hljs-symbol">:private</span> <span class="hljs-title">another-var</span> <span class="hljs-title">#</span><span class="hljs-string">"foo"</span>) | ||
|
||
<span class="hljs-comment">; private function</span> | ||
(<span class="hljs-keyword">defn</span><span class="hljs-title">-</span> <span class="hljs-title">add</span> [x y] (<span class="hljs-name"><span class="hljs-builtin-name">+</span></span> x y)) | ||
|
||
<span class="hljs-comment">; protocols</span> | ||
(<span class="hljs-keyword">defprotocol</span> <span class="hljs-title">Fly</span> | ||
<span class="hljs-string">"A simple protocol for flying"</span> | ||
(<span class="hljs-name">fly</span> [this] <span class="hljs-string">"Method to fly"</span>)) | ||
|
||
(<span class="hljs-keyword">defrecord</span> <span class="hljs-title">Bird</span> [name species] | ||
Fly | ||
(<span class="hljs-name">fly</span> [this] (<span class="hljs-name"><span class="hljs-builtin-name">str</span></span> (<span class="hljs-symbol">:name</span> this) <span class="hljs-string">" flies..."</span>))) | ||
|
||
<span class="hljs-comment">; multimethods</span> | ||
(<span class="hljs-keyword">defmulti</span> <span class="hljs-title">service-charge</span> (<span class="hljs-name"><span class="hljs-builtin-name">fn</span></span> [acct] [(<span class="hljs-name">account-level</span> acct) (<span class="hljs-symbol">:tag</span> acct)])) | ||
(<span class="hljs-keyword">defmethod</span> <span class="hljs-title">service-charge</span> [<span class="hljs-symbol">::acc/Basic</span> <span class="hljs-symbol">::acc/Checking</span>] [_] <span class="hljs-number">25</span>) | ||
(<span class="hljs-keyword">defmethod</span> <span class="hljs-title">service-charge</span> [<span class="hljs-symbol">::acc/Basic</span> <span class="hljs-symbol">::acc/Savings</span>] [_] <span class="hljs-number">10</span>) | ||
(<span class="hljs-keyword">defmethod</span> <span class="hljs-title">service-charge</span> [<span class="hljs-symbol">::acc/Premium</span> <span class="hljs-symbol">::acc/Account</span>] [_] <span class="hljs-number">0</span>) | ||
|
||
<span class="hljs-comment">; macros</span> | ||
(<span class="hljs-keyword">defmacro</span> <span class="hljs-title">unless</span> [pred a b] | ||
`(<span class="hljs-name"><span class="hljs-builtin-name">if</span></span> (<span class="hljs-name"><span class="hljs-builtin-name">not</span></span> ~pred) ~a ~b)) | ||
|
||
(<span class="hljs-name">unless</span> <span class="hljs-literal">false</span> (<span class="hljs-name">println</span> <span class="hljs-string">"Will print"</span>) (<span class="hljs-name">println</span> <span class="hljs-string">"Will not print"</span>)) | ||
|
||
<span class="hljs-comment">; types</span> | ||
(<span class="hljs-keyword">deftype</span> <span class="hljs-title">Circle</span> [radius]) | ||
(<span class="hljs-keyword">deftype</span> <span class="hljs-title">Square</span> [length width]) | ||
|
||
<span class="hljs-comment">;; multimethods again</span> | ||
(<span class="hljs-keyword">defmulti</span> <span class="hljs-title">area</span> <span class="hljs-title">class</span>) | ||
(<span class="hljs-keyword">defmethod</span> <span class="hljs-title">area</span> <span class="hljs-title">Circle</span> [c] | ||
(<span class="hljs-name"><span class="hljs-builtin-name">*</span></span> Math/PI (<span class="hljs-name"><span class="hljs-builtin-name">*</span></span> (<span class="hljs-name">.radius</span> c) (<span class="hljs-name">.radius</span> c)))) | ||
(<span class="hljs-keyword">defmethod</span> <span class="hljs-title">area</span> <span class="hljs-title">Square</span> [s] | ||
(<span class="hljs-name"><span class="hljs-builtin-name">*</span></span> (<span class="hljs-name">.length</span> s) (<span class="hljs-name">.width</span> s))) | ||
|
||
<span class="hljs-comment">;; create a couple shapes and get their area</span> | ||
(<span class="hljs-keyword">def</span> <span class="hljs-title">myCircle</span> (<span class="hljs-name">Circle.</span> <span class="hljs-number">10</span>)) | ||
(<span class="hljs-keyword">def</span> <span class="hljs-title">mySquare</span> (<span class="hljs-name">Square.</span> <span class="hljs-number">5</span> <span class="hljs-number">11</span>)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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)) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
(<span class="hljs-name"><span class="hljs-builtin-name">def</span></span> +x [(<span class="hljs-name">a</span> <span class="hljs-number">1</span>) <span class="hljs-number">+2</span> <span class="hljs-number">-3.0</span> y-5]) | ||
(<span class="hljs-keyword">def</span> <span class="hljs-title">+x</span> [(<span class="hljs-name">a</span> <span class="hljs-number">1</span>) <span class="hljs-number">+2</span> <span class="hljs-number">-3.0</span> y-5]) |