SlideShare a Scribd company logo
1 of 53
Download to read offline
Scala QQ
zooey
QuickSort - Scala (functional type)
QuickSort - Scala (functional type)
No return !
MAGIC!
MAGIC!
MAGIC!
MATH!!
MAGIC!
Scala is a Functional Programming language.
And Functional Programming is in essence MATH….
MATH!!
● From Wiki:
In computer science, functional programming is a programming paradigm—a style of
building the structure and elements of computer programs—that treats computation as the
evaluation of mathematical functions and avoids changing-state and mutable data.
● Everything is MATH, even the simplest form of presentation:
○ Type-theory
2: natural means value: type
○ Infix operator
E op E’ means operate between E and E’,
● 1 + 2,
● a max b (dot notation: a.max(b)),
● x defined_function y
Functional Programming
infix notation
filter is
operator!
Type !
filter is
operator!
Type !
Functional programming treats computation as
the evaluation of mathematical functions
How?
How to present computation or program with
mathematical functions?
E op E’
Expression!!
Expression-Oriented Language
● All functional programming languages are expression-oriented.
An expression-oriented programming language is a programming language where every
(or nearly every) construction is an expression and thus yields a value.
● Statement v.s Expression
● Benefits to EOP: easier to test, reason, and express
● Scala expression return a value
○ If/else expression: val greater = if (a > b) a else b
○ Try/catch block:
○ A function call return value
Expression!
Function call
Expression !
● there is no explicit return → Scala returns “expression”
● Programming style will be consisted in “expression”
E op E’
Functional Language
● First-class citizen:
an entity which supports all the operations generally available to other entities.
● First-class function:
A PL treat function as first-class citizen, supports
● Passing functions as function arguments
● Return functions as value from other functions
● Assign function to variable or store in data structure → function literal/value
● Higher-order function
In mathematics and computer science, a higher-order function (also functional, functional form
or functor) is a function that does at least one of the following:
1. takes one or more functions as arguments (i.e., procedural parameters),
2. returns a function as its result.
Func1 Func2
First-class function
Higher-order
function
How do we express function?
- Functional programming languages implement the lambda calculus. -
Lambda Calculus!
Lambda calculus
● Lambda calculus (λ-calculus) is somehow a tool that provides provides a simple
semantic to express function (computation)
Lambda calculus is a formal system in mathematical logic for expressing computation based on function abstraction and
application using variable binding and substitution, which is developed in the 1930s to investigate computability.
● Two simplifications to make semantic simple
1. Anonymous function
Anonymous functions are sometimes called lambda expressions.
2. uses functions of a single input
An ordinary function that requires two inputs, ex. f(x, y), can be reworked into an equivalent
function that accepts a single input, and as output returns another function, that in turn accepts
a single input.
Anonymous Function
● Nested function
a function which is defined within another function, the enclosing function.
● Anonymous function: syntactically lighter than a named function
In computer programming, an anonymous function (function literal, lambda abstraction) is a
function definition that is not bound to an identifier.
Anonymous functions are a form of nested function, in allowing access to variables in the
scope of the containing function (non-local variables). This means anonymous functions need
to be implemented using closures.
● Scala anonymous function syntax: fat arrow => to separate params and body
Closure
● Closure
Operationally, a closure is a record storing a function[a] together with an environment
The referencing environment binds the non-local names to the corresponding variables in the
lexical environment at the time the closure is created, additionally extending their lifetime to at
least as long as the lifetime of the closure itself.
● Free variable and bound variable
After doSome(), x is still alive in
the following two println...
Closure
Every time doSome got called,
f and execution context for f will
returned.
doSome
(execution context for f)
f
(function to execute)
x
(Bound variable for
doSome, free variable
for f)
Two different execution context
● So
○ Function is a first-class citizen, so we can have function literal
○ Function variable is constant since function is literal:
val function_name = …
○ filter is an operator takes two expressions: xs and an anonymous function
○ In sort(xs filter (pivot >)) , sort and filter are higher-order functions since they take
function as argument or return function
○ No closure here
E op E’
● So
○ Function is a first-class citizen, so we can have function literal
○ Function variable is constant since function is literal:
val function_name = …
○ filter is an operator takes two expressions: xs and an anonymous function
○ In sort(xs filter (pivot >)) , sort and filter are higher-order functions since they take
function as argument or return function
○ No closure here
A function call/expression is an
argument for the *filter* operator…
Should be (x => pivot > x)
(pivot >) … WTH?
E op E’
Simple Semantic for Lambda
● Concise syntax
● Closure and syntax
■ Dynamic programming language (ex. python, js)
■ Static programming language (ex. java, scala)
→ end up writing code with tons of types
→ Type inference in Scala
● Arity
○ Arity-0: no arguments. omission of parentheses on methods of arity-0
○ Arity-1: one arguments. This syntax is formally known as “infix notation”. It should
only be used for
● purely-functional methods (methods with no side-effects) - such as mkString, or
● methods which take functions as parameters - such as foreach:
Simple Semantic for Lambda
● Concise syntax
● Closure and syntax
■ Dynamic programming language (ex. python, js)
■ Static programming language (ex. java, scala)
→ end up writing code with tons of types
→ Type inference in Scala
● Arity (wiki)
○ Arity-0: no arguments. omission of parentheses on methods of arity-0
○ Arity-1: one arguments. This syntax is formally known as “infix notation”. It should
only be used for
● purely-functional methods (methods with no side-effects) - such as mkString, or
● methods which take functions as parameters - such as foreach
● Issue
○ Infix v.s dot (stackoverflow discussion)
○ Complicated Underscore (stackoverflow discussion)
Different world view for Object-oriented and functional?
New World
World View: OOP
Obj1
Obj2
input
Obj3
Data
Method
Data
Method
Data
Method
Interface
Out
action
action
World View: OOP
Obj1
Obj2
input
Obj3
Data
Method
Data
Method
Data
Method
Interface
Out
Obj a
(superclass)
Data
Method
action
action
World View: OOP
Obj1
Obj2
input
Obj3
Data
Method
Data
Method
Data
Method
Interface
Out
Obj a
(superclass)
Data
Method
action
action
Reusable entity
World View: OOP
Obj1
Obj2
input
Obj3
Data
Method
Data
Method
Data
Method
Interface
Out
Obj a
(superclass)
Data
Method
action
action
Abstraction with objects
Reusable entity
World View: FP
Func 1
Func 2
input
Func 3
Out
Func 4 Func 5
F(input) = out
World View: FP
Func 1
Func 2
input
Func 3
Out
Func 4 Func 5
F(input) = out
F = F1(F2, F3(F4, F5)) x2
+ 2x + 1 = (x + 1)2
World View: FP
Func 1
Func 2
input
Func 3
Out
Func 4 Func 5
F(input) = out
Func 31 Func 32 Func 33Func 2’ Data
World View: FP
Func 1
Func 2
input
Func 3
Out
Func 4 Func 5
F(input) = out
Reusable entity
Func 31 Func 32 Func 33Func 2’ Data
World View: FP
Func 1
Func 2
input
Func 3
Out
Func 4 Func 5
F(input) = out
Abstraction with Functions
Reusable entity
Func 31 Func 32 Func 33Func 2’ Data
Function, data, syntax, domain specific language
Abstraction
Partially applied function
● Apply (wiki)
In mathematics and computer science, apply is a
function that applies functions to arguments.
● In functional programming,
○ Fully applied function
when you call a function with all parameters
○ Partially applied function
when you call a function with a subset of parameters
Programexecution
Func2’(?=3)
Func2’(?=9)
Func2’(?=13)
Func2’ Data ?
Func2 arguments
● You can not only replace one parameter with the placeholder syntax “_” myNumbers.foreach(println(_))
but also an entire parameter list: myNumbers.foreach(println _)
Partially applied function
● Example: HTML wrapper
A normal function call:
A partially applied function:
● Anonymous function
○ Initially (x => pivot > x)
○ Shorthand partially applied function (pivot > _)
○ Drop underscore (pivot > )
NOTE: Leaving out the underscore is only allowed when a function is expected:
● val c = t2 → Not OK!
● val c = t2 _ → OK!
● Anonymous function
○ Initially (x => pivot > x)
○ Shorthand partially applied function (pivot > _)
○ Drop underscore (pivot > )
NOTE: Leaving out the underscore is only allowed when a function is expected.
● These two are different: funcA _ v.s. funcA(_)
Currying
● Currying is the technique of translating the evaluation of a function that takes multiple
arguments (or a tuple of arguments) into evaluating a sequence of functions, each with
a single argument.
F(x, y, z) → F(x)(y)(z) Func 31 Func 32 Func 33Func 3
● Currying is useful in both practical and theoretical settings.
○ Practical
provides a way of automatically managing how arguments are passed to functions and exceptions in functional
programming.
○ Theoretical
provides a way to study functions with multiple arguments in simpler theoretical models with only one argument
Function Composition
● Compose: f(g(x))
● andThen: g(f(x))
Polymorphic Methods
Method dup is parameterized with type T and
with the value parameters x: T and n:
Int.
In the first call to dup, the programmer
provides the required parameters, but as
the following line shows, the programmer is
not required to give actual type parameters
explicitly.
The type system of Scala can infer such
types. This is done by looking at the types of
the given value parameters and at the
context where the method is called.
Passing Parameter
● a, b, cond, func are called By-value parameters
By-name Parameter
● Only function body got passed
By-name Parameter
● Only function body got passed
By-name Parameter
● Only function body got passed
● By-name parameter is NOT a function object, just a name, cannot be called
Syntax Abstraction
● Function abstraction
○ lambda calculus provide flexibility to define function/formula, control flow, etc.
● Syntactic abstraction
○ Build its own syntax → Domain specific language
By-name parameter + Curry:
Unless and until seems to be a syntax
Recap
● Functional, expression-oriented language
○ Expression
○ First-class function
● Lambda calculus
○ Anonymous function
○ Closure
○ Simple semantic
● World view: OOP and FP
● Abstraction
○ Function
■ Partially applied function
■ Currying
■ Function composition
■ Polymorphic methods
○ Syntax
■ By-value, by-name parameter
Think MATH
To Be Continued ...
Reference
● Codes are from
○ http://www.scala-lang.org/docu/files/ScalaByExample.pdf chapter 2
○ https://openhome.cc/Gossip/Scala/index.html
○ http://alvinalexander.com/
● Wiki pages
Appendix
Infix and dot
● Infix can be cumbersome http://stackoverflow.com/questions/10233227/scala-infix-vs-dot-notation
● Scala style guide (method invocation)
Infix > dot, when...
○ Suffix Notation: names.toList
○ Arity-1
○ Higher-Order Functions
○
○ Symbolic methods/Operators:
Underscore

