Introduction Basics Concurrency Conclusion. Clojure. Marcel Klinzing. December 13, M. Klinzing Clojure 1/18

Similar documents
Seminar on Languages for Scientific Computing Aachen, 6 Feb Navid Abbaszadeh.

Reference types in Clojure. April 2, 2014

Clojure Lisp for the Real #clojure

Clojure Lisp for the Real clojure.com

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Persistent Data Structures and Managed References

Clojure is. A dynamic, LISP-based. programming language. running on the JVM

.consulting.solutions.partnership. Clojure by Example. A practical introduction to Clojure on the JVM

The Curious Clojureist

Stuart

Clojure. A Dynamic Programming Language for the JVM. Rich Hickey

Identity, State and Values

Tackling Concurrency With STM. Mark Volkmann 10/22/09

Tackling Concurrency With STM

This tutorial is designed for all those software professionals who are keen on learning the basics of Clojure and how to put it into practice.

Symbols. abstractions, implementing, 270 through indirection, 77 with macros, 183 abstract syntax tree (AST), 149

ClojureScript. as a compilation target to JS. Michiel Vijay FP AMS October 16th 2014

Multi-core Parallelization in Clojure - a Case Study

Clojure Concurrency Constructs. CSCI 5828: Foundations of Software Engineering Lecture 12 10/02/2014

Interfacing Clojure with Pogamut 3 platform

What if Type Systems were more like Linters?

Clojure. A Dynamic Programming Language for the JVM. (and CLR) Rich Hickey

Clojure: Enemy of the State

Programming Clojure, Third Edition

Clojure. A (not-so-pure) functional approach to concurrency. Paolo Baldan Linguaggi per il Global Computing AA 2016/2017

Macro #clojureconj

June 27, 2014 EuroClojure 2014 Krakow, Poland. Components. Just Enough

Data Structures in Functional Languages

clojure & cfml sitting in a tree sean corfield world singles

Clojure. The Revenge of Data. by Vjeran Marcinko Kapsch CarrierCom

Executable Formal Specifications with Clojure. Matti Nieminen

Lazytest Better Living Through Protocols. Stuart Sierra. Clojure NYC April 15, 2010

CS 2340 Objects and Design - Scala

Clojure Distilled. Immutable

Week 2: The Clojure Language. Background Basic structure A few of the most useful facilities. A modernized Lisp. An insider's opinion

Programming Clojure. Extracted from: Second Edition. The Pragmatic Bookshelf

Clojure & Incanter for Java Programmers

A Practical Optional Type System for Clojure. Ambrose Bonnaire-Sergeant

Scala : an LLVM-targeted Scala compiler

Problems with Concurrency. February 19, 2014

Introduction to Clojure Concurrency (and data structures)

BUILDING DECLARATIVE GUIS WITH CLOJURE, JAVAFX AND FN-FX DR. NILS BLUM-OESTE

Michiel DomCode, May 26th 2015

Declarative concurrency. March 3, 2014

Common LISP Tutorial 1 (Basic)

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

STARCOUNTER. Technical Overview

Advanced concurrent programming in Java Shared objects

Reagent. a ClojureScript interface to React. React Amsterdam Meetup 12 Feb. 2015

Ruby: Introduction, Basics

SAMPLE CHAPTER IN ACTION SECOND EDITION. Amit Rathore Francis Avila MANNING

Use of Clojure in Undergraduate CS Curriculum

1.3.4 case and case* macro since 1.2. Listing Conditional Branching, Fast Switch. Listing Contract

Clojure for OOP folks Stefan innoq

Towards Lean 4: Sebastian Ullrich 1, Leonardo de Moura 2.

Map3D V58 - Multi-Processor Version

Functional programming is a productive

Drinking from the Clojure fire hose

Simple Java Programs. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

John McCarthy IBM 704

Alan Bateman Java Platform Group, Oracle November Copyright 2018, Oracle and/or its affiliates. All rights reserved.!1

A Survey of Concurrency Constructs. Ted Leung Sun

Scala, Your Next Programming Language

Programming Languages

Functional Programming and the Web

15CS45 : OBJECT ORIENTED CONCEPTS

Parallelism. Master 1 International. Andrea G. B. Tettamanzi. Université de Nice Sophia Antipolis Département Informatique

First Programming Language in CS Education The Arguments for Scala

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Akka: Simpler Concurrency, Scalability & Fault-tolerance through Actors. Jonas Bonér Viktor Klang

Java Programming. Manuel Oriol, March 22nd, 2007

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

POETRY OF PROGRAMMING CODE READING EXERCISES IN CLOJURE V

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

<Insert Picture Here> Get on the Grid. JVM Language Summit Getting Started Guide. Cameron Purdy, VP of Development, Oracle

Modeling Process. Rich Hickey

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Virtual Machine Design

Efficient Composition Styles

OBJECT ORIENTED PROGRAMMING TYm. Allotted : 3 Hours Full Marks: 70

Transactifying Apache s Cache Module

Programming Safe Agents in Blueprint. Alex Muscar University of Craiova

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Functional Objects. Christopher Simpkins CS 3693, Fall Chris Simpkins (Georgia Tech) CS 3693 Scala / 1

Midterm Exam CS 251, Intermediate Programming March 6, 2015

Robot Programming with Lisp

What the optimizer does to your code

Applied Unified Ownership. Capabilities for Sharing Across Threads

Compiler construction 2009

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Next Gen Networking Infrastructure With Rust

Manchester University Transactions for Scala

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

CST242 Concurrency Page 1

Functional programming

CompSci 125 Lecture 02

Quote of the Day. CS Functional Programming. Introductory Notes on Lisp/Clojure. Another Quote. What is Clojure? 1-4

The Actor Model. Towards Better Concurrency. By: Dror Bereznitsky

Transcription:

Clojure Marcel Klinzing December 13, 2012 M. Klinzing Clojure 1/18

Overview/History Functional programming language Lisp dialect Compiles to Java Bytecode Implemented in Java Created by Rich Hickey Version 1.0.0 released in 2009 Rich Hickey Current Version: 1.4.0 M. Klinzing Clojure 2/18

Features Program as data First-class functions Dynamically typed Java interoperability Immutable, persistent data Mutable references to data Support for concurrent programming and more... M. Klinzing Clojure 3/18

Forms Part of Clojure called Reader reads chunks of the program called forms Reader translates forms into Clojure data structures Clojure compiles data structures and executes them Forms (excerpt): Form Example Boolean true, false List (+ 1 2), (map + [1 2] [3 4]) Number 1, 3.142, 22/7 String hello world Symbol foo, foo-?, java.lang.thread Vector [1 2 3] M. Klinzing Clojure 4/18

Variables and Functions Defining a (global) variable: (def identifier value) Defining a function: (defn identifier (parameter-vec body) + ) Calling a function: (identifier param1 param2...) 1 (defn f 2 ([] (println "hello, world!")) 3 ([x] (println x))) 4 5 ; brackets can be omitted if 6 ; there is only one parameter vec and body 7 (defn g [] (println "bar")) 8 9 (f) ; Output: hello, world! 10 (f "foo") ; Output: foo 11 (g) ; Output: bar M. Klinzing Clojure 5/18

Destructuring Destructuring used to bind single values in a collection to variables 1 (defn f [ [x y] ] 2 (+ x y)) 3 4 (println (f [1 1])) ; Output: 2 5 (println (f [4 5 6 7])) ; Output: 9 M. Klinzing Clojure 6/18

Java Interoperability Package java.lang imported automatically Create new Object: (new classname & args) Call of variables/methods: (. class-or-instance member-symbol & args) Note that every Clojure function is an Object which implements java.lang.runnable and java.lang.callable 1 (defn delay-print [] 2 (. Thread sleep 100) 3 (println "new Thread")) 4 5 (. (new Thread delay-print) start) 6 (print "start ") 7 ; Output: start new Thread M. Klinzing Clojure 7/18

Reference Types Reference types = mutable references to immutable data Reference types and their purpose: Refs: Synchronous, coordinated updates of data Agents: Asynchronous update of a single value Atoms: Synchronous update of a single value Vars: Synchronous update of a thread-local value M. Klinzing Clojure 8/18

Software Transactional Memory Software Transactional Memory (STM) is a model/concept to view memory as a database STM used for coordinated updates (refs) Updates of values are done in transactions Properties of STM transactions: Atomic updates Consistent updates Isolated updates M. Klinzing Clojure 9/18

