Functional Programming
CSEC SEMINAR
What a seminar (at least mine) isn’t …
 A lecture
 A tutorial
 A
What a seminar (at least mine) is about
 Sharing ‘Aha’ moments
 Building a vibrant community
About Me
 A CEO
 A web application developer
 An expert in Functional programming.
What is FP?
 A programming paradigm
 A coding style
 A mindset
 A hype
 Closures
 Currying
 Higher-Order-Functions
 Side-effects
 Lambda expressions
 Reduce , filter, map
BUZZ WORDS
What FP is not …
 A procedural programming
 All about creating functions and using them
Thinking
Functional
CONCEPTS
Basic FP Concepts
 Functions as a first-class-citizens
 Immutable values over mutable variables
 Currying, partial application of functions
 Laziness, deferred execution
 Avoiding side-effects
 DRY
Thinking Functional
”
“A first-class citizen : an entity which supports
all the operations generally available to other
entities. These operations typically include
being passed as an argument, returned from
a function, and assigned to a variable.
SCOTT, MICHAEL (2006). PROGRAMMING LANGUAGE PRAGMATICS
Demo
Thinking Functional
Side-effects
 a function or expression is said to have a side effect if it modifies some
state or has an observable interaction with calling functions or the
outside world.
 For example, a particular function might modify a global variable or
static variable, modify one of its arguments, raise an exception, write
data to a display or file, read data, or call other side-effecting functions.
 In the presence of side effects, a program's behaviour may depend on
history; that is, the order of evaluation matters. Understanding and
debugging a function with side effects requires knowledge about the
context and its possible histories.
Thinking Functional
Pure vs Impure
 The function always evaluates the same result value given the same argument
value(s). The function result value cannot depend on any hidden information or
state that may change while program execution proceeds or between different
executions of the program, nor can it depend on any external input from I/O
devices.
 Evaluation of the result does not cause any semantically observable side effect or
output, such as mutation of mutable objects or output to I/O devices.
 Pure: sin(x)
 Impure: random()
Code reuse (DRY)
 demo
Don’t iterate MAP, REDUCE, FILTER
Declarative programming
 Declarative programming: A style of building the structure and
elements of computer programs — that expresses the logic of a
computation without describing its control flow.
 E.g: Database query languages (e.g., SQL), regular expressions,
logic programming (e.g., Prolog), functional programming.
Iteration
TheArray<String>[] = { “GoT”,”BrBa”,”Zemen”};
for(int i=0;i<TheArray.size();i++){
System.out.println(“I’ve watched ”+TheArray[i]);
}
Map, reduce , filter, lambda
Demo
Implementing
Languages
Pure
 Haskell
 Curry
Not-pure
 Clojure
 F#
 Scala
 JavaScript
 Java8
Excuses
 This is my very first seminar.
 Internet

Functional programming

  • 1.
  • 2.
    What a seminar(at least mine) isn’t …  A lecture  A tutorial  A
  • 3.
    What a seminar(at least mine) is about  Sharing ‘Aha’ moments  Building a vibrant community
  • 4.
    About Me  ACEO  A web application developer  An expert in Functional programming.
  • 5.
    What is FP? A programming paradigm  A coding style  A mindset  A hype
  • 6.
     Closures  Currying Higher-Order-Functions  Side-effects  Lambda expressions  Reduce , filter, map BUZZ WORDS
  • 7.
    What FP isnot …  A procedural programming  All about creating functions and using them
  • 8.
  • 9.
    Basic FP Concepts Functions as a first-class-citizens  Immutable values over mutable variables  Currying, partial application of functions  Laziness, deferred execution  Avoiding side-effects  DRY Thinking Functional
  • 10.
    ” “A first-class citizen: an entity which supports all the operations generally available to other entities. These operations typically include being passed as an argument, returned from a function, and assigned to a variable. SCOTT, MICHAEL (2006). PROGRAMMING LANGUAGE PRAGMATICS Demo Thinking Functional
  • 11.
    Side-effects  a functionor expression is said to have a side effect if it modifies some state or has an observable interaction with calling functions or the outside world.  For example, a particular function might modify a global variable or static variable, modify one of its arguments, raise an exception, write data to a display or file, read data, or call other side-effecting functions.  In the presence of side effects, a program's behaviour may depend on history; that is, the order of evaluation matters. Understanding and debugging a function with side effects requires knowledge about the context and its possible histories. Thinking Functional
  • 12.
    Pure vs Impure The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change while program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices.  Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices.  Pure: sin(x)  Impure: random()
  • 13.
  • 14.
    Don’t iterate MAP,REDUCE, FILTER
  • 15.
    Declarative programming  Declarativeprogramming: A style of building the structure and elements of computer programs — that expresses the logic of a computation without describing its control flow.  E.g: Database query languages (e.g., SQL), regular expressions, logic programming (e.g., Prolog), functional programming.
  • 16.
    Iteration TheArray<String>[] = {“GoT”,”BrBa”,”Zemen”}; for(int i=0;i<TheArray.size();i++){ System.out.println(“I’ve watched ”+TheArray[i]); }
  • 17.
    Map, reduce ,filter, lambda Demo
  • 18.
  • 19.
    Languages Pure  Haskell  Curry Not-pure Clojure  F#  Scala  JavaScript  Java8
  • 20.
    Excuses  This ismy very first seminar.  Internet