SlideShare a Scribd company logo
Почему
 функциональное
программирование?
    Сборная солянка
def sum(list):
 result = 0
 for element in list:
  result = result + element
 return result



sum' [] = 0
sum' (x:xs) = x + sum' xs
public static <E extends Comparable<? super E>> List<E> quickSort(List<E> arr) {
  if (arr.size() <= 1)
      return arr;
  E pivot = arr.getFirst();

    List<E> less = new LinkedList<E>();
    List<E> pivotList = new LinkedList<E>();
    List<E> more = new LinkedList<E>();

    for (E i: arr) {
      if (i.compareTo(pivot) < 0)
          less.add(i);
      else if (i.compareTo(pivot) > 0)
          more.add(i);
      else
          pivotList.add(i);
    }

    less = quickSort(less);
    more = quickSort(more);

    less.addAll(pivotList);
    less.addAll(more);
    return less;
}
def qsort[T <% Ordered[T]](list:List[T]):List[T] = {
  list match {
  case Nil => Nil
  case x::xs =>
    val (before,after) = xs partition (_ < x)
    qsort(before) ++ (x :: qsort(after))
  }
}
sum(L) ->
 lists:foldl(fun(X, Sum) -> X + Sum end, 0, L).
Параллелизм
Программа выполняется
быстрее в условиях
многоядерного окружения




Конкурентность
Неизбежное
взаимодействие
конкурентных потоков в
предметной области
var x = 0;

setTimeout(function () { x = x + 1; }, 0);
setTimeout(function () { x = x * 2; }, 0);

console.log(x);
$people = array();
$men = array();
$women = array();

foreach ($people as $person) {
  array_push(($person->gender == 'male' ? $men : $women), $person)
}




val people: Array[Person]
val (men, women) = people partition (_.gender == "male")
???
val people: Array[Person]
val (men, women) = people.par partition (_.gender == "male")
???
val people: Array[Person]
val totalMoney = people.par map (_.money) reduce (_+_)
actor {
 receive {
   case people: Set[Person] =>
    val (men, women) = people partition (_.gender == "male")
    Cosmopolitan ! women
    Playboy        ! men
 }
}
class OrderController < ApplicationController
  before_filter :authenticate_user!
  before_filter :load_order, only: [:edit, :update, :destroy]
  before_filter :create_order_from_params, only: [:new, :create]
  before_filter :authorize_user_on_order!, only: [:edit, :update, :destroy]
  before_filter :update_order_from_params, only: [:update]
  before_filter :render_if_save_error, only: [:create, :update]

  def new; end
  def edit; end

  def create
    redirect_to success_url
  end

  def update
    redirect_to @order
  end

  def destroy
    @order.destroy
    redirect_to success_url
  end

  def load_order
    @order = Order.find params[:id]
  end

  def create_order_from_params
    @order = Order.new params[:order]
  end

  def authorize_user_on_order!
    permission_denied! unless current_user.can_edit? @order
  end

  def update_order_from_params
    @order.update_attributes
  end

  def render_if_save_error
    unless @order.valid?
       render 'error' and return false
    end
  end
class OrderController < ApplicationController
  before_filter :authenticate_user!

  def new
    @order = order
  end

  def create
    @order = refreshed_order
    @order.save
    respond_with(@order)
  end

  def edit
    @order = order
  end

  def update
    @order = refreshed_order
    @order.save
    respond_with(@order)
  end

  def destroy
    @order = order
    @order.destroy
    respond_with(@order)
  end

  private

  def order
     params[:id] ? ensure_access! Order.find(params[:id]) : Order.new
  end

  def resfreshed_order
    order.tap { |o| o.attributes = params[:order] }
  end

  def ensure_access!(order)
    current_user.can_edit?(order) ? order : permission_denied!
  end
end
class OrderController < ApplicationController
  before_filter :authenticate_user!
  helper_method :order

  def new; end
  def edit; end

  def create
    order.save
    respond_with(order)
  end

  def update
    order.save
    respond_with(order)
  end

  def destroy
    order.destroy
    respond_with(order)
  end

  private

  def order
    @_order ||= refresh(params[:id] ? ensure_access! Order.find(params[:id]) : Order.new)
  end

  def resfresh(order)
    order.tap { |o| o.attributes = params[:order] if params[:order] }
  end

  def ensure_access!(order)
    current_user.can_edit?(order) ? order : permission_denied!
  end
