SlideShare a Scribd company logo
Introductory Functional
           Programming

16.01.2013
Valera Rozuvan
We all know what programming is


                  #include <stdio.h>


                  int main(void) {


                    printf("Hello 
                    world!n");
                    return 0;


                  }
Functional?
A function is a rule
which operates on an
input and produces an
output.




… programming ?
Wait... isn't this
     #include <stdio.h>


     int main(void) {
       printf("Hello world!n");
       return 0;
     }



already functional programming? It is after all a
  program that defines a function main(), which
  is then executed ...
Wrong!
In computer science, functional
  programming is a programming
  paradigm that treats computation
  as the evaluation of mathematical
  functions and avoids state and
  mutable data.




    … just having functions isn't enough ...
procedural
                            FORTRAN, C, Pascal, BASIC
              imperative
                            object-oriented
                            C++, Java, C#, VB.NET, Python
programming
                            logic
                            Gödel, PROLOG
              declarative
                            functional
                            Lisp, Clojure, Erlang, Haskell
Imperative vs. Declarative
●
    Imperative programming is a programming
    paradigm that describes computation in
    terms of statements that change a program
    state.    Imperative    programs    define
    sequences of commands for the computer
    to perform.
●
    Declarative programming is a programming
    paradigm that expresses the logic of a
    computation without describing its control
    flow.
Huh?
Example: factorial
Imperative:             Declarative:
 def factorial(x)        def factorial(x)
   res = 1                 if x < 2
   while (x > 1)             return 1
     res = res*x           else
     x = x­1                 return x*factorial(x­1)
   end                     end
   return res            end
 end
                         print(factorial(10))
 x = 10
 print(factorial(x))
Functional programming languages
          are declarative
… in the beginning there was mathematics ...
Lambda calculus
●
    The λ-calculus calculus was introduced by
    mathematician Alonzo Church in the 1930s.
●
    The λ-calculus treats functions "anonymously",
    without giving them explicit names.
●
    In λ-calculus, functions are taken to be 'first
    class values', so functions may be used as the
    inputs and returned as outputs from other
    functions.
●
    λ-calculus – a formal system for function
    definition, application, and recursion.
Introduction Functional Programming - Tech Hangout #11 - 2013.01.16
Wow...




         … hard.
Lambda calculus For Dummies
Applying FP to the *real* world?
●
    I am a JavaScript person, so I will write JS!




                                                   you
                                                  have
                                                  been
                                                warned
Can you do FP in JS?
●
    If you define functional language as the
    language that supports first class functions
    and lambdas, then yes, JavaScript *is* a
    functional language.
●
    JavaScript supports passing around functions
    as variables.
●
    JavaScript supports anonymous functions.


                          Short answer – yes! If you
                          start to argue – no.
A short demo
●
    If you have any good taste at all, one ugly
    detail must be starting to bother you - the
    endlessly repeated for loop going over an
    array.


       function printArray(array) {
         for (var i = 0; i < array.length; i++)
           print(array[i]);
       }
●
    But what if we want to do something other
    than print? Because 'doing something' is really
    a function, and functions are also values, we
    can pass our action as a function value.


       function forEach(array, action) {
         for (var i = 0; i < array.length; i++)
           action(array[i]);
       }

       forEach(
         ["Wampeter", "Foma", "Granfalloon"],
         print
       );
●
    And by making use of an anonymous function,
    something just like a for loop can be written
    with less useless details.


       function sum(numbers) {
         var total = 0;

         forEach(numbers, function (number) {
           total += number;
         });

         return total;
       }
       show(sum[1, 10, 100]);
●
    On the whole, using more abstract (or 'higher
    level') constructs results in more information
    and less noise. Compare the following:

       var paragraphs = archive[today].split("n");
       for (var i = 0; i < paragraphs.length; i++)
           processParagraph(paragraphs[i]);


versus

       forEach(
         archive[today].split("n"),
         ProcessParagraph
       );
Use functional programming
for the greater good!
questions

More Related Content

What's hot (19)

PDF
Functional programing in Javascript (lite intro)
Nikos Kalogridis
 
PDF
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
PDF
Scala qq
羽祈 張
 
PPTX
5.functions
Hardik gupta
 
PDF
Functional go
Geison Goes
 
PPT
ALGOL ailesi programlama dilleri
Cumhuriyet Üniversitesi
 
PDF
Introduction to functional programming
Konrad Szydlo
 
PPTX
Interpreter Design Pattern in Javascript
Dmytro Verbovyi
 
PPT
Scala functions
Knoldus Inc.
 
PDF
Thinking in Functions: Functional Programming in Python
Anoop Thomas Mathew
 
PDF
Lecture 3 getting_started_with__c_
eShikshak
 
PDF
Type Checking JavaScript
Pascal-Louis Perez
 
PPTX
An Introduction to Functional Programming with Javascript
Doug Sparling
 
PPTX
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
PPTX
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
reactima
 
PDF
Functional programming in scala
Stratio
 
PPTX
Storage Class in C Progrmming
Kamal Acharya
 
PDF
Pointers & functions
Manjitsing Valvi
 
PPTX
Developer’s viewpoint on swift programming language
Azilen Technologies Pvt. Ltd.
 
Functional programing in Javascript (lite intro)
Nikos Kalogridis
 
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
Scala qq
羽祈 張
 
5.functions
Hardik gupta
 
Functional go
Geison Goes
 
ALGOL ailesi programlama dilleri
Cumhuriyet Üniversitesi
 
Introduction to functional programming
Konrad Szydlo
 
Interpreter Design Pattern in Javascript
Dmytro Verbovyi
 
Scala functions
Knoldus Inc.
 
Thinking in Functions: Functional Programming in Python
Anoop Thomas Mathew
 
Lecture 3 getting_started_with__c_
eShikshak
 
Type Checking JavaScript
Pascal-Louis Perez
 
An Introduction to Functional Programming with Javascript
Doug Sparling
 
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
reactima
 
Functional programming in scala
Stratio
 
Storage Class in C Progrmming
Kamal Acharya
 
Pointers & functions
Manjitsing Valvi
 
Developer’s viewpoint on swift programming language
Azilen Technologies Pvt. Ltd.
 

Similar to Introduction Functional Programming - Tech Hangout #11 - 2013.01.16 (20)

PDF
Functional Programming #FTW
Adriano Bonat
 
PPTX
Introduction to Functional Programming
Dave Fancher
 
PDF
Introduction to functional programming (In Arabic)
Omar Abdelhafith
 
PDF
Functional programming
ijcd
 
PPTX
Intro f# functional_programming
Mauro Ghiani
 
PPTX
Functional reactive programming
Hung Hoang
 
PPTX
Functional Programming.pptx
KarthickT28
 
PPTX
Functional Programming Fundamentals
OleksiyTereshchenko
 
PPTX
Unraveling the mystery of monads
Faisal Waris
 
PPTX
Functional Programming Concepts for Imperative Programmers
Chris
 
PDF
JavaScript for ABAP Programmers - 7/7 Functional Programming
Chris Whealy
 
PPTX
Plc part 3
Taymoor Nazmy
 
KEY
Scala: functional programming for the imperative mind
Sander Mak (@Sander_Mak)
 
PPTX
Functional Programming in Swift
Saugat Gautam
 
PPTX
Functional Programming Fundamentals
Shahriar Hyder
 
PPTX
The joy of functional programming
Steve Zhang
 
KEY
Functional programming in clojure
Juan-Manuel Gimeno
 
PDF
Functional Programming for Busy Object Oriented Programmers
Diego Freniche Brito
 
PPS
Presentation of GetTogether on Functional Programming
Filip De Sutter
 
PDF
Functional programming in Python 1st Edition David Mertz
nkossivilana87
 
Functional Programming #FTW
Adriano Bonat
 
Introduction to Functional Programming
Dave Fancher
 
Introduction to functional programming (In Arabic)
Omar Abdelhafith
 
Functional programming
ijcd
 
Intro f# functional_programming
Mauro Ghiani
 
Functional reactive programming
Hung Hoang
 
Functional Programming.pptx
KarthickT28
 
Functional Programming Fundamentals
OleksiyTereshchenko
 
Unraveling the mystery of monads
Faisal Waris
 
Functional Programming Concepts for Imperative Programmers
Chris
 
JavaScript for ABAP Programmers - 7/7 Functional Programming
Chris Whealy
 
Plc part 3
Taymoor Nazmy
 
Scala: functional programming for the imperative mind
Sander Mak (@Sander_Mak)
 
Functional Programming in Swift
Saugat Gautam
 
Functional Programming Fundamentals
Shahriar Hyder
 
The joy of functional programming
Steve Zhang
 
Functional programming in clojure
Juan-Manuel Gimeno
 
Functional Programming for Busy Object Oriented Programmers
Diego Freniche Brito
 
Presentation of GetTogether on Functional Programming
Filip De Sutter
 
Functional programming in Python 1st Edition David Mertz
nkossivilana87
 
Ad

More from Innovecs (20)

PDF
Building Efficient and High Performing iLottery Solutions
Innovecs
 
PDF
Innovecs Meetup Lifestory
Innovecs
 
PPTX
Подходы и технологии в React Redux
Innovecs
 
PPTX
Redux vs RxJS vs Mobx в связке с React
Innovecs
 
PDF
React & Redux (Lazarev)
Innovecs
 
PDF
Web Platform for Fashion Shop
Innovecs
 
PDF
Programmatic Advertising Platform
Innovecs
 
PDF
Multimedia Newsroom
Innovecs
 
PDF
Media Buying Platform (DSP+DPM)
Innovecs
 
PDF
Web-based Shipment Application
Innovecs
 
PDF
Digital Trading Platform
Innovecs
 
PDF
Mobile Insurance Agent
Innovecs
 
PDF
Online Learning Platform
Innovecs
 
PDF
Client Bank
Innovecs
 
PDF
Fertility Tracking App
Innovecs
 
PDF
Warranty Wallet App
Innovecs
 
PDF
Online Bingo Game
Innovecs
 
PDF
Secure Messenger
Innovecs
 
PDF
Search Data Platform
Innovecs
 
PDF
Website Builder for Insurance Agents
Innovecs
 
Building Efficient and High Performing iLottery Solutions
Innovecs
 
Innovecs Meetup Lifestory
Innovecs
 
Подходы и технологии в React Redux
Innovecs
 
Redux vs RxJS vs Mobx в связке с React
Innovecs
 
React & Redux (Lazarev)
Innovecs
 
Web Platform for Fashion Shop
Innovecs
 
Programmatic Advertising Platform
Innovecs
 
Multimedia Newsroom
Innovecs
 
Media Buying Platform (DSP+DPM)
Innovecs
 
Web-based Shipment Application
Innovecs
 
Digital Trading Platform
Innovecs
 
Mobile Insurance Agent
Innovecs
 
Online Learning Platform
Innovecs
 
Client Bank
Innovecs
 
Fertility Tracking App
Innovecs
 
Warranty Wallet App
Innovecs
 
Online Bingo Game
Innovecs
 
Secure Messenger
Innovecs
 
Search Data Platform
Innovecs
 
Website Builder for Insurance Agents
Innovecs
 
Ad

Recently uploaded (20)

PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
infertility, types,causes, impact, and management
Ritu480198
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
epi editorial commitee meeting presentation
MIPLM
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
Introduction to Indian Writing in English
Trushali Dodiya
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 

Introduction Functional Programming - Tech Hangout #11 - 2013.01.16

  • 1. Introductory Functional Programming 16.01.2013 Valera Rozuvan
  • 2. We all know what programming is #include <stdio.h> int main(void) {   printf("Hello  world!n");   return 0; }
  • 3. Functional? A function is a rule which operates on an input and produces an output. … programming ?
  • 4. Wait... isn't this #include <stdio.h> int main(void) {   printf("Hello world!n");   return 0; } already functional programming? It is after all a program that defines a function main(), which is then executed ...
  • 6. In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. … just having functions isn't enough ...
  • 7. procedural FORTRAN, C, Pascal, BASIC imperative object-oriented C++, Java, C#, VB.NET, Python programming logic Gödel, PROLOG declarative functional Lisp, Clojure, Erlang, Haskell
  • 8. Imperative vs. Declarative ● Imperative programming is a programming paradigm that describes computation in terms of statements that change a program state. Imperative programs define sequences of commands for the computer to perform. ● Declarative programming is a programming paradigm that expresses the logic of a computation without describing its control flow.
  • 10. Example: factorial Imperative: Declarative: def factorial(x) def factorial(x)   res = 1   if x < 2   while (x > 1)     return 1     res = res*x   else     x = x­1     return x*factorial(x­1)   end   end   return res end end print(factorial(10)) x = 10 print(factorial(x))
  • 12. … in the beginning there was mathematics ...
  • 13. Lambda calculus ● The λ-calculus calculus was introduced by mathematician Alonzo Church in the 1930s. ● The λ-calculus treats functions "anonymously", without giving them explicit names. ● In λ-calculus, functions are taken to be 'first class values', so functions may be used as the inputs and returned as outputs from other functions. ● λ-calculus – a formal system for function definition, application, and recursion.
  • 15. Wow... … hard.
  • 17. Applying FP to the *real* world? ● I am a JavaScript person, so I will write JS! you have been warned
  • 18. Can you do FP in JS? ● If you define functional language as the language that supports first class functions and lambdas, then yes, JavaScript *is* a functional language. ● JavaScript supports passing around functions as variables. ● JavaScript supports anonymous functions. Short answer – yes! If you start to argue – no.
  • 19. A short demo ● If you have any good taste at all, one ugly detail must be starting to bother you - the endlessly repeated for loop going over an array. function printArray(array) {   for (var i = 0; i < array.length; i++)     print(array[i]); }
  • 20. But what if we want to do something other than print? Because 'doing something' is really a function, and functions are also values, we can pass our action as a function value. function forEach(array, action) {   for (var i = 0; i < array.length; i++)     action(array[i]); } forEach(   ["Wampeter", "Foma", "Granfalloon"],   print );
  • 21. And by making use of an anonymous function, something just like a for loop can be written with less useless details. function sum(numbers) {   var total = 0;   forEach(numbers, function (number) {     total += number;   });   return total; } show(sum[1, 10, 100]);
  • 22. On the whole, using more abstract (or 'higher level') constructs results in more information and less noise. Compare the following: var paragraphs = archive[today].split("n"); for (var i = 0; i < paragraphs.length; i++)     processParagraph(paragraphs[i]); versus forEach(   archive[today].split("n"),   ProcessParagraph );
  • 23. Use functional programming for the greater good!