Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
- PUT /admin/articles — создать статью.
- POST /admin/articles — обновить статью.
{deps, [ {rest, ".*", {git, "git://github.com/synrc/rest", "HEAD"}}]}
-module(users).
-behaviour(rest).
-compile({parse_transform, rest}).
-include("users.hrl").
-export(?REST_API).
-rest_record(user).
init() -> ets:new(users, [public, named_table, {keypos, #user.id}]).
populate(Users) -> ets:insert(users, Users).
exists(Id) -> ets:member(users, wf:to_list(Id)).
get() -> ets:tab2list(users).
get(Id) -> [User] = ets:lookup(users, wf:to_list(Id)), User.
delete(Id) -> ets:delete(users, wf:to_list(Id)).
post(#user{} = User) -> ets:insert(users, User);
post(Data) -> post(from_json(Data, #user{}))
curl -i -X POST -d "id=vlad" localhost:8000/rest/users
curl -i -X POST -d "id=doxtop" localhost:8000/rest/users
curl -i -X GET localhost:8000/rest/users
curl -i -X PUT -d "id=5HT" localhost:8000/rest/users/vlad
curl -i -X GET localhost:8000/rest/users/5HT
curl -i -X DELETE localhost:8000/rest/users/5HT
➜ rest cloc ./src
5 text files.
5 unique files.
1 file ignored.
http://cloc.sourceforge.net v 1.62 T=0.02 s (167.8 files/s, 11284.5 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Erlang 4 47 2 220
-------------------------------------------------------------------------------
SUM: 4 47 2 220
-------------------------------------------------------------------------------
-export(?REST_API).
-export([init/0, populate/1, exists/1, get/0, get/1, post/1, delete/1]).
-record(user, {id, name, email, proplist = [{facebook, udefined},
{github, "github.com/b0oh"},
{local, undefined},
{twitter, udefined}],
string = "common",
number = 12,
list_of_strings = ["one", "two", "three"],
list_of_numbers = [34958726345, 12],
nested_proplists = [{nested, [{number, 12},
{string, "common"},
{list_of_strings, ["one", "two", "three"]},
{list_of_atoms, [one, two, three]},
{list_of_numbers, [100000, 2,3 ]}]}]}).
stateless-сервиса очень удобно использовать Basic-аутентификацию
String — список обычных ASCII-символов, восьмибитных, естественно. Этот тип данных встроен в язык.
String = [Char]
-- sizeOf ('x' :: Char) == 4
ghc-options: -O3 -threaded -with-rtsopts=-N32
ghc-options: -O3 -threaded -rtsopts "-with-rtsopts=-N -A32m"
если вы знаете, что вам нужна ленивость в поле, т.е. это как-то используется алгоритмами — то не делайте его строгим, в противном случае — делайте.
data A = A Int
data B = B Int
data C = C ![Int]
mk constr conv = const . conv
Далее в интерпретаторе:
> let a = mk A id 7
> :sprint a
a = _ -- у нас thunk вместо значения
> (\x@(A _) -> True) a -- вычисляем до WHNF
True
> :sprint a
a = A _ -- конструктор вычислен в поле Thunk
> let b = mk B id 8
> :sprint b
b = _
> (\x@(B _) -> True) b
True
> :sprint b
b = B 8 -- конструктор вычислен и поле тоже вычислено
*Main> let c = mk C id (replicate 7 9)
*Main> (\x@(C _) -> True) c
True
*Main> :sprint c
c = C (9 : _) -- как видим поле вычислено только до WHNF
REST-сервер для простого блога на Haskell