end
class OrderController < ApplicationController
  before_filter :authenticate_user!

  def new; end
  def edit; end

  def create
    order.save
    respond_with(order)
  end

  def update
    order.save
    respond_with(order)
  end

  def destroy
    order.destroy
    respond_with(order)
  end

  expose(:order) {
     refresh(params[:id] ? ensure_access! Order.find(params[:id]) : Order.new)
  }

  private

  def resfresh(order)
    order.tap { |o| o.attributes = params[:order] if params[:order] }
  end

  def ensure_access!(order)
    current_user.can_edit?(order) ? order : permission_denied!
  end
end
2013 28-03-dak-why-fp

More Related Content

What's hot

Perl6 grammars
Perl6 grammarsPerl6 grammars
Perl6 grammars
Andrew Shitov
 
The Magic Of Tie
The Magic Of TieThe Magic Of Tie
The Magic Of Tie
brian d foy
 
Perl6 one-liners
Perl6 one-linersPerl6 one-liners
Perl6 one-liners
Andrew Shitov
 
Clauses
ClausesClauses
DEV Čtvrtkon #76 - Fluent Interface
DEV Čtvrtkon #76 - Fluent InterfaceDEV Čtvrtkon #76 - Fluent Interface
DEV Čtvrtkon #76 - Fluent Interface
Ctvrtkoncz
 
R57shell
R57shellR57shell
R57shell
ady36
 
Inc
IncInc
Php tutorial handout
Php tutorial handoutPhp tutorial handout
Php tutorial handout
SBalan Balan
 
Closure, Higher-order function in Swift
Closure, Higher-order function in SwiftClosure, Higher-order function in Swift
Closure, Higher-order function in Swift
SeongGyu Jo
 
Creating a compiler in Perl 6
Creating a compiler in Perl 6Creating a compiler in Perl 6
Creating a compiler in Perl 6
Andrew Shitov
 
Python Ireland Nov 2010 Talk: Unit Testing
Python Ireland Nov 2010 Talk: Unit TestingPython Ireland Nov 2010 Talk: Unit Testing
Python Ireland Nov 2010 Talk: Unit Testing
Python Ireland
 
COMP2021 Final Project - LightHTML
COMP2021 Final Project - LightHTMLCOMP2021 Final Project - LightHTML
COMP2021 Final Project - LightHTML
Conrad Lo
 
Data Types Master
Data Types MasterData Types Master
Data Types Master
Paolo Marcatili
 
Business Rules with Brick
Business Rules with BrickBusiness Rules with Brick
Business Rules with Brick
brian d foy
 
Refactor like a boss
Refactor like a bossRefactor like a boss
Refactor like a boss
gsterndale
 
6 things about perl 6
6 things about perl 66 things about perl 6
6 things about perl 6
brian d foy
 
Slaying the Dragon: Implementing a Programming Language in Ruby
Slaying the Dragon: Implementing a Programming Language in RubySlaying the Dragon: Implementing a Programming Language in Ruby
Slaying the Dragon: Implementing a Programming Language in Ruby
Jason Yeo Jie Shun
 
wget.pl
wget.plwget.pl
Codigos
CodigosCodigos
Codigos
Manuel Valero
 
Session8
Session8Session8

What's hot (20)

Perl6 grammars
Perl6 grammarsPerl6 grammars
Perl6 grammars
 
The Magic Of Tie
The Magic Of TieThe Magic Of Tie
The Magic Of Tie
 
Perl6 one-liners
Perl6 one-linersPerl6 one-liners
Perl6 one-liners
 
Clauses
ClausesClauses
Clauses
 
DEV Čtvrtkon #76 - Fluent Interface
DEV Čtvrtkon #76 - Fluent InterfaceDEV Čtvrtkon #76 - Fluent Interface
DEV Čtvrtkon #76 - Fluent Interface
 
R57shell
R57shellR57shell
R57shell
 
Inc
IncInc
Inc
 
Php tutorial handout
Php tutorial handoutPhp tutorial handout
Php tutorial handout
 
Closure, Higher-order function in Swift
Closure, Higher-order function in SwiftClosure, Higher-order function in Swift
Closure, Higher-order function in Swift
 
Creating a compiler in Perl 6
Creating a compiler in Perl 6Creating a compiler in Perl 6
Creating a compiler in Perl 6
 
Python Ireland Nov 2010 Talk: Unit Testing
Python Ireland Nov 2010 Talk: Unit TestingPython Ireland Nov 2010 Talk: Unit Testing
Python Ireland Nov 2010 Talk: Unit Testing
 
