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.
Clojure and Java Clojure runs on the JVM. Java is an incredibly verbose language that has only limited support for functions via lambda’s Clojure is an incredibly terse language By incorporating Clojure into Java code we can greatly reduce the verbosity and allow important functional tools like recursion By incorporating Java into Clojure code allows use of Java’s extensive native functionality and its enormous ecosystem The ability to use Java classes, objects, and methods is called Java interop http://clojure.org/reference/java_interop
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 assembly Is Javascript like the assembly language of the web. Javascript is a high-level, dynamic language Writing high-performance JavaScript is a challenge Javascript 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
Clojure Application Packaging Software Engineering Build Tools compiling computer source code into binary code packaging binary code running tests deployment to production systems creating 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.
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" ])
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” ===> Form
inserts the first term as the first /last item in second form inserts the first form as the first /last item in second form
Clojure Memory Model Clojure is a dialect of Lisp that runs on the JVM. (Common Language Runtime (like JVM) and Javascript engines) Objectives What is a memory model why is it needed JMM what is it how does it work STM what is it how does it work
Clojure Process core-async Summary Clojure Concurrency Vars Immutable by default Mutable call dynamic (def ^:dynamic *my-str-type* "mutable variable") As a mutable variable have thread local scope (visibility) Atoms Are 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
STM Refs Agents Refs ACID Transcational Atoms Update with alter in synchronised block (dosync (translate-point )) Agents Refs that update asynchronously (def my-agent (agent 10)) No strict consistency for reads (send my-agent update-my-agent)
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 pattern Compojure route definitions are just functions configured for Ring (accept request maps and return response maps) 1. Use a template lein new compojure eddy_comp 2. run the Ring web server lein ring server 3. defroutes site-defaults src/eddy_compjure/handler.clj 4. :keys
:plugins
:ring
project.clj
5. Access the context root localhost:3000 6. JSON https://github.com/dakrone/cheshire
7. ClojureScript configuration 8. ClojureScript rest client
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 applied partial is a way of currying partial https://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)
Clojure Concurrent tools: Atoms, Refs, Vars  Objectives
1. What is a memory model why is it needed 2. JMM what is it how does it work 3. STM what is it how does it work Result
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
Functional recipe: Pure functions with Immutable Data Structures
Pure Functions Side effects = changes that functions make in addition to their return value Pure 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 with the 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 Abstractions Eg sequence functions map reduce filter sequence <==> Data Structure Abstraction {sequence}
Gradle Environment 1 install gradle .bashrc export GRADLE_HOME=/home/ubu/gradle export PATH=$PATH:$GRADLE_HOME/bin
export JAVA_HOME=/usr/lib/jvm/java-8-oracle
.bashrc export JAVA_HOME=/usr/lib/jvm/java-8-oracle export GRADLE_HOME=/home/ubu/gradle export PATH=$PATH:$GRADLE_HOME/bin source .bashrc
Leiningen Environment
Software Engineering Build Tools
Compiling computer source code into binary code Packaging binary code Running tests Deployment to production systems Creating documentation and/or release notes
Objectives
1. 2. 3. 4.
Install leningen Understand Leiningen project layout Understand Leiningen build script (dependencies ect ..) Run the clojure repl
Functional Recursive Data Flow Recursion is one of the fundamental techniques used in functional programming. Review Function 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
Source Link https://www.eduonix.com/courses/Software-Development/clojure-fundamentals-for-beginners
Thank You www.eduonix.com