More Related Content

What's hot

Review of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iiiReview of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iiiNico Ludwig
 
Thinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonThinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonAnoop Thomas Mathew
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translationAkshaya Arunan
 
Introduction to functional programming
Introduction to functional programmingIntroduction to functional programming
Introduction to functional programmingKonrad Szydlo
 
C++ overloading
C++ overloadingC++ overloading
C++ overloadingsanya6900
 
Tokens expressionsin C++
Tokens expressionsin C++Tokens expressionsin C++
Tokens expressionsin C++HalaiHansaika
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parametersKnoldus Inc.
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture TwoAngelo Corsaro
 
New c sharp3_features_(linq)_part_v
New c sharp3_features_(linq)_part_vNew c sharp3_features_(linq)_part_v
New c sharp3_features_(linq)_part_vNico Ludwig
 
Reflection in Go
Reflection in GoReflection in Go
Reflection in Gostrikr .
 
Intro f# functional_programming
Intro f# functional_programmingIntro f# functional_programming
Intro f# functional_programmingMauro Ghiani
 
Dev Concepts: Functional Programming
Dev Concepts: Functional ProgrammingDev Concepts: Functional Programming
Dev Concepts: Functional ProgrammingSvetlin Nakov
 
C cpluplus 2
C cpluplus 2C cpluplus 2
C cpluplus 2sanya6900
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScripttmont
 

