SlideShare a Scribd company logo
1 of 33
1
Structure and
Interpretation of Computer
Programs
Modularity, Objects, and State (part 1)
Abelson & Sussman2
UC San Diego
CSE 294
May 11, 2011
Barry Demchak
Agenda
 Functional vs Imperative languages
 OOP in Functional languages (e.g., Clojure)
 Data Encapsulation case studies
 Simple mutability (persistence)
 A simple object
 Mutability support
 Failings of the substitution model
 The environment model
 Method dispatch
 Language comparisons 2
Functional vs Imperative (1/2)
 Programming without any use of assignments
… is … known as functional programming.p230
 Looping via recursion
 Higher order programming
 Repeatable function execution  memoization
 Fewer special forms
 Programming that makes extensive use of
assignment is … imperative programming.p234
 Explicit control structures and special forms
 Introduces significant data dependency
3
Functional vs Imperative (2/2)
 Java/C++/Pascal
int factAnswer = 1;
for (int counter = 2; counter <= n; counter++)
factAnswer = factAnswer * counter;
Maps directly to registers and compare/jump
 Lisp/Clojure
(defn factorial [n]
(if (<= n 1)
1
(* n (factorial (dec n)))))
Maps to recurrence relation
4
Agenda
 Functional vs Imperative languages
 OOP in Functional languages (e.g., Clojure)
 Data Encapsulation case studies
 Simple mutability (persistence)
 A simple object
 Mutability support
 Failings of the substitution model
 The environment model
 Method dispatch
 Language comparisons 5
Object Oriented Programming
 Major properties
 Abstraction
 Inheritance
 Polymorphism
 Data encapsulation
 Major uses
 Manage complexity
 Multiple instances of stateful entities
 Orchestration of stateful entities
 Key point: Stateful entities change over time 6
Functional Programming vs OOP
 Contradiction
 Repeatable execution requires immutability
 Substitution model
(defn factorial [n]
(if (<= n 1)
1
(* n (factorial (dec n)))))
 (factorial 3)
 (* 3 (factorial 2))
 (* 3 (* 2 (factorial 1)))
 (* 3 (* 2 (* 1 1)))
 OOP depends on mutability
7
Agenda
 Functional vs Imperative languages
 OOP in Functional languages (e.g., Clojure)
 Data Encapsulation case studies
 Simple mutability (persistence)
 A simple object
 Mutability support
 Failings of the substitution model
 The environment model
 Method dispatch
 Language comparisons 8
Bringing OOP to Functional
 Abstraction (as seen by Valentin)
 Inheritance
 Polymorphism
 Data Encapsulation …implies mutable state
 Suppose a min() function
(defn min [x y]
(if (<= x y) x y))
where (min 5 6) -> 5
(min 5 6) -> 5
…
 Define last-min() to return last min() calculated
9
Simple Persistence
 Global persistence
(def last-min (atom 0))
(defn min [x y]
(reset! last-min
(if (<= x y)
x
y)))
10
 No encapsulation
 Single instance