COMP2021 Final Project - LightHTML
COMP2021 Final Project - LightHTMLCOMP2021 Final Project - LightHTML
COMP2021 Final Project - LightHTML
 
Data Types Master
Data Types MasterData Types Master
Data Types Master
 
Business Rules with Brick
Business Rules with BrickBusiness Rules with Brick
Business Rules with Brick
 
Refactor like a boss
Refactor like a bossRefactor like a boss
Refactor like a boss
 
6 things about perl 6
6 things about perl 66 things about perl 6
6 things about perl 6
 
Slaying the Dragon: Implementing a Programming Language in Ruby
Slaying the Dragon: Implementing a Programming Language in RubySlaying the Dragon: Implementing a Programming Language in Ruby
Slaying the Dragon: Implementing a Programming Language in Ruby
 
wget.pl
wget.plwget.pl
wget.pl
 
Codigos
CodigosCodigos
Codigos
 
Session8
Session8Session8
Session8
 

Viewers also liked

Chune PM Awards Presentation
Chune PM Awards PresentationChune PM Awards Presentation
Chune PM Awards Presentation
Kern Elliott
 
Функциональное реактивное программирование
Функциональное реактивное программированиеФункциональное реактивное программирование
Функциональное реактивное программирование
Dmitriy Kiriyenko
 
February 2014 column slideshare
February 2014 column slideshareFebruary 2014 column slideshare
February 2014 column slideshare
Sierra Circuits, Inc.
 
Marketing local music via digital technology
Marketing local music via digital technologyMarketing local music via digital technology
Marketing local music via digital technology
Kern Elliott
 
Rbrown
RbrownRbrown
Rbrown
deadcrown9
 
Блоки, лямбды, замыкания
Блоки, лямбды, замыканияБлоки, лямбды, замыкания
Блоки, лямбды, замыкания
Dmitriy Kiriyenko
 
Chune - PitchIT Caribbean 5 Minute Pitch Deck
Chune - PitchIT Caribbean 5 Minute Pitch DeckChune - PitchIT Caribbean 5 Minute Pitch Deck
Chune - PitchIT Caribbean 5 Minute Pitch Deck
Kern Elliott
 
15 korea introduction
15 korea introduction15 korea introduction
15 korea introduction
iancerep
 
PCBs for MICROELECTRONICS - Design for Manufacturability
PCBs for MICROELECTRONICS - Design for ManufacturabilityPCBs for MICROELECTRONICS - Design for Manufacturability
PCBs for MICROELECTRONICS - Design for Manufacturability
Sierra Circuits, Inc.
 
Bitcoin Data Pipeline - Insight Data Science project - September 2014
Bitcoin Data Pipeline - Insight Data Science project - September 2014Bitcoin Data Pipeline - Insight Data Science project - September 2014
Bitcoin Data Pipeline - Insight Data Science project - September 2014
Jean-Marc Soumet
 
стандарты
стандартыстандарты
Streaming Music in 2016
Streaming Music in 2016Streaming Music in 2016
Streaming Music in 2016
Kern Elliott
 

Viewers also liked (12)

Chune PM Awards Presentation
Chune PM Awards PresentationChune PM Awards Presentation
Chune PM Awards Presentation
 
Функциональное реактивное программирование
Функциональное реактивное программированиеФункциональное реактивное программирование
Функциональное реактивное программирование
 
February 2014 column slideshare
February 2014 column slideshareFebruary 2014 column slideshare
February 2014 column slideshare
 
Marketing local music via digital technology
Marketing local music via digital technologyMarketing local music via digital technology
Marketing local music via digital technology
 
Rbrown
RbrownRbrown
Rbrown
 
Блоки, лямбды, замыкания
Блоки, лямбды, замыканияБлоки, лямбды, замыкания
Блоки, лямбды, замыкания
 
Chune - PitchIT Caribbean 5 Minute Pitch Deck
Chune - PitchIT Caribbean 5 Minute Pitch DeckChune - PitchIT Caribbean 5 Minute Pitch Deck
Chune - PitchIT Caribbean 5 Minute Pitch Deck
 
15 korea introduction
15 korea introduction15 korea introduction
15 korea introduction
 
PCBs for MICROELECTRONICS - Design for Manufacturability
PCBs for MICROELECTRONICS - Design for ManufacturabilityPCBs for MICROELECTRONICS - Design for Manufacturability
PCBs for MICROELECTRONICS - Design for Manufacturability
 
Bitcoin Data Pipeline - Insight Data Science project - September 2014
Bitcoin Data Pipeline - Insight Data Science project - September 2014Bitcoin Data Pipeline - Insight Data Science project - September 2014
Bitcoin Data Pipeline - Insight Data Science project - September 2014
 
стандарты
стандартыстандарты
стандарты
 
Streaming Music in 2016
Streaming Music in 2016Streaming Music in 2016
Streaming Music in 2016
 

Similar to 2013 28-03-dak-why-fp

Beware: Sharp Tools
Beware: Sharp ToolsBeware: Sharp Tools
Beware: Sharp Tools
chrismdp
 
Decent exposure: Controladores sin @ivars
Decent exposure: Controladores sin @ivarsDecent exposure: Controladores sin @ivars
Decent exposure: Controladores sin @ivars
Leonardo Soto
 
Beware sharp tools
Beware sharp toolsBeware sharp tools
Beware sharp tools
AgileOnTheBeach
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128
PrinceGuru MS
 
An Elephant of a Different Colour: Hack
An Elephant of a Different Colour: HackAn Elephant of a Different Colour: Hack
An Elephant of a Different Colour: Hack
Vic Metcalfe
 
Zend Framework Study@Tokyo #2
Zend Framework Study@Tokyo #2Zend Framework Study@Tokyo #2
Zend Framework Study@Tokyo #2
Shinya Ohyanagi
 
WTF Oriented Programming, com Fabio Akita
WTF Oriented Programming, com Fabio AkitaWTF Oriented Programming, com Fabio Akita
WTF Oriented Programming, com Fabio Akita
iMasters
 
Php & my sql
Php & my sqlPhp & my sql
Php & my sql
Norhisyam Dasuki
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
Arturo Herrero
 
Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子
Yasuko Ohba
 
Python : Functions
Python : FunctionsPython : Functions
Funkcija, objekt, python
Funkcija, objekt, pythonFunkcija, objekt, python
Funkcija, objekt, python
Robert Lujo
 
Zend framework service
Zend framework serviceZend framework service
Zend framework service
Michelangelo van Dam
 
Zend framework service
Zend framework serviceZend framework service
Zend framework service
Michelangelo van Dam
 
php AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdfphp AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdf
SVN Polytechnic Kalan Sultanpur UP
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
Vineet Kumar Saini
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
AbhishekSharma2958
 
PHP and MySQL
PHP and MySQLPHP and MySQL
PHP and MySQL
Sanketkumar Biswas
 
TI1220 Lecture 8: Traits & Type Parameterization
TI1220 Lecture 8: Traits & Type ParameterizationTI1220 Lecture 8: Traits & Type Parameterization
TI1220 Lecture 8: Traits & Type Parameterization
Eelco Visser
 
Functional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioFunctional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis Atencio
Luis Atencio
 

Similar to 2013 28-03-dak-why-fp (20)

Beware: Sharp Tools
Beware: Sharp ToolsBeware: Sharp Tools
Beware: Sharp Tools
 
Decent exposure: Controladores sin @ivars
Decent exposure: Controladores sin @ivarsDecent exposure: Controladores sin @ivars
Decent exposure: Controladores sin @ivars
 
Beware sharp tools
Beware sharp toolsBeware sharp tools
Beware sharp tools
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128
 
An Elephant of a Different Colour: Hack
An Elephant of a Different Colour: HackAn Elephant of a Different Colour: Hack
An Elephant of a Different Colour: Hack
 
Zend Framework Study@Tokyo #2
Zend Framework Study@Tokyo #2Zend Framework Study@Tokyo #2
Zend Framework Study@Tokyo #2
 
WTF Oriented Programming, com Fabio Akita
WTF Oriented Programming, com Fabio AkitaWTF Oriented Programming, com Fabio Akita
WTF Oriented Programming, com Fabio Akita
 
Php & my sql
Php & my sqlPhp & my sql
Php & my sql
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
 
Funkcija, objekt, python
Funkcija, objekt, pythonFunkcija, objekt, python
Funkcija, objekt, python
 
Zend framework service
Zend framework serviceZend framework service
Zend framework service
 
Zend framework service
Zend framework serviceZend framework service
Zend framework service
 
php AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdfphp AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdf
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 
PHP and MySQL
PHP and MySQLPHP and MySQL
PHP and MySQL
 
TI1220 Lecture 8: Traits & Type Parameterization
TI1220 Lecture 8: Traits & Type ParameterizationTI1220 Lecture 8: Traits & Type Parameterization
TI1220 Lecture 8: Traits & Type Parameterization
 
