clojure fundamentals course for beginners

16
One of the Most Popular Course on Clojure - Clojure Fundamentals For Beginners Write shorter codes that do so much more with Clojure. Clojure is the perfect blend of a general-purpose programming language and a functional programming language. Created by Rich Hickey, the programming language combines the interactive development of a scripting language with that of a robust infrastructure for multi- threading programming. This course covers the fundamentals of Clojure as well as teach you how to start coding using the language. The following slides will let you know, all that the course encompasses.

Upload: paddy-lock

Post on 18-Jan-2017

922 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Clojure Fundamentals Course For Beginners

One of the Most Popular Course on Clojure - Clojure Fundamentals For Beginners

Write shorter codes that do so much more with Clojure. Clojure is the perfect blend of a general-purpose programming language and a functional programming language. Created by Rich Hickey, the programming language combines the interactive development of a scripting language with that of a robust infrastructure for multi-threading programming.

This course covers the fundamentals of Clojure as well as teach you how to start coding using the language. The following slides will let you know, all that the course encompasses.

Page 2: Clojure Fundamentals Course For Beginners

Clojure and Java Clojure runs on the JVM.Java is an incredibly verbose language that has only limited support for functions via lambda’sClojure is an incredibly terse languageBy incorporating Clojure into Java code we can greatly reduce the verbosity and allow important functional tools like recursionBy incorporating Java into Clojure code allows use of Java’s extensive native functionality and its enormous ecosystemThe ability to use Java classes, objects, and methods is called Java interophttp://clojure.org/reference/java_interop

Page 3: Clojure Fundamentals Course For Beginners

Clojure and JavaScript Developers don't just write assembly to write a desktop applications it is just not feasable. JavaScript is ubiquitous.Developers use compilers to take a high level language to assemblyIs Javascript like the assembly language of the web.Javascript is a high-level, dynamic languageWriting high-performance JavaScript is a challengeJavascript does not really have shared memory model, it has webworkers can only pass very limited set of data, strings or JSON objects By using Compilers we can take JavaScript to output JavaScript as properly formed and otherwise cross-browser compatible code

Page 4: Clojure Fundamentals Course For Beginners

Clojure Application Packaging Software Engineering Build Toolscompiling computer source code into binary codepackaging binary coderunning testsdeployment to production systemscreating documentation and/or release notes Uber Jar Package your Clojure project into one self-contained file. (a Java “uber” jar file.)The JVM has a classloader, which takes the compiled bytecode and loads it into the running JVM. If the byte code for your dependencies is not available at runtime then your application fails with a “class not found exception”.“Uber jar” is a jar file of all Clojure source code compiled to bytecode and all the projects dependencies.

Page 5: Clojure Fundamentals Course For Beginners

Clojure Functions Function defn( defn square [x] ( * x x))https://clojuredocs.org/clojure.core/range( range 10)Higher order function ‘map‘(map square ( range 10) )  Core functions data flow for expressions Core function = str(def myMap {:firstKey "firstValue", :secKey "secValue"})(str "first key value: " (:firstKey myMap ) " second key value: " (:secKey myMap )) Higher order function ‘apply ‘ (apply str [ "one" "two" "three" ]) is the same as : (str "one" "two" "three") (str [ "one" "two" "three" ]) 

Page 6: Clojure Fundamentals Course For Beginners

Clojure Macros 

Macros use the source code as data (input) like functions use data

"thread-first" macro(-> "first" (str " last"))(str "first" " last")

"thread-last" macro(->> " last" (str " first"))

Thread macro takes an expression to a form f(source code) -> source code‘->’ : “term” ===> Form‘-->’ : “term” ===> Forminserts the first term as the first /last item in second form inserts the first form as the first /last item in second form

Page 7: Clojure Fundamentals Course For Beginners

Clojure Memory Model Clojure is a dialect of Lisp that runs on the JVM.(Common Language Runtime (like JVM) and Javascript engines) ObjectivesWhat is a memory model why is it neededJMM what is it how does it workSTM what is it how does it work

Page 8: Clojure Fundamentals Course For Beginners

Clojure Process core-async Summary Clojure ConcurrencyVarsImmutable by defaultMutable call dynamic (def ^:dynamic *my-str-type* "mutable variable")As a mutable variable have thread local scope (visibility)

AtomsAre visible (synchronised)(def my-atom (atom 10)) de-reference @my-atom (reset! my-atom 12) functional (swap! my-atom update )Use swap! in STM block