What's hot (20)

Some basic FP concepts
Some basic FP conceptsSome basic FP concepts
Some basic FP concepts
 
LISP:Program structure in lisp
LISP:Program structure in lispLISP:Program structure in lisp
LISP:Program structure in lisp
 
Software Developer Training
Software Developer TrainingSoftware Developer Training
Software Developer Training
 
Review of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iiiReview of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iii
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Thinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonThinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in Python
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Introduction to functional programming
Introduction to functional programmingIntroduction to functional programming
Introduction to functional programming
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
Tokens expressionsin C++
Tokens expressionsin C++Tokens expressionsin C++
Tokens expressionsin C++
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
 
Theory of computation and automata
Theory of computation and automataTheory of computation and automata
Theory of computation and automata
 
LISP:Control Structures In Lisp
LISP:Control Structures In LispLISP:Control Structures In Lisp
LISP:Control Structures In Lisp
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
 
New c sharp3_features_(linq)_part_v
New c sharp3_features_(linq)_part_vNew c sharp3_features_(linq)_part_v
New c sharp3_features_(linq)_part_v
 
Reflection in Go
Reflection in GoReflection in Go
Reflection in Go
 
Intro f# functional_programming
Intro f# functional_programmingIntro f# functional_programming
Intro f# functional_programming
 
Dev Concepts: Functional Programming
Dev Concepts: Functional ProgrammingDev Concepts: Functional Programming
Dev Concepts: Functional Programming
 
C cpluplus 2
C cpluplus 2C cpluplus 2
C cpluplus 2
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScript
 

Similar to Scala qq

Advance python programming
Advance python programming Advance python programming
Advance python programming Jagdish Chavan
 
Python functional programming
Python functional programmingPython functional programming
Python functional programmingGeison Goes
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scaladatamantra
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programmingNico Ludwig
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional ProgrammingGeison Goes
 
Functional Programming.pptx
Functional Programming.pptxFunctional Programming.pptx
Functional Programming.pptxKarthickT28
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextUnfold UI
 
what-is-python-presentation.pptx
what-is-python-presentation.pptxwhat-is-python-presentation.pptx
what-is-python-presentation.pptxVijay Krishna
 
What is Paython.pptx
What is Paython.pptxWhat is Paython.pptx
What is Paython.pptxParag Soni
 
Programming Methodologies Functions - C Language
Programming Methodologies Functions - C LanguageProgramming Methodologies Functions - C Language
Programming Methodologies Functions - C LanguageChobodiDamsaraniPadm
 
uom-2552-what-is-python-presentation.pptx
uom-2552-what-is-python-presentation.pptxuom-2552-what-is-python-presentation.pptx
uom-2552-what-is-python-presentation.pptxChetanChauhan203001
 
uom-2552-what-is-python-presentation (1).pptx
uom-2552-what-is-python-presentation (1).pptxuom-2552-what-is-python-presentation (1).pptx
uom-2552-what-is-python-presentation (1).pptxPrabha Karan
 
python-presentation.pptx
python-presentation.pptxpython-presentation.pptx
python-presentation.pptxVijay Krishna
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++Manzoor ALam
 
Introduction Functional Programming - Tech Hangout #11 - 2013.01.16
Introduction Functional Programming - Tech Hangout #11 - 2013.01.16Introduction Functional Programming - Tech Hangout #11 - 2013.01.16
Introduction Functional Programming - Tech Hangout #11 - 2013.01.16Innovecs
 
Introduction To Programming with Python-1
Introduction To Programming with Python-1Introduction To Programming with Python-1
Introduction To Programming with Python-1Syed Farjad Zia Zaidi
 

Similar to Scala qq (20)

Advance python programming
Advance python programming Advance python programming
Advance python programming
 
Python functional programming
Python functional programmingPython functional programming
Python functional programming
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional Programming
 
