SlideShare ist ein Scribd-Unternehmen logo
1 von 45
Downloaden Sie, um offline zu lesen
Functional Smalltalk
Dave Mason
Toronto Metropolitan University
©2022 Dave Mason
Value
I’m going to start with a quote from Kent Beck
Value
Software creates value 2 ways:
What it does today
What new things we can make it do tomorrow
Value
Software creates value 2 ways:
What it does today
What new things we can make it do tomorrow
Value
Software creates value 2 ways:
What it does today
What new things we can make it do tomorrow
Value
Smalltalk creates value 2 ways:
What it does today
What new things we can make it do tomorrow
Functional Smalltalk
Smalltalk already has many functional features
extensions by syntax
extensions by class
Functional Smalltalk
Smalltalk already has many functional features
extensions by syntax
extensions by class
Syntax: Functional Programming
Smalltalk has always had blocks - needed full closures
CompileWithCompose in Pharo-Functional repo
leverages class-bounded alternative compiler
just syntactic sugar - more succinct
all are upward compatible as they are currently syntax errors
Syntax: Functional Programming
Smalltalk has always had blocks - needed full closures
CompileWithCompose in Pharo-Functional repo
leverages class-bounded alternative compiler
just syntactic sugar - more succinct
all are upward compatible as they are currently syntax errors
Syntax: Functional Programming
Smalltalk has always had blocks - needed full closures
CompileWithCompose in Pharo-Functional repo
leverages class-bounded alternative compiler
just syntactic sugar - more succinct
all are upward compatible as they are currently syntax errors
Syntax: Functional Programming
Smalltalk has always had blocks - needed full closures
CompileWithCompose in Pharo-Functional repo
leverages class-bounded alternative compiler
just syntactic sugar - more succinct
all are upward compatible as they are currently syntax errors
Syntax: Functional Programming
Smalltalk has always had blocks - needed full closures
CompileWithCompose in Pharo-Functional repo
leverages class-bounded alternative compiler
just syntactic sugar - more succinct
all are upward compatible as they are currently syntax errors
Compose/pipe/parrot operator
very convenient to pass result of one expression to another
without parentheses
particularly convenient in PharoJS for e.g. D3
Compose/pipe/parrot operator
very convenient to pass result of one expression to another
without parentheses
particularly convenient in PharoJS for e.g. D3
1 foo
2 " s e l f new foo >>> 42 "
3 ↑ 17 negated
4 :> min: -53
5 :> abs
6 :> < 100
7 :> and: [ 4 > 2 ]
8 :> and: [ 5 < 10 ]
9 :> ifTrue: [ 42 ] ifFalse: [ 99 ]
Compose/pipe/parrot operator
very convenient to pass result of one expression to another
without parentheses
particularly convenient in PharoJS for e.g. D3
1 foo
2 " s e l f new foo >>> 42 "
3 ↑ 17 negated
4 :> min: -53
5 :> abs
6 :> < 100
7 :> and: [ 4 > 2 ]
8 :> and: [ 5 < 10 ]
9 :> ifTrue: [ 42 ] ifFalse: [ 99 ]
... Compose/pipe/parrot operator
The precedence is the same as cascade, so you can intermix them
and could say something like:
1 x := OrderedCollection new
2 add: 42;
3 add: 17;
4 yourself
5 :> collect: #negated
6 :> add: 35;
7 add: 99;
8 yourself
9 :> with: #(1 2 3 4) collect: [:l :r| l+r ]
10 :> max
... Compose/pipe/parrot operator
If you don’t want to use the alternate compiler (and get the :> syntax)
PharoFunctional also provides a chain method on Object that
supports chaining using cascades (unfortunately quite a bit slower
because it requires a DNU and perform for each chained message):
1 foo
2 " s e l f new foo >>> 42 "
3 ↑ 17 chain
4 negated
5 ; min: -53
6 ; abs
7 ; < 100
8 ; and: [ 4 > 2 ]
9 ; and: [ 5 < 10 ]
10 ; ifTrue: [ 42 ] ifFalse: [ 99 ]
Point-free programming style
popular style of functional programming
composing functions to build up operations with implicit
parameters
various “combinators” that recognize patterns in these
compositions
in Smalltalk this is composing symbols and blocks
e.g.
1 isPalindrome := #reverse <| > #= .
2 isPalindrome value: ’madam’
Point-free programming style
popular style of functional programming
composing functions to build up operations with implicit
parameters
various “combinators” that recognize patterns in these
compositions
in Smalltalk this is composing symbols and blocks
e.g.
1 isPalindrome := #reverse <| > #= .
2 isPalindrome value: ’madam’
Point-free programming style
popular style of functional programming
composing functions to build up operations with implicit
parameters
various “combinators” that recognize patterns in these
compositions
in Smalltalk this is composing symbols and blocks
e.g.
1 isPalindrome := #reverse <| > #= .
2 isPalindrome value: ’madam’
Point-free programming style
popular style of functional programming
composing functions to build up operations with implicit
parameters
various “combinators” that recognize patterns in these
compositions
in Smalltalk this is composing symbols and blocks
e.g.
1 isPalindrome := #reverse <| > #= .
2 isPalindrome value: ’madam’
Point-free programming style
popular style of functional programming
composing functions to build up operations with implicit
parameters
various “combinators” that recognize patterns in these
compositions
in Smalltalk this is composing symbols and blocks
e.g.
1 isPalindrome := #reverse <| > #= .
2 isPalindrome value: ’madam’
Expressions as unary or binary messages
To use point-free style, it is very convenient to have a more succinct
syntax for applying them
1 x (...)
2 x (...) + y
3 x (...): y
4 x (#sort <| > #=)
Converts to.
1 ([...] value: x)
2 ([...] value: x) + y
3 ([...] value: x value: y)
4 ((#sort <| > #=) value: x)
Blocks as unary or binary messages
You can do the same with unary or binary blocks. Because we know
the arity of blocks the trailing : isn’t used for block operators
1 x [:w| ...]
2 x [:w:z| ...] y
becomes
1 ([:w| ...] value: x)
2 ([:w:z| ...] value: x value: y)
Initializing local variables at point of declaration
Even in functional languages where mutation is possible, it is rarely
used. Instead programming is by a sequence of definitions, which
always have a value. I personally very much miss this in Smalltalk.
1 | w x := 42. y = x+5. z a |
is legal, but
1 | x := 42. y = x+5. z = 17 |
isn’t.
Collection literals
Arrays have a literal syntax {1 . 2 . 3}, but other collections don’t.
This extension recognizes :className immediately after the { and
translates, e.g.
1 {:Set 3 . 4 . 5 . 3}
2 {:Dictionary #a->1 . #b->2}
3 {:Set 1 . 2 . 3 . 4 . 5 . 6 . 7}
to
1 Set with: 3 with: 4 with: 5 with: 3
2 Dictionary with: #a->1 with: #b->2
3 Set withAll: {1 . 2 . 3 . 4 . 5 . 6 . 7}
Destructuring collections
There isn’t a convenient way to return multiple values from a method,
or even to extract multiple values from a collection. For example:
1 :| a b c | := some-collection
destructures the 3 elements of a SequenceableCollection or would
extract the value of keys #a #b etc. if it was a Dictionary, with anything
else being a runtime error. This is conveniently done by converting that
to:
1 ([:temp|
2 a := temp firstNamed: #a.
3 b := temp secondNamed: #b.
4 c := temp thirdNamed: #c.
5 temp] value: some-collection)
Classes: Functional Programming
PharoFunctional adds several new classes and a variety of extension
methods to facilitate functional programming.
curry: and @@
value:, value:value: and cull, etc. for Symbol
map:, map:map: for BlockClosure and Symbol
<*> and other combinators for BlockClosure and Symbol
nilOr:, emptyOrNilOr:
Slice, Pair and Tuple, ZippedCollection
zip:, >===<
iota
many algorithms on collections: rotate:, slide:, product,
allEqual, unique, isUnique, groupByRunsEqual:,
groupByRunsTrue:
Classes: Functional Programming
PharoFunctional adds several new classes and a variety of extension
methods to facilitate functional programming.
curry: and @@
value:, value:value: and cull, etc. for Symbol
map:, map:map: for BlockClosure and Symbol
<*> and other combinators for BlockClosure and Symbol
nilOr:, emptyOrNilOr:
Slice, Pair and Tuple, ZippedCollection
zip:, >===<
iota
many algorithms on collections: rotate:, slide:, product,
allEqual, unique, isUnique, groupByRunsEqual:,
groupByRunsTrue:
Classes: Functional Programming
PharoFunctional adds several new classes and a variety of extension
methods to facilitate functional programming.
curry: and @@
value:, value:value: and cull, etc. for Symbol
map:, map:map: for BlockClosure and Symbol
<*> and other combinators for BlockClosure and Symbol
nilOr:, emptyOrNilOr:
Slice, Pair and Tuple, ZippedCollection
zip:, >===<
iota
many algorithms on collections: rotate:, slide:, product,
allEqual, unique, isUnique, groupByRunsEqual:,
groupByRunsTrue:
Classes: Functional Programming
PharoFunctional adds several new classes and a variety of extension
methods to facilitate functional programming.
curry: and @@
value:, value:value: and cull, etc. for Symbol
map:, map:map: for BlockClosure and Symbol
<*> and other combinators for BlockClosure and Symbol
nilOr:, emptyOrNilOr:
Slice, Pair and Tuple, ZippedCollection
zip:, >===<
iota
many algorithms on collections: rotate:, slide:, product,
allEqual, unique, isUnique, groupByRunsEqual:,
groupByRunsTrue:
Classes: Functional Programming
PharoFunctional adds several new classes and a variety of extension
methods to facilitate functional programming.
curry: and @@
value:, value:value: and cull, etc. for Symbol
map:, map:map: for BlockClosure and Symbol
<*> and other combinators for BlockClosure and Symbol
nilOr:, emptyOrNilOr:
Slice, Pair and Tuple, ZippedCollection
zip:, >===<
iota
many algorithms on collections: rotate:, slide:, product,
allEqual, unique, isUnique, groupByRunsEqual:,
groupByRunsTrue:
Classes: Functional Programming
PharoFunctional adds several new classes and a variety of extension
methods to facilitate functional programming.
curry: and @@
value:, value:value: and cull, etc. for Symbol
map:, map:map: for BlockClosure and Symbol
<*> and other combinators for BlockClosure and Symbol
nilOr:, emptyOrNilOr:
Slice, Pair and Tuple, ZippedCollection
zip:, >===<
iota
many algorithms on collections: rotate:, slide:, product,
allEqual, unique, isUnique, groupByRunsEqual:,
groupByRunsTrue:
Classes: Functional Programming
PharoFunctional adds several new classes and a variety of extension
methods to facilitate functional programming.
curry: and @@
value:, value:value: and cull, etc. for Symbol
map:, map:map: for BlockClosure and Symbol
<*> and other combinators for BlockClosure and Symbol
nilOr:, emptyOrNilOr:
Slice, Pair and Tuple, ZippedCollection
zip:, >===<
iota
many algorithms on collections: rotate:, slide:, product,
allEqual, unique, isUnique, groupByRunsEqual:,
groupByRunsTrue:
Classes: Functional Programming
PharoFunctional adds several new classes and a variety of extension
methods to facilitate functional programming.
curry: and @@
value:, value:value: and cull, etc. for Symbol
map:, map:map: for BlockClosure and Symbol
<*> and other combinators for BlockClosure and Symbol
nilOr:, emptyOrNilOr:
Slice, Pair and Tuple, ZippedCollection
zip:, >===<
iota
many algorithms on collections: rotate:, slide:, product,
allEqual, unique, isUnique, groupByRunsEqual:,
groupByRunsTrue:
Classes: Functional Programming
PharoFunctional adds several new classes and a variety of extension
methods to facilitate functional programming.
curry: and @@
value:, value:value: and cull, etc. for Symbol
map:, map:map: for BlockClosure and Symbol
<*> and other combinators for BlockClosure and Symbol
nilOr:, emptyOrNilOr:
Slice, Pair and Tuple, ZippedCollection
zip:, >===<
iota
many algorithms on collections: rotate:, slide:, product,
allEqual, unique, isUnique, groupByRunsEqual:,
groupByRunsTrue:
Demo
Using CompileWithCompose
1 Metacello new
2 baseline: ’PharoFunctional’;
3 repository: ’github://dvmason/Pharo-Functional:ma
4 load: #compiler
Then for any class heirarchy, add a trait:
1 RBScannerTest subclass: #ComposeExampleTest
2 uses: ComposeSyntax
3 instanceVariableNames: ’’
4 classVariableNames: ’’
5 package: ’CompileWithCompose-Tests’
Or, on the class-side define the following method:
1 compilerClass
2 " Answer a compiler c l a s s a p p r o p r i a t e f o r source
3 ↑ ComposeCompiler
You can use this second approach if you want to add it to the entire
image (including in playgrounds), by defining this in Object class.
Conclusions
Smalltalk already has the fundamentals for functional
programming
some simple syntactic suger can make it a lot more pleasant
I would love it if some of these became mainstream (with no
backward compatibility issues)
in the meantime, anyone can add this to their Pharo
the compiler tweaks are not hard for other Smalltalks to implement
Conclusions
Smalltalk already has the fundamentals for functional
programming
some simple syntactic suger can make it a lot more pleasant
I would love it if some of these became mainstream (with no
backward compatibility issues)
in the meantime, anyone can add this to their Pharo
the compiler tweaks are not hard for other Smalltalks to implement
Conclusions
Smalltalk already has the fundamentals for functional
programming
some simple syntactic suger can make it a lot more pleasant
I would love it if some of these became mainstream (with no
backward compatibility issues)
in the meantime, anyone can add this to their Pharo
the compiler tweaks are not hard for other Smalltalks to implement
Conclusions
Smalltalk already has the fundamentals for functional
programming
some simple syntactic suger can make it a lot more pleasant
I would love it if some of these became mainstream (with no
backward compatibility issues)
in the meantime, anyone can add this to their Pharo
the compiler tweaks are not hard for other Smalltalks to implement
Conclusions
Smalltalk already has the fundamentals for functional
programming
some simple syntactic suger can make it a lot more pleasant
I would love it if some of these became mainstream (with no
backward compatibility issues)
in the meantime, anyone can add this to their Pharo
the compiler tweaks are not hard for other Smalltalks to implement
Questions?
@DrDaveMason dmason@ryerson.ca
https://github.com/dvmason/Pharo-Functional

Weitere ähnliche Inhalte

Was ist angesagt?

PharoJS: Pharo-Based TDD for Javascript Applications
PharoJS: Pharo-Based TDD for Javascript ApplicationsPharoJS: Pharo-Based TDD for Javascript Applications
PharoJS: Pharo-Based TDD for Javascript Applications
ESUG
 
Continuous integration
Continuous integrationContinuous integration
Continuous integration
amscanne
 

Was ist angesagt? (20)

Towards Object-centric Time-traveling Debuggers
 Towards Object-centric Time-traveling Debuggers Towards Object-centric Time-traveling Debuggers
Towards Object-centric Time-traveling Debuggers
 
PharoJS: Pharo-Based TDD for Javascript Applications
PharoJS: Pharo-Based TDD for Javascript ApplicationsPharoJS: Pharo-Based TDD for Javascript Applications
PharoJS: Pharo-Based TDD for Javascript Applications
 
Microdown
MicrodownMicrodown
Microdown
 
Basic Git Intro
Basic Git IntroBasic Git Intro
Basic Git Intro
 
Git 101: Git and GitHub for Beginners
Git 101: Git and GitHub for Beginners Git 101: Git and GitHub for Beginners
Git 101: Git and GitHub for Beginners
 
Git
GitGit
Git
 
GIT presentation
GIT presentationGIT presentation
GIT presentation
 
Git Branching Model
Git Branching ModelGit Branching Model
Git Branching Model
 
Git and GitHub for Documentation
Git and GitHub for DocumentationGit and GitHub for Documentation
Git and GitHub for Documentation
 
Introduction to Git
Introduction to GitIntroduction to Git
Introduction to Git
 
Github - Git Training Slides: Foundations
Github - Git Training Slides: FoundationsGithub - Git Training Slides: Foundations
Github - Git Training Slides: Foundations
 
CMake - Introduction and best practices
CMake - Introduction and best practicesCMake - Introduction and best practices
CMake - Introduction and best practices
 
Git flow Introduction
Git flow IntroductionGit flow Introduction
Git flow Introduction
 
Git - An Introduction
Git - An IntroductionGit - An Introduction
Git - An Introduction
 
Git & GitHub for Beginners
Git & GitHub for BeginnersGit & GitHub for Beginners
Git & GitHub for Beginners
 
Git branching strategies
Git branching strategiesGit branching strategies
Git branching strategies
 
React JS
React JSReact JS
React JS
 
Pharo 11: A stabilization release
Pharo 11: A stabilization releasePharo 11: A stabilization release
Pharo 11: A stabilization release
 
Applications in Pharo
Applications in PharoApplications in Pharo
Applications in Pharo
 
Continuous integration
Continuous integrationContinuous integration
Continuous integration
 

Ähnlich wie Functional Smalltalk

Ähnlich wie Functional Smalltalk (20)

Aspect-oriented programming in Perl
Aspect-oriented programming in PerlAspect-oriented programming in Perl
Aspect-oriented programming in Perl
 
Swift, swiftly
Swift, swiftlySwift, swiftly
Swift, swiftly
 
A Recovering Java Developer Learns to Go
A Recovering Java Developer Learns to GoA Recovering Java Developer Learns to Go
A Recovering Java Developer Learns to Go
 
The use of the code analysis library OpenC++: modifications, improvements, er...
The use of the code analysis library OpenC++: modifications, improvements, er...The use of the code analysis library OpenC++: modifications, improvements, er...
The use of the code analysis library OpenC++: modifications, improvements, er...
 
New features in Ruby 2.5
New features in Ruby 2.5New features in Ruby 2.5
New features in Ruby 2.5
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 
Matlab for diploma students(1)
Matlab for diploma students(1)Matlab for diploma students(1)
Matlab for diploma students(1)
 
CS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesCS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic Services
 
What's New In Python 2.4
What's New In Python 2.4What's New In Python 2.4
What's New In Python 2.4
 
EKON 12 Closures Coding
EKON 12 Closures CodingEKON 12 Closures Coding
EKON 12 Closures Coding
 
Loops and Unicorns - The Future of the Puppet Language - PuppetConf 2013
Loops and Unicorns - The Future of the Puppet Language - PuppetConf 2013Loops and Unicorns - The Future of the Puppet Language - PuppetConf 2013
Loops and Unicorns - The Future of the Puppet Language - PuppetConf 2013
 
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
 
Andy On Closures
Andy On ClosuresAndy On Closures
Andy On Closures
 
More on bpftrace for MariaDB DBAs and Developers - FOSDEM 2022 MariaDB Devroom
More on bpftrace for MariaDB DBAs and Developers - FOSDEM 2022 MariaDB DevroomMore on bpftrace for MariaDB DBAs and Developers - FOSDEM 2022 MariaDB Devroom
More on bpftrace for MariaDB DBAs and Developers - FOSDEM 2022 MariaDB Devroom
 
Php
PhpPhp
Php
 
Best practices tekx
Best practices tekxBest practices tekx
Best practices tekx
 
Sinatra and friends
Sinatra and friendsSinatra and friends
Sinatra and friends
 
Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharp
 
Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharp
 
What`s New in Java 8
What`s New in Java 8What`s New in Java 8
What`s New in Java 8
 

Mehr von ESUG

Workshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programmingWorkshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programming
ESUG
 
The Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and RoadmapThe Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and Roadmap
ESUG
 
Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...
ESUG
 
Analyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early resultsAnalyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early results
ESUG
 
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
ESUG
 
A Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test GenerationA Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test Generation
ESUG
 
Creating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic ProgrammingCreating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic Programming
ESUG
 
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution ModesThreaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
ESUG
 
Exploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience ReportExploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience Report
ESUG
 
Pharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIsPharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIs
ESUG
 
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame CaseImproving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
ESUG
 
Pharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and FuturePharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and Future
ESUG
 
A New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and TransformationsA New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and Transformations
ESUG
 

Mehr von ESUG (20)

Workshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programmingWorkshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programming
 
Technical documentation support in Pharo
Technical documentation support in PharoTechnical documentation support in Pharo
Technical documentation support in Pharo
 
The Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and RoadmapThe Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and Roadmap
 
Sequence: Pipeline modelling in Pharo
Sequence: Pipeline modelling in PharoSequence: Pipeline modelling in Pharo
Sequence: Pipeline modelling in Pharo
 
Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...
 
Analyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early resultsAnalyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early results
 
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
 
A Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test GenerationA Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test Generation
 
Creating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic ProgrammingCreating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic Programming
 
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution ModesThreaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
 
Exploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience ReportExploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience Report
 
Pharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIsPharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIs
 
Garbage Collector Tuning
Garbage Collector TuningGarbage Collector Tuning
Garbage Collector Tuning
 
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame CaseImproving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
 
Pharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and FuturePharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and Future
 
thisContext in the Debugger
thisContext in the DebuggerthisContext in the Debugger
thisContext in the Debugger
 
Websockets for Fencing Score
Websockets for Fencing ScoreWebsockets for Fencing Score
Websockets for Fencing Score
 
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScriptShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
 
Advanced Object- Oriented Design Mooc
Advanced Object- Oriented Design MoocAdvanced Object- Oriented Design Mooc
Advanced Object- Oriented Design Mooc
 
A New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and TransformationsA New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and Transformations
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

A Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data MigrationA Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data Migration
 
OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024
 
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfA Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 
The Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion ProductionThe Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion Production
 
Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024
 
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdfStrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
 
APVP,apvp apvp High quality supplier safe spot transport, 98% purity
APVP,apvp apvp High quality supplier safe spot transport, 98% purityAPVP,apvp apvp High quality supplier safe spot transport, 98% purity
APVP,apvp apvp High quality supplier safe spot transport, 98% purity
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
 
How to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabberHow to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabber
 
Crafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationCrafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM Integration
 
10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdf10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdf
 
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
 
Microsoft 365 Copilot; An AI tool changing the world of work _PDF.pdf
Microsoft 365 Copilot; An AI tool changing the world of work _PDF.pdfMicrosoft 365 Copilot; An AI tool changing the world of work _PDF.pdf
Microsoft 365 Copilot; An AI tool changing the world of work _PDF.pdf
 
INGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by DesignINGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by Design
 

Functional Smalltalk

  • 1. Functional Smalltalk Dave Mason Toronto Metropolitan University ©2022 Dave Mason
  • 2. Value I’m going to start with a quote from Kent Beck
  • 3. Value Software creates value 2 ways: What it does today What new things we can make it do tomorrow
  • 4. Value Software creates value 2 ways: What it does today What new things we can make it do tomorrow
  • 5. Value Software creates value 2 ways: What it does today What new things we can make it do tomorrow
  • 6. Value Smalltalk creates value 2 ways: What it does today What new things we can make it do tomorrow
  • 7. Functional Smalltalk Smalltalk already has many functional features extensions by syntax extensions by class
  • 8. Functional Smalltalk Smalltalk already has many functional features extensions by syntax extensions by class
  • 9. Syntax: Functional Programming Smalltalk has always had blocks - needed full closures CompileWithCompose in Pharo-Functional repo leverages class-bounded alternative compiler just syntactic sugar - more succinct all are upward compatible as they are currently syntax errors
  • 10. Syntax: Functional Programming Smalltalk has always had blocks - needed full closures CompileWithCompose in Pharo-Functional repo leverages class-bounded alternative compiler just syntactic sugar - more succinct all are upward compatible as they are currently syntax errors
  • 11. Syntax: Functional Programming Smalltalk has always had blocks - needed full closures CompileWithCompose in Pharo-Functional repo leverages class-bounded alternative compiler just syntactic sugar - more succinct all are upward compatible as they are currently syntax errors
  • 12. Syntax: Functional Programming Smalltalk has always had blocks - needed full closures CompileWithCompose in Pharo-Functional repo leverages class-bounded alternative compiler just syntactic sugar - more succinct all are upward compatible as they are currently syntax errors
  • 13. Syntax: Functional Programming Smalltalk has always had blocks - needed full closures CompileWithCompose in Pharo-Functional repo leverages class-bounded alternative compiler just syntactic sugar - more succinct all are upward compatible as they are currently syntax errors
  • 14. Compose/pipe/parrot operator very convenient to pass result of one expression to another without parentheses particularly convenient in PharoJS for e.g. D3
  • 15. Compose/pipe/parrot operator very convenient to pass result of one expression to another without parentheses particularly convenient in PharoJS for e.g. D3 1 foo 2 " s e l f new foo >>> 42 " 3 ↑ 17 negated 4 :> min: -53 5 :> abs 6 :> < 100 7 :> and: [ 4 > 2 ] 8 :> and: [ 5 < 10 ] 9 :> ifTrue: [ 42 ] ifFalse: [ 99 ]
  • 16. Compose/pipe/parrot operator very convenient to pass result of one expression to another without parentheses particularly convenient in PharoJS for e.g. D3 1 foo 2 " s e l f new foo >>> 42 " 3 ↑ 17 negated 4 :> min: -53 5 :> abs 6 :> < 100 7 :> and: [ 4 > 2 ] 8 :> and: [ 5 < 10 ] 9 :> ifTrue: [ 42 ] ifFalse: [ 99 ]
  • 17. ... Compose/pipe/parrot operator The precedence is the same as cascade, so you can intermix them and could say something like: 1 x := OrderedCollection new 2 add: 42; 3 add: 17; 4 yourself 5 :> collect: #negated 6 :> add: 35; 7 add: 99; 8 yourself 9 :> with: #(1 2 3 4) collect: [:l :r| l+r ] 10 :> max
  • 18. ... Compose/pipe/parrot operator If you don’t want to use the alternate compiler (and get the :> syntax) PharoFunctional also provides a chain method on Object that supports chaining using cascades (unfortunately quite a bit slower because it requires a DNU and perform for each chained message): 1 foo 2 " s e l f new foo >>> 42 " 3 ↑ 17 chain 4 negated 5 ; min: -53 6 ; abs 7 ; < 100 8 ; and: [ 4 > 2 ] 9 ; and: [ 5 < 10 ] 10 ; ifTrue: [ 42 ] ifFalse: [ 99 ]
  • 19. Point-free programming style popular style of functional programming composing functions to build up operations with implicit parameters various “combinators” that recognize patterns in these compositions in Smalltalk this is composing symbols and blocks e.g. 1 isPalindrome := #reverse <| > #= . 2 isPalindrome value: ’madam’
  • 20. Point-free programming style popular style of functional programming composing functions to build up operations with implicit parameters various “combinators” that recognize patterns in these compositions in Smalltalk this is composing symbols and blocks e.g. 1 isPalindrome := #reverse <| > #= . 2 isPalindrome value: ’madam’
  • 21. Point-free programming style popular style of functional programming composing functions to build up operations with implicit parameters various “combinators” that recognize patterns in these compositions in Smalltalk this is composing symbols and blocks e.g. 1 isPalindrome := #reverse <| > #= . 2 isPalindrome value: ’madam’
  • 22. Point-free programming style popular style of functional programming composing functions to build up operations with implicit parameters various “combinators” that recognize patterns in these compositions in Smalltalk this is composing symbols and blocks e.g. 1 isPalindrome := #reverse <| > #= . 2 isPalindrome value: ’madam’
  • 23. Point-free programming style popular style of functional programming composing functions to build up operations with implicit parameters various “combinators” that recognize patterns in these compositions in Smalltalk this is composing symbols and blocks e.g. 1 isPalindrome := #reverse <| > #= . 2 isPalindrome value: ’madam’
  • 24. Expressions as unary or binary messages To use point-free style, it is very convenient to have a more succinct syntax for applying them 1 x (...) 2 x (...) + y 3 x (...): y 4 x (#sort <| > #=) Converts to. 1 ([...] value: x) 2 ([...] value: x) + y 3 ([...] value: x value: y) 4 ((#sort <| > #=) value: x)
  • 25. Blocks as unary or binary messages You can do the same with unary or binary blocks. Because we know the arity of blocks the trailing : isn’t used for block operators 1 x [:w| ...] 2 x [:w:z| ...] y becomes 1 ([:w| ...] value: x) 2 ([:w:z| ...] value: x value: y)
  • 26. Initializing local variables at point of declaration Even in functional languages where mutation is possible, it is rarely used. Instead programming is by a sequence of definitions, which always have a value. I personally very much miss this in Smalltalk. 1 | w x := 42. y = x+5. z a | is legal, but 1 | x := 42. y = x+5. z = 17 | isn’t.
  • 27. Collection literals Arrays have a literal syntax {1 . 2 . 3}, but other collections don’t. This extension recognizes :className immediately after the { and translates, e.g. 1 {:Set 3 . 4 . 5 . 3} 2 {:Dictionary #a->1 . #b->2} 3 {:Set 1 . 2 . 3 . 4 . 5 . 6 . 7} to 1 Set with: 3 with: 4 with: 5 with: 3 2 Dictionary with: #a->1 with: #b->2 3 Set withAll: {1 . 2 . 3 . 4 . 5 . 6 . 7}
  • 28. Destructuring collections There isn’t a convenient way to return multiple values from a method, or even to extract multiple values from a collection. For example: 1 :| a b c | := some-collection destructures the 3 elements of a SequenceableCollection or would extract the value of keys #a #b etc. if it was a Dictionary, with anything else being a runtime error. This is conveniently done by converting that to: 1 ([:temp| 2 a := temp firstNamed: #a. 3 b := temp secondNamed: #b. 4 c := temp thirdNamed: #c. 5 temp] value: some-collection)
  • 29. Classes: Functional Programming PharoFunctional adds several new classes and a variety of extension methods to facilitate functional programming. curry: and @@ value:, value:value: and cull, etc. for Symbol map:, map:map: for BlockClosure and Symbol <*> and other combinators for BlockClosure and Symbol nilOr:, emptyOrNilOr: Slice, Pair and Tuple, ZippedCollection zip:, >===< iota many algorithms on collections: rotate:, slide:, product, allEqual, unique, isUnique, groupByRunsEqual:, groupByRunsTrue:
  • 30. Classes: Functional Programming PharoFunctional adds several new classes and a variety of extension methods to facilitate functional programming. curry: and @@ value:, value:value: and cull, etc. for Symbol map:, map:map: for BlockClosure and Symbol <*> and other combinators for BlockClosure and Symbol nilOr:, emptyOrNilOr: Slice, Pair and Tuple, ZippedCollection zip:, >===< iota many algorithms on collections: rotate:, slide:, product, allEqual, unique, isUnique, groupByRunsEqual:, groupByRunsTrue:
  • 31. Classes: Functional Programming PharoFunctional adds several new classes and a variety of extension methods to facilitate functional programming. curry: and @@ value:, value:value: and cull, etc. for Symbol map:, map:map: for BlockClosure and Symbol <*> and other combinators for BlockClosure and Symbol nilOr:, emptyOrNilOr: Slice, Pair and Tuple, ZippedCollection zip:, >===< iota many algorithms on collections: rotate:, slide:, product, allEqual, unique, isUnique, groupByRunsEqual:, groupByRunsTrue:
  • 32. Classes: Functional Programming PharoFunctional adds several new classes and a variety of extension methods to facilitate functional programming. curry: and @@ value:, value:value: and cull, etc. for Symbol map:, map:map: for BlockClosure and Symbol <*> and other combinators for BlockClosure and Symbol nilOr:, emptyOrNilOr: Slice, Pair and Tuple, ZippedCollection zip:, >===< iota many algorithms on collections: rotate:, slide:, product, allEqual, unique, isUnique, groupByRunsEqual:, groupByRunsTrue:
  • 33. Classes: Functional Programming PharoFunctional adds several new classes and a variety of extension methods to facilitate functional programming. curry: and @@ value:, value:value: and cull, etc. for Symbol map:, map:map: for BlockClosure and Symbol <*> and other combinators for BlockClosure and Symbol nilOr:, emptyOrNilOr: Slice, Pair and Tuple, ZippedCollection zip:, >===< iota many algorithms on collections: rotate:, slide:, product, allEqual, unique, isUnique, groupByRunsEqual:, groupByRunsTrue:
  • 34. Classes: Functional Programming PharoFunctional adds several new classes and a variety of extension methods to facilitate functional programming. curry: and @@ value:, value:value: and cull, etc. for Symbol map:, map:map: for BlockClosure and Symbol <*> and other combinators for BlockClosure and Symbol nilOr:, emptyOrNilOr: Slice, Pair and Tuple, ZippedCollection zip:, >===< iota many algorithms on collections: rotate:, slide:, product, allEqual, unique, isUnique, groupByRunsEqual:, groupByRunsTrue:
  • 35. Classes: Functional Programming PharoFunctional adds several new classes and a variety of extension methods to facilitate functional programming. curry: and @@ value:, value:value: and cull, etc. for Symbol map:, map:map: for BlockClosure and Symbol <*> and other combinators for BlockClosure and Symbol nilOr:, emptyOrNilOr: Slice, Pair and Tuple, ZippedCollection zip:, >===< iota many algorithms on collections: rotate:, slide:, product, allEqual, unique, isUnique, groupByRunsEqual:, groupByRunsTrue:
  • 36. Classes: Functional Programming PharoFunctional adds several new classes and a variety of extension methods to facilitate functional programming. curry: and @@ value:, value:value: and cull, etc. for Symbol map:, map:map: for BlockClosure and Symbol <*> and other combinators for BlockClosure and Symbol nilOr:, emptyOrNilOr: Slice, Pair and Tuple, ZippedCollection zip:, >===< iota many algorithms on collections: rotate:, slide:, product, allEqual, unique, isUnique, groupByRunsEqual:, groupByRunsTrue:
  • 37. Classes: Functional Programming PharoFunctional adds several new classes and a variety of extension methods to facilitate functional programming. curry: and @@ value:, value:value: and cull, etc. for Symbol map:, map:map: for BlockClosure and Symbol <*> and other combinators for BlockClosure and Symbol nilOr:, emptyOrNilOr: Slice, Pair and Tuple, ZippedCollection zip:, >===< iota many algorithms on collections: rotate:, slide:, product, allEqual, unique, isUnique, groupByRunsEqual:, groupByRunsTrue:
  • 38. Demo
  • 39. Using CompileWithCompose 1 Metacello new 2 baseline: ’PharoFunctional’; 3 repository: ’github://dvmason/Pharo-Functional:ma 4 load: #compiler Then for any class heirarchy, add a trait: 1 RBScannerTest subclass: #ComposeExampleTest 2 uses: ComposeSyntax 3 instanceVariableNames: ’’ 4 classVariableNames: ’’ 5 package: ’CompileWithCompose-Tests’ Or, on the class-side define the following method: 1 compilerClass 2 " Answer a compiler c l a s s a p p r o p r i a t e f o r source 3 ↑ ComposeCompiler You can use this second approach if you want to add it to the entire image (including in playgrounds), by defining this in Object class.
  • 40. Conclusions Smalltalk already has the fundamentals for functional programming some simple syntactic suger can make it a lot more pleasant I would love it if some of these became mainstream (with no backward compatibility issues) in the meantime, anyone can add this to their Pharo the compiler tweaks are not hard for other Smalltalks to implement
  • 41. Conclusions Smalltalk already has the fundamentals for functional programming some simple syntactic suger can make it a lot more pleasant I would love it if some of these became mainstream (with no backward compatibility issues) in the meantime, anyone can add this to their Pharo the compiler tweaks are not hard for other Smalltalks to implement
  • 42. Conclusions Smalltalk already has the fundamentals for functional programming some simple syntactic suger can make it a lot more pleasant I would love it if some of these became mainstream (with no backward compatibility issues) in the meantime, anyone can add this to their Pharo the compiler tweaks are not hard for other Smalltalks to implement
  • 43. Conclusions Smalltalk already has the fundamentals for functional programming some simple syntactic suger can make it a lot more pleasant I would love it if some of these became mainstream (with no backward compatibility issues) in the meantime, anyone can add this to their Pharo the compiler tweaks are not hard for other Smalltalks to implement
  • 44. Conclusions Smalltalk already has the fundamentals for functional programming some simple syntactic suger can make it a lot more pleasant I would love it if some of these became mainstream (with no backward compatibility issues) in the meantime, anyone can add this to their Pharo the compiler tweaks are not hard for other Smalltalks to implement