STMRefs Agents

Refs ACID Transcational AtomsUpdate with alter in synchronised block (dosync (translate-point ))

AgentsRefs that update asynchronously (def my-agent (agent 10))No strict consistency for reads(send my-agent update-my-agent)  

Page 9: Clojure Fundamentals Course For Beginners

Clojure Web Applications Ring is a Clojure web applications library inspired by Python's WSGI and Ruby's Rack

Compojure is a rest api for rapid development of web applications in Clojure https://github.com/weavejester/compojure In compojure, each route is an HTTP method paired with a URL-matching patternCompojure route definitions are just functions configured for Ring (accept request maps and return response maps) 1. Use a template lein new compojure eddy_comp2. run the Ring web server lein ring server3. defroutes site-defaults src/eddy_compjure/handler.clj4. :keys :plugins :ring project.clj5. Access the context root localhost:30006. JSON https://github.com/dakrone/cheshire7. ClojureScript configuration8. ClojureScript rest client

Page 10: Clojure Fundamentals Course For Beginners

Functional Composition

Arity

arity 2(def make-a-set

(fn ([x] #{x})([x y] #{x y})))

(make-a-set 1)(make-a-set 1 2)

Variadic functions(defn var-args [x & rest] (apply str (butlast rest) ) )(var-args 0 1 2 3 4)

 

Currying Currying is a way to generate a new function with an argument partially appliedpartial is a way of currying partialhttps://clojuredocs.org/clojure.core/partial 

(def make-a-set (fn ([x y] #{x y})))( (partial make-a-set 2) 3 ) What if we want to create a function not just a form (def make-a-set-5 (partial make-a-set 5))(make-a-set-5 10)

Page 11: Clojure Fundamentals Course For Beginners

Clojure Concurrent tools: Atoms, Refs, Vars 

Objectives

1. What is a memory model why is it needed2. JMM what is it how does it work3. STM what is it how does it workResult

4. memory model is a set of rules implemented in hardware and software that controls the way variables are updated during program execution by multiple threads of execution

5. Java memory model (JMM) is a set of low level concurrency artifacts (locks, atomic variables and synchronisation(visibility) ) combined with high level frameworks (Executor Service, Futures, ForkJoin, …...) to implement a memory model.

6. Software Transactional Memory (STM) an ACID transactional system for how a variable that is shared between threads updates, STM is implemented with the low

Page 12: Clojure Fundamentals Course For Beginners

Functional recipe: Pure functions with Immutable Data Structures Pure FunctionsSide effects = changes that functions make in addition to their return valuePure functions do not depend on external data sources and do not provoke side effects of any kind. When invoked with a set of arguments, will always respond withthe same return value

Higher-Order Functions ● Takes one or more functions as arguments ● Returns a function as a result Clojure has families of higher order functions <==> Clojure AbstractionsEg sequence functions map reduce filtersequence <==> Data Structure Abstraction {sequence}

Page 13: Clojure Fundamentals Course For Beginners

Gradle Environment1 install gradle.bashrcexport GRADLE_HOME=/home/ubu/gradleexport PATH=$PATH:$GRADLE_HOME/bin

export JAVA_HOME=/usr/lib/jvm/java-8-oracle

.bashrcexport JAVA_HOME=/usr/lib/jvm/java-8-oracleexport GRADLE_HOME=/home/ubu/gradleexport PATH=$PATH:$GRADLE_HOME/binsource .bashrc

Page 14: Clojure Fundamentals Course For Beginners

Leiningen Environment Software Engineering Build Tools 

Compiling computer source code into binary codePackaging binary codeRunning testsDeployment to production systemsCreating documentation and/or release notes 

Objectives

1. Install leningen2. Understand Leiningen project layout3. Understand Leiningen build script (dependencies ect ..)4. Run the clojure repl

Page 15: Clojure Fundamentals Course For Beginners

Functional Recursive Data FlowRecursion is one of the fundamental techniques used in functional programming.ReviewFunction literal((fn [x y] #{x y}) 1 2)Function arity 2(def make-a-set

(fn ([x] #{x})([x y] #{x y})))

(make-a-set 1)(make-a-set 1 2)

 def special form is a way to assign a symbolic name to a piece of Clojure data

Page 16: Clojure Fundamentals Course For Beginners

Thank Youwww.eduonix.com

Source Linkhttps://www.eduonix.com/courses/Software-Development/clojure-fundamentals-for-beginners