Functional Programming.pptx
Functional Programming.pptxFunctional Programming.pptx
Functional Programming.pptx
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
Java 8
Java 8Java 8
Java 8
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNext
 
what-is-python-presentation.pptx
what-is-python-presentation.pptxwhat-is-python-presentation.pptx
what-is-python-presentation.pptx
 
What is Paython.pptx
What is Paython.pptxWhat is Paython.pptx
What is Paython.pptx
 
Programming Methodologies Functions - C Language
Programming Methodologies Functions - C LanguageProgramming Methodologies Functions - C Language
Programming Methodologies Functions - C Language
 
uom-2552-what-is-python-presentation.pptx
uom-2552-what-is-python-presentation.pptxuom-2552-what-is-python-presentation.pptx
uom-2552-what-is-python-presentation.pptx
 
uom-2552-what-is-python-presentation (1).pptx
uom-2552-what-is-python-presentation (1).pptxuom-2552-what-is-python-presentation (1).pptx
uom-2552-what-is-python-presentation (1).pptx
 
python-presentation.pptx
python-presentation.pptxpython-presentation.pptx
python-presentation.pptx
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
 
Introduction Functional Programming - Tech Hangout #11 - 2013.01.16
Introduction Functional Programming - Tech Hangout #11 - 2013.01.16Introduction Functional Programming - Tech Hangout #11 - 2013.01.16
Introduction Functional Programming - Tech Hangout #11 - 2013.01.16
 
Introductory func prog
Introductory func progIntroductory func prog
Introductory func prog
 
Introduction To Programming with Python-1
Introduction To Programming with Python-1Introduction To Programming with Python-1
Introduction To Programming with Python-1
 

Recently uploaded

CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIkoyaldeepu123
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixingviprabot1
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 

Recently uploaded (20)

CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AI
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixing
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 