only
 (reset!
<name> <value>)
 (swap!
<name> <func>)
(defn min-obj [init-min]
(let [last-min (atom init-min)]
(defn do-min [x y]
(reset! last-min (if (<= x y) x y)))
(defn get-last [] (deref last-min))
{:get #(get-last)
:min #(do-min %1 %2)}))
A Min Object
11
http://thinkrelevance.com/blog/2009/08/12/rifle-oriented-programming-with-clojure-2.html
A Min Object
12
(defn min-obj [init-min]
(let [last-min (atom init-min)]
(defn do-min [x y]
(reset! last-min (if (<= x y) x y)))
(defn get-last [] (deref last-min))
{:get #(get-last)
:min #(do-min %1 %2)}))
(define min1 (min-obj 1000000))  Constructor
A Min Object
13
(defn min-obj [init-min]
(let [last-min (atom init-min)]
(defn do-min [x y]
(reset! last-min (if (<= x y) x y)))
(defn get-last [] (deref last-min))
{:get #(get-last)
:min #(do-min %1 %2)}))
(define min1 (min-obj 1000000))
((min1 :min) 5 6)  Call to min()
A Min Object
14
(defn min-obj [init-min]
(let [last-min (atom init-min)]
(defn do-min [x y]
(reset! last-min (if (<= x y) x y)))
(defn get-last [] (deref last-min))
{:get #(get-last)
:min #(do-min %1 %2)}))
(define min1 (min-obj 1000000))
((min1 :min) 5 6)
((min1 :get))  Fetch last min
Agenda
 Functional vs Imperative languages
 OOP in Functional languages (e.g., Clojure)
 Data Encapsulation case studies
 Simple mutability (persistence)
 A simple object
 Mutability support
 Failings of the substitution model
 The environment model
 Method dispatch
 Language comparisons 15
(defn min-obj [init-min]
(let [last-min (atom init-min)]
(defn do-min [x y]
(reset! last-min (if (<= x y) x y)))
(defn get-last [] (deref last-min))
{:get #(get-last)
:min #(do-min %1 %2)}))
(define min1 (min-obj 1000000))
((min1 :get))
The Problem: Binding last-min
16
The Solution: Environments
17
Global environment
min:
other variables
parameters: x y
body: (if …)
(defn min [x y] (if (<= x y) x y))
 What happens when you define min():
The Solution: Environments
18
Global environment
min:
other variables
parameters: x y
body: (if …)
(defn min [x y] (if (<= x y) x y))
 What happens when you define min():
Simple Execution
19
 What happens when you execute min():
Global environment
min:
other variables
parameters: x y
body: (if …)
(min 5 6)
E1 environment
x: 5
y: 6
(if …)
Simple Execution
20
 What happens when you execute min():
Global environment
min:
other variables
parameters: x y
body: (if …)
(min 5 6)
E1 environment
x: 5
y: 6
(if …)
Variable
references
resolved by
traversing
environment
chain
(defn min-obj [init-min]
(let [last-min (atom init-min)]
(defn do-min [x y]
(reset! last-min (if (<= x y) x y)))
(defn get-last [] (deref last-min))
{:get #(get-last)
:min #(do-min %1 %2)}))
(define min1 (min-obj 1000000))
((min1 :get))
How Does min-obj Get Defined?
21
Object Definition
22
Global environment
min-obj:
other variables
parameters: init-min
body: (let [... )
(defn min-obj [init-min] (let [...))
 What happens when you define min-obj():
Object Instantiation
23
 How do you instantiate min-obj():
Global environment
min-obj:
other variables
parameters: init-min
body: (let [... )
(def min1 (min-obj 1000000))
min1:
parameters:
body: (:get #(…),
:min #(…))
last-min: 1000000
do-min:
get-last:
Environment E1
parameters: x y
body: (reset! …)
parameters:
body: (deref …)
Object Instantiation
24
 How do you instantiate min-obj():
Global environment
min-obj:
other variables
parameters: init-min
body: (let [... )
(def min1 (min-obj 1000000))
min1:
parameters:
body: (:get #(…),
:min #(…))
last-min: 1000000
do-min:
get-last:
Environment E1
parameters: x y
body: (reset! …)
parameters:
body: (deref …)
Method Execution
25
 How do you execute min():
Global environment
min-obj:
other variables
parameters: init-min
body: (let [...)
((min1 :min) 5 6)
min1:
parameters:
body: (:get #(…),
:min #(…))
last-min: 1000000
do-min:
get-last:
Environment E1
parameters: x y
body: (reset! …)
parameters:
body: (deref ...)
E2
x: 5
y: 6
Functional Language OOP
 Data encapsulation as a result of
 Constructor parameter accessible only within
function
 Binding of internal functions to constructor
parameter allows state retention
 Other points
 Dismantling of environments amenable to
access count tracking
 Dispatcher can operate on any criteria, and is
pre-wired for SOA-style message passing
26
Future Investigation
 How to subclass
 How inheritance works
 Existing (or desirable) object frameworks
 Why isn’t OOP very common in Clojure?
27
Agenda
 Functional vs Imperative languages
 OOP in Functional languages (e.g., Clojure)
 Data Encapsulation case studies
 Simple mutability (persistence)
 A simple object
 Mutability support
 Failings of the substitution model
 The environment model
 Method dispatch
 Language comparisons 28
Selected Comparisons
Deep Static
Scope
Data
Encapsulation
Clojure/Lisp Arbitrary depth Yes
Pascal Arbitrary depth Yes, but no
instances
Java/C++ No Yes
29
Data Encapsulation Comparison
(defn min-obj [init-min]
(let [last-min (atom init-min)]
(defn do-min [x y]
(reset! last-min
(if (<= x y)
x
y)))
(defn get-last []
(deref last-min))
{:get #(get-last)
:min #(do-min %1 %2)}))
(define min1 (min-obj 1000000))
((min1 :min) 5 6)
((min1 :get)) 30
class min_obj {
private int last_min;
public min_obj(int init_min) {
last_min = init_min;}
public int min(int x, int y) {
last_min = (x <= y ? x : y);
return last_min;
}
public int get () {
return last_min;}
}
min_obj a = new min_obj(1000000);
a.min(5, 6);
a.get();
Clojure Java
Static Scoping in Pascal
program test;
var a : integer;
procedure outer();
var b : integer;
procedure inner();
var c : integer;
begin
c := a + b;
end;
begin
b := a;
inner();
end;
begin
a := 0;
outer();
end.
31
Evaluation
Stack
a
prev-frame-ptr
b
prev-frame-ptr
c
prev-frame-ptr
Compiler
knows stack
location of all
variables in
scope – no
traversing
Agenda
 Functional vs Imperative languages
 OOP in Functional languages (e.g., Clojure)
 Data Encapsulation case studies
 Simple mutability (persistence)
 A simple object
 Mutability support
 Failings of the substitution model
 The environment model
 Method dispatch
 Language comparisons 32
33
Questions

More Related Content

What's hot

Modul Praktek Java OOP
Modul Praktek Java OOP Modul Praktek Java OOP
Modul Praktek Java OOP Zaenal Arifin
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John StevensonJAX London
 
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12Abu Saleh
 
Clojure concurrency
Clojure concurrencyClojure concurrency
Clojure concurrencyAlex Navis
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentalsHCMUTE
 
Effective java - concurrency
Effective java - concurrencyEffective java - concurrency
Effective java - concurrencyfeng lee
 
Clojure for Data Science
Clojure for Data ScienceClojure for Data Science
Clojure for Data Sciencehenrygarner
 
Machine Learning for Trading
Machine Learning for TradingMachine Learning for Trading
Machine Learning for TradingLarry Guo
 
MODEL OF A PROGRAM AS MULTITHREADED STOCHASTIC AUTOMATON AND ITS EQUIVALENT T...
MODEL OF A PROGRAM AS MULTITHREADED STOCHASTIC AUTOMATON AND ITS EQUIVALENT T...MODEL OF A PROGRAM AS MULTITHREADED STOCHASTIC AUTOMATON AND ITS EQUIVALENT T...
MODEL OF A PROGRAM AS MULTITHREADED STOCHASTIC AUTOMATON AND ITS EQUIVALENT T...Sergey Staroletov
 
Python 3.6 Features 20161207
Python 3.6 Features 20161207Python 3.6 Features 20161207
Python 3.6 Features 20161207Jay Coskey
 
Introduction to Haskell: 2011-04-13
Introduction to Haskell: 2011-04-13Introduction to Haskell: 2011-04-13
Introduction to Haskell: 2011-04-13Jay Coskey
 
Prototypes in Pharo
Prototypes in PharoPrototypes in Pharo
Prototypes in PharoESUG
 
Generics Past, Present and Future (Latest)
Generics Past, Present and Future (Latest)Generics Past, Present and Future (Latest)
Generics Past, Present and Future (Latest)RichardWarburton
 
Java8 and Functional Programming
Java8 and Functional ProgrammingJava8 and Functional Programming
Java8 and Functional ProgrammingYiguang Hu
 
Java collections the force awakens
Java collections  the force awakensJava collections  the force awakens
Java collections the force awakensRichardWarburton
 
QwalKeko, a History Querying Tool
QwalKeko, a History Querying ToolQwalKeko, a History Querying Tool
QwalKeko, a History Querying Toolstevensreinout
 

What's hot (20)

Modul Praktek Java OOP
Modul Praktek Java OOP Modul Praktek Java OOP
Modul Praktek Java OOP
 
Class method
Class methodClass method
Class method
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
 
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
 
Clojure concurrency
Clojure concurrencyClojure concurrency
Clojure concurrency
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
Kotlin decompiled
Kotlin decompiledKotlin decompiled
Kotlin decompiled
 
Effective java - concurrency
Effective java - concurrencyEffective java - concurrency
Effective java - concurrency
 
Clojure for Data Science
Clojure for Data ScienceClojure for Data Science
Clojure for Data Science
 
Machine Learning for Trading
Machine Learning for TradingMachine Learning for Trading
Machine Learning for Trading
 
MODEL OF A PROGRAM AS MULTITHREADED STOCHASTIC AUTOMATON AND ITS EQUIVALENT T...
MODEL OF A PROGRAM AS MULTITHREADED STOCHASTIC AUTOMATON AND ITS EQUIVALENT T...MODEL OF A PROGRAM AS MULTITHREADED STOCHASTIC AUTOMATON AND ITS EQUIVALENT T...
MODEL OF A PROGRAM AS MULTITHREADED STOCHASTIC AUTOMATON AND ITS EQUIVALENT T...
 
Python 3.6 Features 20161207
Python 3.6 Features 20161207Python 3.6 Features 20161207
Python 3.6 Features 20161207
 
Introduction to Haskell: 2011-04-13
Introduction to Haskell: 2011-04-13Introduction to Haskell: 2011-04-13
Introduction to Haskell: 2011-04-13
 
Lazy java
Lazy javaLazy java
Lazy java
 
Prototypes in Pharo
Prototypes in PharoPrototypes in Pharo
Prototypes in Pharo
 
Generics Past, Present and Future (Latest)
Generics Past, Present and Future (Latest)Generics Past, Present and Future (Latest)
Generics Past, Present and Future (Latest)
 
Java8 and Functional Programming
Java8 and Functional ProgrammingJava8 and Functional Programming
Java8 and Functional Programming
 
Java collections the force awakens
Java collections  the force awakensJava collections  the force awakens
Java collections the force awakens
 
QwalKeko, a History Querying Tool
QwalKeko, a History Querying ToolQwalKeko, a History Querying Tool
QwalKeko, a History Querying Tool
 
CODEsign 2015
CODEsign 2015CODEsign 2015
CODEsign 2015
 

Viewers also liked

Presentación galileo gds
Presentación galileo gdsPresentación galileo gds
Presentación galileo gdsdanivillate
 
OnPoint Direct Challenges for Small to Mid Company Infographic 2016
OnPoint Direct Challenges for Small to Mid Company Infographic 2016OnPoint Direct Challenges for Small to Mid Company Infographic 2016
OnPoint Direct Challenges for Small to Mid Company Infographic 2016Lauren Carr
 
power,agilty and wicked enviroments
power,agilty and wicked enviromentspower,agilty and wicked enviroments
power,agilty and wicked enviromentsCaoilte Dunne
 
Java9 moduulit jigsaw
Java9 moduulit jigsawJava9 moduulit jigsaw
Java9 moduulit jigsawArto Santala
 
ITIL Intermediate Certificate in IT Service Strategy
ITIL Intermediate Certificate in IT Service StrategyITIL Intermediate Certificate in IT Service Strategy
ITIL Intermediate Certificate in IT Service StrategyReymond Seda
 
There's a chat for that – is messaging the next big platform?
There's a chat for that – is messaging the next big platform?There's a chat for that – is messaging the next big platform?
There's a chat for that – is messaging the next big platform?Richard Keen
 
Facebook Open Graph API and How To Use It
Facebook Open Graph API and How To Use ItFacebook Open Graph API and How To Use It
Facebook Open Graph API and How To Use ItAayush Shrestha
 
Amadeus m power
Amadeus m powerAmadeus m power
Amadeus m powersmillevi
 
Innovation, design thinking, and competitive advantage
Innovation, design thinking, and competitive advantageInnovation, design thinking, and competitive advantage
Innovation, design thinking, and competitive advantagePhil Barrett
 
Reliance life insurance
Reliance life insuranceReliance life insurance
Reliance life insuranceProjects Kart
 
Antifragility and testing for distributed systems failure
Antifragility and testing for distributed systems failureAntifragility and testing for distributed systems failure
Antifragility and testing for distributed systems failureDiUS
 
использование линейных алгоритмов для решения задач
использование линейных алгоритмов для решения задачиспользование линейных алгоритмов для решения задач
использование линейных алгоритмов для решения задачMaria Zakharova
 

Viewers also liked (15)

Agrawal_Komal
Agrawal_KomalAgrawal_Komal
Agrawal_Komal
 
Presentación galileo gds
Presentación galileo gdsPresentación galileo gds
Presentación galileo gds
 
OnPoint Direct Challenges for Small to Mid Company Infographic 2016
OnPoint Direct Challenges for Small to Mid Company Infographic 2016OnPoint Direct Challenges for Small to Mid Company Infographic 2016
OnPoint Direct Challenges for Small to Mid Company Infographic 2016
 
power,agilty and wicked enviroments
power,agilty and wicked enviromentspower,agilty and wicked enviroments
power,agilty and wicked enviroments
 
Java9 moduulit jigsaw
Java9 moduulit jigsawJava9 moduulit jigsaw
Java9 moduulit jigsaw
 
MEB_flyer
MEB_flyerMEB_flyer
MEB_flyer
 
ITIL Intermediate Certificate in IT Service Strategy
ITIL Intermediate Certificate in IT Service StrategyITIL Intermediate Certificate in IT Service Strategy
ITIL Intermediate Certificate in IT Service Strategy
 
There's a chat for that – is messaging the next big platform?
There's a chat for that – is messaging the next big platform?There's a chat for that – is messaging the next big platform?
There's a chat for that – is messaging the next big platform?
 
Pharmaceutical business management
Pharmaceutical business managementPharmaceutical business management
Pharmaceutical business management
 
Facebook Open Graph API and How To Use It
Facebook Open Graph API and How To Use ItFacebook Open Graph API and How To Use It
Facebook Open Graph API and How To Use It
 
Amadeus m power
Amadeus m powerAmadeus m power
Amadeus m power
 
Innovation, design thinking, and competitive advantage
Innovation, design thinking, and competitive advantageInnovation, design thinking, and competitive advantage
Innovation, design thinking, and competitive advantage
 
Reliance life insurance
Reliance life insuranceReliance life insurance
Reliance life insurance
 
Antifragility and testing for distributed systems failure
Antifragility and testing for distributed systems failureAntifragility and testing for distributed systems failure
Antifragility and testing for distributed systems failure
 
использование линейных алгоритмов для решения задач
использование линейных алгоритмов для решения задачиспользование линейных алгоритмов для решения задач
использование линейных алгоритмов для решения задач
 

Similar to Structure and interpretation of computer programs modularity, objects, and state (part 1)

Functional programming with clojure
Functional programming with clojureFunctional programming with clojure
Functional programming with clojureLucy Fang
 
[Greach 17] make concurrency groovy again
[Greach 17] make concurrency groovy again[Greach 17] make concurrency groovy again
[Greach 17] make concurrency groovy againAlonso Torres
 
Clojure for Data Science
Clojure for Data ScienceClojure for Data Science
Clojure for Data ScienceMike Anderson
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring ClojurescriptLuke Donnet
 
ClojureScript for the web
ClojureScript for the webClojureScript for the web
ClojureScript for the webMichiel Borkent
 
Pydiomatic
PydiomaticPydiomatic
Pydiomaticrik0
 
Functional Programming with JavaScript
Functional Programming with JavaScriptFunctional Programming with JavaScript
Functional Programming with JavaScriptAung Baw
 
From Java to Parellel Clojure - Clojure South 2019
From Java to Parellel Clojure - Clojure South 2019From Java to Parellel Clojure - Clojure South 2019
From Java to Parellel Clojure - Clojure South 2019Leonardo Borges
 
Fuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingFuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingShine Xavier
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойSigma Software
 
Keynote joearmstrong
Keynote joearmstrongKeynote joearmstrong
Keynote joearmstrongSentifi
 
Introduction to R
Introduction to RIntroduction to R
Introduction to Ragnonchik
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Introthnetos
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojureAbbas Raza
 

Similar to Structure and interpretation of computer programs modularity, objects, and state (part 1) (20)

Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
Clojure
ClojureClojure
Clojure
 
Functional programming with clojure
Functional programming with clojureFunctional programming with clojure
Functional programming with clojure
 
[Greach 17] make concurrency groovy again
[Greach 17] make concurrency groovy again[Greach 17] make concurrency groovy again
[Greach 17] make concurrency groovy again
 
Programming with Freedom & Joy
Programming with Freedom & JoyProgramming with Freedom & Joy
Programming with Freedom & Joy
 
Clojure for Data Science
Clojure for Data ScienceClojure for Data Science
Clojure for Data Science
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring Clojurescript
 
ClojureScript for the web
ClojureScript for the webClojureScript for the web
ClojureScript for the web
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
Functional Programming with JavaScript
Functional Programming with JavaScriptFunctional Programming with JavaScript
Functional Programming with JavaScript
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
From Java to Parellel Clojure - Clojure South 2019
From Java to Parellel Clojure - Clojure South 2019From Java to Parellel Clojure - Clojure South 2019
From Java to Parellel Clojure - Clojure South 2019
 
Fuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingFuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional Programming
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай Мозговой
 
Keynote joearmstrong
Keynote joearmstrongKeynote joearmstrong
Keynote joearmstrong
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Intro
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Clojure 1a
Clojure 1aClojure 1a
Clojure 1a
 

More from bdemchak

Cytoscape Network Visualization and Analysis
Cytoscape Network Visualization and AnalysisCytoscape Network Visualization and Analysis
Cytoscape Network Visualization and Analysisbdemchak
 
The New CyREST: Economical Delivery of Complex, Reproducible Network Biology ...
The New CyREST: Economical Delivery of Complex, Reproducible Network Biology ...The New CyREST: Economical Delivery of Complex, Reproducible Network Biology ...
The New CyREST: Economical Delivery of Complex, Reproducible Network Biology ...bdemchak
 
Cytoscape Cyberinfrastructure
Cytoscape CyberinfrastructureCytoscape Cyberinfrastructure
Cytoscape Cyberinfrastructurebdemchak
 
No More Silos! Cytoscape CI Enables Interoperability
No More Silos! Cytoscape CI Enables InteroperabilityNo More Silos! Cytoscape CI Enables Interoperability
No More Silos! Cytoscape CI Enables Interoperabilitybdemchak
 
Cytoscape CI Chapter 2
Cytoscape CI Chapter 2Cytoscape CI Chapter 2
Cytoscape CI Chapter 2bdemchak
 
Composable Chat Introduction
Composable Chat IntroductionComposable Chat Introduction
Composable Chat Introductionbdemchak
 
Rich Services: Composable chat
Rich Services: Composable chatRich Services: Composable chat
Rich Services: Composable chatbdemchak
 
Ucsd tum workshop bd
Ucsd tum workshop bdUcsd tum workshop bd
Ucsd tum workshop bdbdemchak
 
Rich Feeds for RESCUE and PALMS
Rich Feeds for RESCUE and PALMSRich Feeds for RESCUE and PALMS
Rich Feeds for RESCUE and PALMSbdemchak
 
Iscram 2008 presentation
Iscram 2008 presentationIscram 2008 presentation
Iscram 2008 presentationbdemchak
 
Rich feeds policy, the cloud, and CAP
Rich feeds   policy, the cloud, and CAPRich feeds   policy, the cloud, and CAP
Rich feeds policy, the cloud, and CAPbdemchak
 
Rich services to the Rescue
Rich services to the RescueRich services to the Rescue
Rich services to the Rescuebdemchak
 
Hicss 2012 presentation
Hicss 2012 presentationHicss 2012 presentation
Hicss 2012 presentationbdemchak
 
Policy 2012 presentation
Policy 2012 presentationPolicy 2012 presentation
Policy 2012 presentationbdemchak
 
Rich feeds for rescue an integration story
Rich feeds for rescue   an integration storyRich feeds for rescue   an integration story
Rich feeds for rescue an integration storybdemchak
 
Background scenario drivers and critical issues with a focus on technology ...
Background   scenario drivers and critical issues with a focus on technology ...Background   scenario drivers and critical issues with a focus on technology ...
Background scenario drivers and critical issues with a focus on technology ...bdemchak
 
Rich feeds for rescue, palms cyberinfrastructure integration stories
Rich feeds for rescue, palms cyberinfrastructure   integration storiesRich feeds for rescue, palms cyberinfrastructure   integration stories
Rich feeds for rescue, palms cyberinfrastructure integration storiesbdemchak
 
Data quality and uncertainty visualization
Data quality and uncertainty visualizationData quality and uncertainty visualization
Data quality and uncertainty visualizationbdemchak
 
Web programming in clojure
Web programming in clojureWeb programming in clojure
Web programming in clojurebdemchak
 
Requirements engineering from system goals to uml models to software specif...
Requirements engineering   from system goals to uml models to software specif...Requirements engineering   from system goals to uml models to software specif...
Requirements engineering from system goals to uml models to software specif...bdemchak
 

More from bdemchak (20)

Cytoscape Network Visualization and Analysis
Cytoscape Network Visualization and AnalysisCytoscape Network Visualization and Analysis
Cytoscape Network Visualization and Analysis
 
The New CyREST: Economical Delivery of Complex, Reproducible Network Biology ...
The New CyREST: Economical Delivery of Complex, Reproducible Network Biology ...The New CyREST: Economical Delivery of Complex, Reproducible Network Biology ...
The New CyREST: Economical Delivery of Complex, Reproducible Network Biology ...
 
Cytoscape Cyberinfrastructure
Cytoscape CyberinfrastructureCytoscape Cyberinfrastructure
Cytoscape Cyberinfrastructure
 
No More Silos! Cytoscape CI Enables Interoperability
No More Silos! Cytoscape CI Enables InteroperabilityNo More Silos! Cytoscape CI Enables Interoperability
No More Silos! Cytoscape CI Enables Interoperability
 
Cytoscape CI Chapter 2
Cytoscape CI Chapter 2Cytoscape CI Chapter 2
Cytoscape CI Chapter 2
 
Composable Chat Introduction
Composable Chat IntroductionComposable Chat Introduction
Composable Chat Introduction
 
Rich Services: Composable chat
Rich Services: Composable chatRich Services: Composable chat
Rich Services: Composable chat
 
Ucsd tum workshop bd
Ucsd tum workshop bdUcsd tum workshop bd
Ucsd tum workshop bd
 
Rich Feeds for RESCUE and PALMS
Rich Feeds for RESCUE and PALMSRich Feeds for RESCUE and PALMS
Rich Feeds for RESCUE and PALMS
 
Iscram 2008 presentation
Iscram 2008 presentationIscram 2008 presentation
Iscram 2008 presentation
 
Rich feeds policy, the cloud, and CAP
Rich feeds   policy, the cloud, and CAPRich feeds   policy, the cloud, and CAP
Rich feeds policy, the cloud, and CAP
 
Rich services to the Rescue
Rich services to the RescueRich services to the Rescue
Rich services to the Rescue
 
Hicss 2012 presentation
Hicss 2012 presentationHicss 2012 presentation
Hicss 2012 presentation
 
Policy 2012 presentation
Policy 2012 presentationPolicy 2012 presentation
Policy 2012 presentation
 
Rich feeds for rescue an integration story
Rich feeds for rescue   an integration storyRich feeds for rescue   an integration story
Rich feeds for rescue an integration story
 
Background scenario drivers and critical issues with a focus on technology ...
Background   scenario drivers and critical issues with a focus on technology ...Background   scenario drivers and critical issues with a focus on technology ...
Background scenario drivers and critical issues with a focus on technology ...
 
Rich feeds for rescue, palms cyberinfrastructure integration stories
Rich feeds for rescue, palms cyberinfrastructure   integration storiesRich feeds for rescue, palms cyberinfrastructure   integration stories
Rich feeds for rescue, palms cyberinfrastructure integration stories
 
Data quality and uncertainty visualization
Data quality and uncertainty visualizationData quality and uncertainty visualization
Data quality and uncertainty visualization
 
Web programming in clojure
Web programming in clojureWeb programming in clojure
Web programming in clojure
 
Requirements engineering from system goals to uml models to software specif...
Requirements engineering   from system goals to uml models to software specif...Requirements engineering   from system goals to uml models to software specif...
Requirements engineering from system goals to uml models to software specif...
 

Recently uploaded

MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 

Recently uploaded (20)

MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 

Structure and interpretation of computer programs modularity, objects, and state (part 1)

  • 1. 1 Structure and Interpretation of Computer Programs Modularity, Objects, and State (part 1) Abelson & Sussman2 UC San Diego CSE 294 May 11, 2011 Barry Demchak
  • 2. Agenda  Functional vs Imperative languages  OOP in Functional languages (e.g., Clojure)  Data Encapsulation case studies  Simple mutability (persistence)  A simple object  Mutability support  Failings of the substitution model  The environment model  Method dispatch  Language comparisons 2
  • 3. Functional vs Imperative (1/2)  Programming without any use of assignments … is … known as functional programming.p230  Looping via recursion  Higher order programming  Repeatable function execution  memoization  Fewer special forms  Programming that makes extensive use of assignment is … imperative programming.p234  Explicit control structures and special forms  Introduces significant data dependency 3
  • 4. Functional vs Imperative (2/2)  Java/C++/Pascal int factAnswer = 1; for (int counter = 2; counter <= n; counter++) factAnswer = factAnswer * counter; Maps directly to registers and compare/jump  Lisp/Clojure (defn factorial [n] (if (<= n 1) 1 (* n (factorial (dec n))))) Maps to recurrence relation 4
  • 5. Agenda  Functional vs Imperative languages  OOP in Functional languages (e.g., Clojure)  Data Encapsulation case studies  Simple mutability (persistence)  A simple object  Mutability support  Failings of the substitution model  The environment model  Method dispatch  Language comparisons 5
  • 6. Object Oriented Programming  Major properties  Abstraction  Inheritance  Polymorphism  Data encapsulation  Major uses  Manage complexity  Multiple instances of stateful entities  Orchestration of stateful entities  Key point: Stateful entities change over time 6
  • 7. Functional Programming vs OOP  Contradiction  Repeatable execution requires immutability  Substitution model (defn factorial [n] (if (<= n 1) 1 (* n (factorial (dec n)))))  (factorial 3)  (* 3 (factorial 2))  (* 3 (* 2 (factorial 1)))  (* 3 (* 2 (* 1 1)))  OOP depends on mutability 7
  • 8. Agenda  Functional vs Imperative languages  OOP in Functional languages (e.g., Clojure)  Data Encapsulation case studies  Simple mutability (persistence)  A simple object  Mutability support  Failings of the substitution model  The environment model  Method dispatch  Language comparisons 8
  • 9. Bringing OOP to Functional  Abstraction (as seen by Valentin)  Inheritance  Polymorphism  Data Encapsulation …implies mutable state  Suppose a min() function (defn min [x y] (if (<= x y) x y)) where (min 5 6) -> 5 (min 5 6) -> 5 …  Define last-min() to return last min() calculated 9
  • 10. Simple Persistence  Global persistence (def last-min (atom 0)) (defn min [x y] (reset! last-min (if (<= x y) x y))) 10  No encapsulation  Single instance only  (reset! <name> <value>)  (swap! <name> <func>)
  • 11. (defn min-obj [init-min] (let [last-min (atom init-min)] (defn do-min [x y] (reset! last-min (if (<= x y) x y))) (defn get-last [] (deref last-min)) {:get #(get-last) :min #(do-min %1 %2)})) A Min Object 11 http://thinkrelevance.com/blog/2009/08/12/rifle-oriented-programming-with-clojure-2.html
  • 12. A Min Object 12 (defn min-obj [init-min] (let [last-min (atom init-min)] (defn do-min [x y] (reset! last-min (if (<= x y) x y))) (defn get-last [] (deref last-min)) {:get #(get-last) :min #(do-min %1 %2)})) (define min1 (min-obj 1000000))  Constructor
  • 13. A Min Object 13 (defn min-obj [init-min] (let [last-min (atom init-min)] (defn do-min [x y] (reset! last-min (if (<= x y) x y))) (defn get-last [] (deref last-min)) {:get #(get-last) :min #(do-min %1 %2)})) (define min1 (min-obj 1000000)) ((min1 :min) 5 6)  Call to min()
  • 14. A Min Object 14 (defn min-obj [init-min] (let [last-min (atom init-min)] (defn do-min [x y] (reset! last-min (if (<= x y) x y))) (defn get-last [] (deref last-min)) {:get #(get-last) :min #(do-min %1 %2)})) (define min1 (min-obj 1000000)) ((min1 :min) 5 6) ((min1 :get))  Fetch last min
  • 15. Agenda  Functional vs Imperative languages  OOP in Functional languages (e.g., Clojure)  Data Encapsulation case studies  Simple mutability (persistence)  A simple object  Mutability support  Failings of the substitution model  The environment model  Method dispatch  Language comparisons 15
  • 16. (defn min-obj [init-min] (let [last-min (atom init-min)] (defn do-min [x y] (reset! last-min (if (<= x y) x y))) (defn get-last [] (deref last-min)) {:get #(get-last) :min #(do-min %1 %2)})) (define min1 (min-obj 1000000)) ((min1 :get)) The Problem: Binding last-min 16
  • 17. The Solution: Environments 17 Global environment min: other variables parameters: x y body: (if …) (defn min [x y] (if (<= x y) x y))  What happens when you define min():
  • 18. The Solution: Environments 18 Global environment min: other variables parameters: x y body: (if …) (defn min [x y] (if (<= x y) x y))  What happens when you define min():
  • 19. Simple Execution 19  What happens when you execute min(): Global environment min: other variables parameters: x y body: (if …) (min 5 6) E1 environment x: 5 y: 6 (if …)
  • 20. Simple Execution 20  What happens when you execute min(): Global environment min: other variables parameters: x y body: (if …) (min 5 6) E1 environment x: 5 y: 6 (if …) Variable references resolved by traversing environment chain
  • 21. (defn min-obj [init-min] (let [last-min (atom init-min)] (defn do-min [x y] (reset! last-min (if (<= x y) x y))) (defn get-last [] (deref last-min)) {:get #(get-last) :min #(do-min %1 %2)})) (define min1 (min-obj 1000000)) ((min1 :get)) How Does min-obj Get Defined? 21
  • 22. Object Definition 22 Global environment min-obj: other variables parameters: init-min body: (let [... ) (defn min-obj [init-min] (let [...))  What happens when you define min-obj():
  • 23. Object Instantiation 23  How do you instantiate min-obj(): Global environment min-obj: other variables parameters: init-min body: (let [... ) (def min1 (min-obj 1000000)) min1: parameters: body: (:get #(…), :min #(…)) last-min: 1000000 do-min: get-last: Environment E1 parameters: x y body: (reset! …) parameters: body: (deref …)
  • 24. Object Instantiation 24  How do you instantiate min-obj(): Global environment min-obj: other variables parameters: init-min body: (let [... ) (def min1 (min-obj 1000000)) min1: parameters: body: (:get #(…), :min #(…)) last-min: 1000000 do-min: get-last: Environment E1 parameters: x y body: (reset! …) parameters: body: (deref …)
  • 25. Method Execution 25  How do you execute min(): Global environment min-obj: other variables parameters: init-min body: (let [...) ((min1 :min) 5 6) min1: parameters: body: (:get #(…), :min #(…)) last-min: 1000000 do-min: get-last: Environment E1 parameters: x y body: (reset! …) parameters: body: (deref ...) E2 x: 5 y: 6
  • 26. Functional Language OOP  Data encapsulation as a result of  Constructor parameter accessible only within function  Binding of internal functions to constructor parameter allows state retention  Other points  Dismantling of environments amenable to access count tracking  Dispatcher can operate on any criteria, and is pre-wired for SOA-style message passing 26
  • 27. Future Investigation  How to subclass  How inheritance works  Existing (or desirable) object frameworks  Why isn’t OOP very common in Clojure? 27
  • 28. Agenda  Functional vs Imperative languages  OOP in Functional languages (e.g., Clojure)  Data Encapsulation case studies  Simple mutability (persistence)  A simple object  Mutability support  Failings of the substitution model  The environment model  Method dispatch  Language comparisons 28
  • 29. Selected Comparisons Deep Static Scope Data Encapsulation Clojure/Lisp Arbitrary depth Yes Pascal Arbitrary depth Yes, but no instances Java/C++ No Yes 29
  • 30. Data Encapsulation Comparison (defn min-obj [init-min] (let [last-min (atom init-min)] (defn do-min [x y] (reset! last-min (if (<= x y) x y))) (defn get-last [] (deref last-min)) {:get #(get-last) :min #(do-min %1 %2)})) (define min1 (min-obj 1000000)) ((min1 :min) 5 6) ((min1 :get)) 30 class min_obj { private int last_min; public min_obj(int init_min) { last_min = init_min;} public int min(int x, int y) { last_min = (x <= y ? x : y); return last_min; } public int get () { return last_min;} } min_obj a = new min_obj(1000000); a.min(5, 6); a.get(); Clojure Java
  • 31. Static Scoping in Pascal program test; var a : integer; procedure outer(); var b : integer; procedure inner(); var c : integer; begin c := a + b; end; begin b := a; inner(); end; begin a := 0; outer(); end. 31 Evaluation Stack a prev-frame-ptr b prev-frame-ptr c prev-frame-ptr Compiler knows stack location of all variables in scope – no traversing
  • 32. Agenda  Functional vs Imperative languages  OOP in Functional languages (e.g., Clojure)  Data Encapsulation case studies  Simple mutability (persistence)  A simple object  Mutability support  Failings of the substitution model  The environment model  Method dispatch  Language comparisons 32