Upcoming SlideShare
×

# 2013 28-03-dak-why-fp

207 views

Published on

My speech on Why functional programming.

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
207
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
4
0
Likes
0
Embeds 0
No embeds

No notes for slide

### 2013 28-03-dak-why-fp

1. 1. Почему функциональноепрограммирование? Сборная солянка
2. 2. def sum(list): result = 0 for element in list: result = result + element return resultsum [] = 0sum (x:xs) = x + sum xs
3. 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. 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. 5. sum(L) -> lists:foldl(fun(X, Sum) -> X + Sum end, 0, L).
6. 6. ПараллелизмПрограмма выполняетсябыстрее в условияхмногоядерного окруженияКонкурентностьНеизбежноевзаимодействиеконкурентных потоков впредметной области
7. 7. var x = 0;setTimeout(function () { x = x + 1; }, 0);setTimeout(function () { x = x * 2; }, 0);console.log(x);
8. 8. \$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")
9. 9. ???val people: Array[Person]val (men, women) = people.par partition (_.gender == "male")
10. 10. ???val people: Array[Person]val totalMoney = people.par map (_.money) reduce (_+_)
11. 11. actor { receive { case people: Set[Person] => val (men, women) = people partition (_.gender == "male") Cosmopolitan ! women Playboy ! men }}
12. 12. 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
13. 13. 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! endend
14. 14. 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! endend
15. 15. 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! endend