Scala qq

  • 2. QuickSort - Scala (functional type)
  • 3. QuickSort - Scala (functional type) No return !
  • 7. MAGIC! Scala is a Functional Programming language. And Functional Programming is in essence MATH…. MATH!!
  • 8. ● From Wiki: In computer science, functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. ● Everything is MATH, even the simplest form of presentation: ○ Type-theory 2: natural means value: type ○ Infix operator E op E’ means operate between E and E’, ● 1 + 2, ● a max b (dot notation: a.max(b)), ● x defined_function y Functional Programming infix notation
  • 10. filter is operator! Type ! Functional programming treats computation as the evaluation of mathematical functions How?
  • 11. How to present computation or program with mathematical functions? E op E’ Expression!!
  • 12. Expression-Oriented Language ● All functional programming languages are expression-oriented. An expression-oriented programming language is a programming language where every (or nearly every) construction is an expression and thus yields a value. ● Statement v.s Expression ● Benefits to EOP: easier to test, reason, and express ● Scala expression return a value ○ If/else expression: val greater = if (a > b) a else b ○ Try/catch block: ○ A function call return value
  • 13. Expression! Function call Expression ! ● there is no explicit return → Scala returns “expression” ● Programming style will be consisted in “expression” E op E’
  • 14. Functional Language ● First-class citizen: an entity which supports all the operations generally available to other entities. ● First-class function: A PL treat function as first-class citizen, supports ● Passing functions as function arguments ● Return functions as value from other functions ● Assign function to variable or store in data structure → function literal/value ● Higher-order function In mathematics and computer science, a higher-order function (also functional, functional form or functor) is a function that does at least one of the following: 1. takes one or more functions as arguments (i.e., procedural parameters), 2. returns a function as its result. Func1 Func2 First-class function Higher-order function
  • 15. How do we express function? - Functional programming languages implement the lambda calculus. - Lambda Calculus!
  • 16. Lambda calculus ● Lambda calculus (λ-calculus) is somehow a tool that provides provides a simple semantic to express function (computation) Lambda calculus is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution, which is developed in the 1930s to investigate computability. ● Two simplifications to make semantic simple 1. Anonymous function Anonymous functions are sometimes called lambda expressions. 2. uses functions of a single input An ordinary function that requires two inputs, ex. f(x, y), can be reworked into an equivalent function that accepts a single input, and as output returns another function, that in turn accepts a single input.
  • 17. Anonymous Function ● Nested function a function which is defined within another function, the enclosing function. ● Anonymous function: syntactically lighter than a named function In computer programming, an anonymous function (function literal, lambda abstraction) is a function definition that is not bound to an identifier. Anonymous functions are a form of nested function, in allowing access to variables in the scope of the containing function (non-local variables). This means anonymous functions need to be implemented using closures. ● Scala anonymous function syntax: fat arrow => to separate params and body
  • 18. Closure ● Closure Operationally, a closure is a record storing a function[a] together with an environment The referencing environment binds the non-local names to the corresponding variables in the lexical environment at the time the closure is created, additionally extending their lifetime to at least as long as the lifetime of the closure itself. ● Free variable and bound variable After doSome(), x is still alive in the following two println...
  • 19. Closure Every time doSome got called, f and execution context for f will returned. doSome (execution context for f) f (function to execute) x (Bound variable for doSome, free variable for f) Two different execution context
  • 20. ● So ○ Function is a first-class citizen, so we can have function literal ○ Function variable is constant since function is literal: val function_name = … ○ filter is an operator takes two expressions: xs and an anonymous function ○ In sort(xs filter (pivot >)) , sort and filter are higher-order functions since they take function as argument or return function ○ No closure here E op E’
  • 21. ● So ○ Function is a first-class citizen, so we can have function literal ○ Function variable is constant since function is literal: val function_name = … ○ filter is an operator takes two expressions: xs and an anonymous function ○ In sort(xs filter (pivot >)) , sort and filter are higher-order functions since they take function as argument or return function ○ No closure here A function call/expression is an argument for the *filter* operator… Should be (x => pivot > x) (pivot >) … WTH? E op E’
  • 22. Simple Semantic for Lambda ● Concise syntax ● Closure and syntax ■ Dynamic programming language (ex. python, js) ■ Static programming language (ex. java, scala) → end up writing code with tons of types → Type inference in Scala ● Arity ○ Arity-0: no arguments. omission of parentheses on methods of arity-0 ○ Arity-1: one arguments. This syntax is formally known as “infix notation”. It should only be used for ● purely-functional methods (methods with no side-effects) - such as mkString, or ● methods which take functions as parameters - such as foreach:
  • 23. Simple Semantic for Lambda ● Concise syntax ● Closure and syntax ■ Dynamic programming language (ex. python, js) ■ Static programming language (ex. java, scala) → end up writing code with tons of types → Type inference in Scala ● Arity (wiki) ○ Arity-0: no arguments. omission of parentheses on methods of arity-0 ○ Arity-1: one arguments. This syntax is formally known as “infix notation”. It should only be used for ● purely-functional methods (methods with no side-effects) - such as mkString, or ● methods which take functions as parameters - such as foreach ● Issue ○ Infix v.s dot (stackoverflow discussion) ○ Complicated Underscore (stackoverflow discussion)
  • 24. Different world view for Object-oriented and functional? New World
  • 27. World View: OOP Obj1 Obj2 input Obj3 Data Method Data Method Data Method Interface Out Obj a (superclass) Data Method action action Reusable entity
  • 28. World View: OOP Obj1 Obj2 input Obj3 Data Method Data Method Data Method Interface Out Obj a (superclass) Data Method action action Abstraction with objects Reusable entity
  • 29. World View: FP Func 1 Func 2 input Func 3 Out Func 4 Func 5 F(input) = out
  • 30. World View: FP Func 1 Func 2 input Func 3 Out Func 4 Func 5 F(input) = out F = F1(F2, F3(F4, F5)) x2 + 2x + 1 = (x + 1)2
  • 31. World View: FP Func 1 Func 2 input Func 3 Out Func 4 Func 5 F(input) = out Func 31 Func 32 Func 33Func 2’ Data
  • 32. World View: FP Func 1 Func 2 input Func 3 Out Func 4 Func 5 F(input) = out Reusable entity Func 31 Func 32 Func 33Func 2’ Data
  • 33. World View: FP Func 1 Func 2 input Func 3 Out Func 4 Func 5 F(input) = out Abstraction with Functions Reusable entity Func 31 Func 32 Func 33Func 2’ Data
  • 34. Function, data, syntax, domain specific language Abstraction
  • 35. Partially applied function ● Apply (wiki) In mathematics and computer science, apply is a function that applies functions to arguments. ● In functional programming, ○ Fully applied function when you call a function with all parameters ○ Partially applied function when you call a function with a subset of parameters Programexecution Func2’(?=3) Func2’(?=9) Func2’(?=13) Func2’ Data ? Func2 arguments ● You can not only replace one parameter with the placeholder syntax “_” myNumbers.foreach(println(_)) but also an entire parameter list: myNumbers.foreach(println _)
  • 36. Partially applied function ● Example: HTML wrapper A normal function call: A partially applied function:
  • 37. ● Anonymous function ○ Initially (x => pivot > x) ○ Shorthand partially applied function (pivot > _) ○ Drop underscore (pivot > ) NOTE: Leaving out the underscore is only allowed when a function is expected: ● val c = t2 → Not OK! ● val c = t2 _ → OK!
  • 38. ● Anonymous function ○ Initially (x => pivot > x) ○ Shorthand partially applied function (pivot > _) ○ Drop underscore (pivot > ) NOTE: Leaving out the underscore is only allowed when a function is expected. ● These two are different: funcA _ v.s. funcA(_)
  • 39. Currying ● Currying is the technique of translating the evaluation of a function that takes multiple arguments (or a tuple of arguments) into evaluating a sequence of functions, each with a single argument. F(x, y, z) → F(x)(y)(z) Func 31 Func 32 Func 33Func 3 ● Currying is useful in both practical and theoretical settings. ○ Practical provides a way of automatically managing how arguments are passed to functions and exceptions in functional programming. ○ Theoretical provides a way to study functions with multiple arguments in simpler theoretical models with only one argument
  • 40. Function Composition ● Compose: f(g(x)) ● andThen: g(f(x))
  • 41. Polymorphic Methods Method dup is parameterized with type T and with the value parameters x: T and n: Int. In the first call to dup, the programmer provides the required parameters, but as the following line shows, the programmer is not required to give actual type parameters explicitly. The type system of Scala can infer such types. This is done by looking at the types of the given value parameters and at the context where the method is called.
  • 42. Passing Parameter ● a, b, cond, func are called By-value parameters
  • 43. By-name Parameter ● Only function body got passed
  • 44. By-name Parameter ● Only function body got passed
  • 45. By-name Parameter ● Only function body got passed ● By-name parameter is NOT a function object, just a name, cannot be called
  • 46. Syntax Abstraction ● Function abstraction ○ lambda calculus provide flexibility to define function/formula, control flow, etc. ● Syntactic abstraction ○ Build its own syntax → Domain specific language By-name parameter + Curry: Unless and until seems to be a syntax
  • 47. Recap ● Functional, expression-oriented language ○ Expression ○ First-class function ● Lambda calculus ○ Anonymous function ○ Closure ○ Simple semantic ● World view: OOP and FP ● Abstraction ○ Function ■ Partially applied function ■ Currying ■ Function composition ■ Polymorphic methods ○ Syntax ■ By-value, by-name parameter
  • 50. Reference ● Codes are from ○ http://www.scala-lang.org/docu/files/ScalaByExample.pdf chapter 2 ○ https://openhome.cc/Gossip/Scala/index.html ○ http://alvinalexander.com/ ● Wiki pages
  • 52. Infix and dot ● Infix can be cumbersome http://stackoverflow.com/questions/10233227/scala-infix-vs-dot-notation ● Scala style guide (method invocation) Infix > dot, when... ○ Suffix Notation: names.toList ○ Arity-1 ○ Higher-Order Functions ○ ○ Symbolic methods/Operators: