The document discusses various approaches to programming including functional programming, parallelism, concurrency, and refactoring object-oriented code. It provides examples of summing a list functionally and with recursion, as well as examples of sorting arrays, partitioning data into groups, and managing orders with an exposed model and authorization checks.
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))
}
}
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