Functional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioFunctional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis Atencio
 

2013 28-03-dak-why-fp

  • 2. def sum(list): result = 0 for element in list: result = result + element return result sum' [] = 0 sum' (x:xs) = x + sum' xs
  • 3. public static <E extends Comparable<? super E>> List<E> quickSort(List<E> arr) { if (arr.size() <= 1) return arr; E pivot = arr.getFirst(); List<E> less = new LinkedList<E>(); List<E> pivotList = new LinkedList<E>(); List<E> more = new LinkedList<E>(); for (E i: arr) { if (i.compareTo(pivot) < 0) less.add(i); else if (i.compareTo(pivot) > 0) more.add(i); else pivotList.add(i); } less = quickSort(less); more = quickSort(more); less.addAll(pivotList); less.addAll(more); return less; }
  • 4. def qsort[T <% Ordered[T]](list:List[T]):List[T] = { list match { case Nil => Nil case x::xs => val (before,after) = xs partition (_ < x) qsort(before) ++ (x :: qsort(after)) } }
  • 5. sum(L) -> lists:foldl(fun(X, Sum) -> X + Sum end, 0, L).
  • 6.
  • 7. Параллелизм Программа выполняется быстрее в условиях многоядерного окружения Конкурентность Неизбежное взаимодействие конкурентных потоков в предметной области
  • 8. var x = 0; setTimeout(function () { x = x + 1; }, 0); setTimeout(function () { x = x * 2; }, 0); console.log(x);
  • 9.
  • 10. $people = array(); $men = array(); $women = array(); foreach ($people as $person) { array_push(($person->gender == 'male' ? $men : $women), $person) } val people: Array[Person] val (men, women) = people partition (_.gender == "male")
  • 11. ??? val people: Array[Person] val (men, women) = people.par partition (_.gender == "male")
  • 12. ??? val people: Array[Person] val totalMoney = people.par map (_.money) reduce (_+_)
  • 13. actor { receive { case people: Set[Person] => val (men, women) = people partition (_.gender == "male") Cosmopolitan ! women Playboy ! men } }
  • 14. class OrderController < ApplicationController before_filter :authenticate_user! before_filter :load_order, only: [:edit, :update, :destroy] before_filter :create_order_from_params, only: [:new, :create] before_filter :authorize_user_on_order!, only: [:edit, :update, :destroy] before_filter :update_order_from_params, only: [:update] before_filter :render_if_save_error, only: [:create, :update] def new; end def edit; end def create redirect_to success_url end def update redirect_to @order end def destroy @order.destroy redirect_to success_url end def load_order @order = Order.find params[:id] end def create_order_from_params @order = Order.new params[:order] end def authorize_user_on_order! permission_denied! unless current_user.can_edit? @order end def update_order_from_params @order.update_attributes end def render_if_save_error unless @order.valid? render 'error' and return false end end
  • 15. class OrderController < ApplicationController before_filter :authenticate_user! def new @order = order end def create @order = refreshed_order @order.save respond_with(@order) end def edit @order = order end def update @order = refreshed_order @order.save respond_with(@order) end def destroy @order = order @order.destroy respond_with(@order) end private def order params[:id] ? ensure_access! Order.find(params[:id]) : Order.new end def resfreshed_order order.tap { |o| o.attributes = params[:order] } end def ensure_access!(order) current_user.can_edit?(order) ? order : permission_denied! end end
  • 16. class OrderController < ApplicationController before_filter :authenticate_user! helper_method :order def new; end def edit; end def create order.save respond_with(order) end def update order.save respond_with(order) end def destroy order.destroy respond_with(order) end private def order @_order ||= refresh(params[:id] ? ensure_access! Order.find(params[:id]) : Order.new) end def resfresh(order) order.tap { |o| o.attributes = params[:order] if params[:order] } end def ensure_access!(order) current_user.can_edit?(order) ? order : permission_denied! end end
  • 17. class OrderController < ApplicationController before_filter :authenticate_user! def new; end def edit; end def create order.save respond_with(order) end def update order.save respond_with(order) end def destroy order.destroy respond_with(order) end expose(:order) { refresh(params[:id] ? ensure_access! Order.find(params[:id]) : Order.new) } private def resfresh(order) order.tap { |o| o.attributes = params[:order] if params[:order] } end def ensure_access!(order) current_user.can_edit?(order) ? order : permission_denied! end end