Skip to content

Instantly share code, notes, and snippets.

@fgui
Last active October 15, 2015 16:25
Show Gist options
  • Save fgui/65beed6ac5141465fc0a to your computer and use it in GitHub Desktop.
Save fgui/65beed6ac5141465fc0a to your computer and use it in GitHub Desktop.
intro to clojure with repl
(ns clj-bcn.intro1)
(comment
;;; literals ;;;
"string"
\s
1
3/4
123412342134214312342134N
342.3
341234231432142.123421341234123421343214M
true
false
nil
:keyword
()
'(1 2 3)
[1 2 3]
{:key1 "value 1" :key2 "value 2"}
#{1 2 3 4}
;;;; symbol ;;;;
inc
+
clojure-definition
;;;; evaluation ;;;;
(inc 1)
(+ 1)
(+ 1 2 3 4)
(+ 1 (inc 1))
(def sym1 1)
(inc sym1)
(println "hello")
(do (print "hello") (print "world") (print "!"))
(let [x 1] (inc x))
(fn [x] (inc x))
((fn [x] (inc x)) 1)
(def my-inc (fn [x] (inc x)))
(my-inc 1)
(defn my-inc2 [x] (inc x))
(my-inc2 1)
;;; boolean
(if true true false)
(if false true false)
(if nil true false)
(if "anything else" true false)
(if [] true false)
;;; hof
(map inc [1 2 3 4])
(filter even? [1 2 3 4])
(reduce + [1 2 3 4])
;;; special evaluation if (special forms and macros)
(if false (println true) (println false))
;;; quote to avoid evaluation
;;; example error
(1 2 3)
;;; how to avoid evaluation
(quote (1 2 3))
'(1 2 3)
;;; definition clojure
(def clojure-definition
"
Clojure is a dynamic programming language that targets the Java Virtual Machine (and the CLR, and JavaScript). It is designed to be a general-purpose language, combining the approachability and interactive development of a scripting language with an efficient and robust infrastructure for multithreaded programming. Clojure is a compiled language - it compiles directly to JVM bytecode, yet remains completely dynamic. Every feature supported by Clojure is supported at runtime. Clojure provides easy access to the Java frameworks, with optional type hints and type inference, to ensure that calls to Java can avoid reflection.
Clojure is a dialect of Lisp, and shares with Lisp the code-as-data philosophy and a powerful macro system. Clojure is predominantly a functional programming language, and features a rich set of immutable, persistent data structures. When mutable state is needed, Clojure offers a software transactional memory system and reactive Agent system that ensure clean, correct, multithreaded designs.
I hope you find Clojure's combination of facilities elegant, powerful, practical and fun to use.")
(println clojure-definition)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment