Skip to content

Instantly share code, notes, and snippets.

@Heimdell
Created November 12, 2012 08:01
Show Gist options
  • Save Heimdell/4058067 to your computer and use it in GitHub Desktop.
Save Heimdell/4058067 to your computer and use it in GitHub Desktop.
Тезисы
Система типов: если есть нечто, что можно представить структурой и выразить через типы - do it.
> newtype End = End (Request -> Maybe Request)
> newtype Node = Node (Request -> Maybe Request)
> Node granny // Node mom = Node (mom <=< granny)
> Node mom /> End son = End (son <=< mom)
> End left \/ End right = End (left `orelse` right)
> orelse left right x = msum [left x, right x] -- first successful action
> rest entity =
> plate entity /> (method "GET" /> action "index"
> \/ method "GET" /> endpoint "new"
> \/ method "POST" /> action "create"
> \/ slug /> (root
> \/ method "GET" /> action "show"
> \/ method "GET" /> endpoint "edit"
> \/ method "PUT" /> action "update"
> \/ method "DELETE" /> action "destroy"))
>
> where action _ = root -- looks more clear
> endpoint point = plate point /> root
Скомпилировалось? Структура верна.
----
Необходимо менять структуру на ходу? Нужно добавить поиск, вложенные сущности?
> data RouterLinkage = Node `Then` Node
> | Node `Holds` End
> | End `Or` End
-- пропущена настройка приоритета конструкторов в форме операторов
> granny // mom = granny `Then` mom
> mom /> son = mom `Holds` son
> left \/ right = left `Or` right
> with ability (plate `Holds` actions) = plate /> ability
\/ actions
> nested son (plate `Holds` actions) = plate /> actions
\/ (slug /> son)
> x |> f = f x
> rest "users" |> with "search"
> |> nested
> (rest "projects"
> \/ rest "friends")
Функция rest и все функции, используемые в ней, остались теми же самыми!
----
Неудачный способ добавления вложенных сущностей из предыдущего пункта?
Добавим метки для обходчика путей:
> data Mark = Plate | Slug
> - newtype End = End (Request -> Request)
> + newtype End = End Mark (Request -> Request)
Допишем замещающий конструктор:
> - end = End
> + end = End Plate
Добавим спецконструктор...
> slug_end = End Slug
... и упомянем его в slug.
> nested son (plate `Holds` actions) = plate `Holds` (update Slug (`Or` son) actions)
----
Достали очень вложенные сущности?
> decomposed (granny `Contains` mother `Contains` son) =
> decomposed (granny `Contains` mother)
> `And` decomposed (mother `Contains` son)
...
----
Типы, как интерфейсы.
> newtype End = End (Request -> Maybe Request)
> newtype Node = Node (Request -> Maybe Request)
> newtype Controller = Controller (Maybe Request -> IO Data)
> newtype View = View (Data -> HTML)
----
Транзакции, не состояния.
> -- from Prelude module
> f . g = \x -> f (g x)
> page n len = take len . drop (len * n)
----
Автоматические абстракции + функции as first-class entities, функции высшего порядка, автокаррирование.
> f . g = \x -> f (g x)
> (.) :: (a -> b) -> -- f
> (b -> c) -> -- g
> (a -> c) -- результат
--
> page n len = take len . drop (len * n)
(page 5 20) - функция, которая выберет записи с 100-й по 120-ю.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment