Haskell was made by some really smart guys (with PhDs) (...) when a committee of researchers got together to design a kick-ass language.
- Miran Lipovaca "Learn You a Haskell for Great Good!: A Beginner's Guide" (No Starch Press, 2011)
Haskell was made by some really smart guys (with PhDs) (...) when a committee of researchers got together to design a kick-ass language.
class ArticlesController < ApplicationController | |
# GET /articles.json | |
# GET /articles.xml | |
def index | |
@articles = Article.all | |
respond_to do |format| | |
format.json { render json: @articles } | |
format.xml { render xml: @articles } | |
end | |
end | |
end |
class ArticlesController < ApplicationController | |
respond_to :json, :xml | |
# GET /articles.json | |
# GET /articlel.xml | |
def index | |
respond_with @articles = Article.all | |
end | |
end |
# GET /articles.json | |
# GET /articles.xml | |
def index | |
@articles = Article.all | |
render json: @articles | |
end |
class ApplicationResponder < ActionController::Responder | |
def api_behavior(error) | |
raise error unless resourceful? | |
if get? | |
display resource | |
elsif has_errors? | |
display resource.errors, :status => :unprocessable_entity | |
elsif post? | |
display resource, :status => :created, :location => api_location | |
elsif put? | |
display resource, status: :ok, location: api_location | |
elsif has_empty_resource_definition? | |
display empty_resource, :status => :ok | |
else | |
display head(:ok) | |
end | |
end | |
end |
require 'application_responder' | |
class ApplicationController < ActionController::Base | |
protect_from_forgery | |
self.responder = ApplicationResponder | |
end |
# PUT /articles/1.json | |
# PUT /articles/1.xml | |
def update | |
article = Article.find(params[:id]) | |
article.update_attributes(params[:article]) | |
respond_with article | |
end |
A rational number is a number that can be expressed as a ratio n/d , where `n` and `d` are integers, except that `d` cannot be zero. `n` is called the numerator and `d` the denominator. Examples of rational numbers are 1/2 , 2/3 , 112/239 , and 2/1 . Compared to floating-point numbers, rational numbers have the advantage that fractions are represented exactly, without rounding or approximation.
The class we’ll design in this chapter must model the behavior of rational numbers, including allowing them to be added, subtracted, multiplied, and divided. To add two rationals, you must first obtain a common denominator, then add the two numerators. For example, to add 1/2 + 2/3 , you multiply both parts of the left operand by 3 and both parts of the right operand by 2, which gives you 3/6 + 4/6 . Adding the two numerators yields the result, 7/6 . To multiply two rational numbers, you can simply multiply their numerators and multiply their denominators. Thus, 1/2 ∗ 2/5 gives 2/10 , which can be represented more compactly in its "normalized" form as 1/5 . You divide by swapping the numerator and denominator of the right operand and then multiplying. For instance 1/2 / 3/5 is the same as 1/2 ∗ 5/3 , or 5/6 .
from piston.handler import BaseHandler | |
from piston.utils import rc | |
class DummyBaseHandler(BaseHandler): | |
''' | |
`DummyBaseHandler` | |
''' | |
model = None | |
def create(self, request, *args, **kwargs): | |
''' | |
Almost copy of `piston.handler.BaseHandler.create()` | |
It gets one more attribute: `attrs` (dict) | |
`attrs` contains fields for model. If it's not given simple `request.POST` is used. | |
''' | |
if not self.has_model(): | |
return rc.NOT_IMPLEMENTED | |
attrs = self.flatten_dict(request.POST) | |
# my changes [start] | |
if 'attrs' in kwargs: | |
attrs.update(kwargs['attrs']) | |
# my changes [end] | |
try: | |
inst = self.model.objects.get(**attrs) | |
return rc.DUPLICATE_ENTRY | |
except self.model.DoesNotExist: | |
inst = self.model(**attrs) | |
inst.save() | |
return inst | |
except self.model.MultipleObjectsReturned: | |
return rc.DUPLICATE_ENTRY | |
class BaseHandler(DummyBaseHandler): pass |
from piston.utils import validate | |
from libs.my_piston.handler import BaseHandler | |
from apps.task.models import Task | |
from apps.task.forms import TaskForm | |
class TaskBaseHandler(BaseHandler): | |
model = Task | |
class TaskHandler(TaskBaseHandler): | |
allowed_methods = ('POST',) | |
@validate(TaskForm) | |
def create(self, request, *args, **kwargs): | |
attrs = { | |
'user': request.user, | |
} | |
return super(TaskHandler, self).create(request, *args, attrs=attrs) |