ºÝºÝߣ

ºÝºÝߣShare 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).
2013 28-03-dak-why-fp
§±§Ñ§â§Ñ§Ý§Ý§Ö§Ý§Ú§Ù§Þ
§±§â§à§Ô§â§Ñ§Þ§Þ§Ñ §Ó§í§á§à§Ý§ß§ñ§Ö§ä§ã§ñ
§Ò§í§ã§ä§â§Ö§Ö §Ó §å§ã§Ý§à§Ó§Ú§ñ§ç
§Þ§ß§à§Ô§à§ñ§Õ§Ö§â§ß§à§Ô§à §à§Ü§â§å§Ø§Ö§ß§Ú§ñ




§¬§à§ß§Ü§å§â§Ö§ß§ä§ß§à§ã§ä§î
§¯§Ö§Ú§Ù§Ò§Ö§Ø§ß§à§Ö
§Ó§Ù§Ñ§Ú§Þ§à§Õ§Ö§Û§ã§ä§Ó§Ú§Ö
§Ü§à§ß§Ü§å§â§Ö§ß§ä§ß§í§ç §á§à§ä§à§Ü§à§Ó §Ó
§á§â§Ö§Õ§Þ§Ö§ä§ß§à§Û §à§Ò§Ý§Ñ§ã§ä§Ú
var x = 0;

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

console.log(x);
2013 28-03-dak-why-fp
$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

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).
  • 7. §±§Ñ§â§Ñ§Ý§Ý§Ö§Ý§Ú§Ù§Þ §±§â§à§Ô§â§Ñ§Þ§Þ§Ñ §Ó§í§á§à§Ý§ß§ñ§Ö§ä§ã§ñ §Ò§í§ã§ä§â§Ö§Ö §Ó §å§ã§Ý§à§Ó§Ú§ñ§ç §Þ§ß§à§Ô§à§ñ§Õ§Ö§â§ß§à§Ô§à §à§Ü§â§å§Ø§Ö§ß§Ú§ñ §¬§à§ß§Ü§å§â§Ö§ß§ä§ß§à§ã§ä§î §¯§Ö§Ú§Ù§Ò§Ö§Ø§ß§à§Ö §Ó§Ù§Ñ§Ú§Þ§à§Õ§Ö§Û§ã§ä§Ó§Ú§Ö §Ü§à§ß§Ü§å§â§Ö§ß§ä§ß§í§ç §á§à§ä§à§Ü§à§Ó §Ó §á§â§Ö§Õ§Þ§Ö§ä§ß§à§Û §à§Ò§Ý§Ñ§ã§ä§Ú
  • 8. var x = 0; setTimeout(function () { x = x + 1; }, 0); setTimeout(function () { x = x * 2; }, 0); console.log(x);
  • 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