STM - Multiversion Control STM uses Multiversion Control (MVCC): Start of a Transaction: Unique ID (called point) is generated Transaction works against private copies of references (associated with the point) Changes to refs will be visible only after the transaction commits/ends If a transaction notices that a ref has been already set/altered by another transaction, the transaction will restart To start a transaction: (dosync & exprs) M. Klinzing Clojure 10/18

Refs Creating a ref: (ref initial-value) Dereferencing a ref: (deref ref-inst) or shorter: @ref-inst To change the reference: (ref-set new-value) Note: ref-set can only be called in a transaction; otherwise a IllegalStateException is thrown M. Klinzing Clojure 11/18

Refs Example 1 (def x (ref 0)) 2 3 (defn increase [] 4 (. Thread sleep 100) 5 (dosync (ref-set x (+ @x 1)))) 6 7 (defn decrease [] 8 (. Thread sleep 100) 9 (dosync (ref-set x (- @x 1)))) 10 11 (def inc-thread (new Thread increase)) 12 (def dec-thread (new Thread decrease)) 13 14 (. inc-thread start) (. dec-thread start) 15 (. inc-thread join) (. dec-thread join) 16 (println @x) ; Output: 0 M. Klinzing Clojure 12/18

Agents Creating an agent: (agent initial-value) Dereferencing for agents as for refs: @agent-inst Changing of agents per update function: (send agent-inst update-fn & args) (update-fn @agent-inst arg1 arg2...) is run later on a thread in a thread pool and the return value is assigned to the agent M. Klinzing Clojure 13/18

Agents Example 1 (def ag (agent 0)) 2 3 (defn increase [x] 4 (+ x 1)) 5 6 (println @(send ag increase)) ; Output: 0 7 (. Thread sleep 100) 8 (println @ag) ; Output: 1 M. Klinzing Clojure 14/18

Benchmark Benchmark taken from the Computer Language Benchmarks Game ( http://shootout.alioth.debian.org ) thread-ring program: Create 503 linked pre-emptive threads Thread 503 should be linked to thread 1, forming an unbroken ring Pass a token to thread 1 Pass the token from thread to thread N times Print the name of the last thread (1 to 503) to take the token M. Klinzing Clojure 15/18

Benchmarks Results (excerpt) N = 50,000,000 Quad-core 2.4Ghz Intel R Q6600 R ; 4GB RAM; 250GB SATA II disk drive x64 Ubuntu TM M. Klinzing Clojure 16/18

Pros: Functional programming (first-class functions, small code,... ) Java interoperability Support for concurrent programming Cons: Speed (since it runs on the JVM) M. Klinzing Clojure 17/18

Other Clojure Implementations ClojureScript Compiles Clojure code to JavaScript implemented in Clojure ClojureCLR Native implementation of Clojure on the Common Language Runtime (CLR; virtual machine of Microsoft s.net framework) Implemented in C# (and Clojure itself) M. Klinzing Clojure 18/18

References Official site: http://clojure.org Book: Stuart Halloway and Aaron Bedra. Programming Clojure. 2nd ed. The Pragmatic Programmers, 2012. Tutorial: R. Mark Volkmann. Clojure - Functional Programming for the JVM http://java.ociweb.com/mark/clojure/article.html Benchmark: http://shootout.alioth.debian.org/ Pictures: Slide 2: http://en.wikipedia.org/wiki/file:rich_hickey.jpg M. Klinzing Clojure

Thank you for your attention. M. Klinzing Clojure

Backup Slides Multimethods Declaring a multimethod: (defmulti identifier choosing-fn) Considering a function call (identifier arg1 arg2...); (choosing-fn arg1 arg2...) is called and the return value determines the implementation to be called Defining a multimethod: (defmethod identifier choosing-val (parameter-vec body) + ) M. Klinzing Clojure

Backup Slides Metadata Metadata = Data about data that doesn t change its logical values Write ^metadata-map before symbols to attach metadata Example: 1 (def x 1) 2 (defn ^{:doc "Returns its first argument"} f [x y] x) 3 4 (println (meta # x)) 5 ; Output: {:ns #<Namespace user>, :name x, :line 1, 6 ; : file D:\Clojure\metadata. clj } 7 (println (meta # f)) 8 ;Output: {: arglists ([x y ]), :ns #<Namespace user>, :name f, 9 ; :doc Returns its first argument, : line 3, : file...} 10 (println (doc f)) 11 ; Output: 12 ; user/f 13 ; ([x y]) 14 ; Returns its first argument M. Klinzing Clojure