Skip to content

Instantly share code, notes, and snippets.

@TheSeamau5
Last active August 29, 2015 14:25
Show Gist options
  • Save TheSeamau5/576dadc6098b1d7c53ff to your computer and use it in GitHub Desktop.
Save TheSeamau5/576dadc6098b1d7c53ff to your computer and use it in GitHub Desktop.
import Html exposing (..)
import String
import List
import Color exposing (Color)
--
infixl 2 =>
(=>) = (,)
--
type Query
= Object String QueryParameters (List Query)
type alias QueryParameters = List (String, String)
field : String -> Query
field name =
Object name [] []
query : String -> List (String, String) -> List Query -> Query
query =
Object
render : Query -> String
render query =
let
ntabs n =
String.repeat n " "
renderParam (name, value) =
name ++ " : " ++ value
renderParams params =
if List.isEmpty params
then
""
else
"("
++ (List.map renderParam params |> String.join ", ")
++ ")"
renderWithTab n q =
case q of
Object name params qs ->
ntabs n
++ name
++ renderParams params
++ renderQueries n qs
renderQueries n queries =
if List.isEmpty queries
then
""
else
"{\n"
++ (List.map (renderWithTab (n + 2)) queries |> String.join "\n")
++ "\n"
++ ntabs n
++ "}"
in
renderWithTab 0 query
--------------------
profilePic =
query "ProfilePic" []
[ field "source"
, field "width"
, field "height"
, field "description"
]
friendInfo =
query "FriendInfo" [ ]
[ field "name"
, field "age"
]
friendListItem id =
query "FriendListItem" [ ("id", toString id) ]
[ profilePic
, friendInfo
]
friendList =
query "FriendList" []
( List.map friendListItem [1..10] )
--main =
-- pre []
-- [ text (render friendList) ]
----------------------
type alias Transaction state effect =
{ state : state
, effect : Maybe effect
}
request : state -> effect -> Transaction state effect
request state effect =
{ state = state
, effect = Just effect
}
----------------------
initImage options =
let
state =
{ color = options.color
, width = options.width
, height = options.height
, source = Nothing
, description = Nothing
}
effect =
query "Image"
[ "id" => toString options.id
, "width" => toString options.width
, "height" => toString options.height
]
[ field "source"
, field "description"
]
in
request state effect
initCarousel options =
let
makeImageOptions id =
{ id = id
, width = 200
, height = 200
, color = options.color
}
imageOptions =
List.map makeImageOptions [1..10]
transactions =
List.map initImage imageOptions
imageStates =
List.map .state transactions
imageQueries =
List.map .effect transactions
|> keepJusts
state =
{ images = imageStates }
effect =
query "Carousel"
[ "id" => toString options.id ]
( imageQueries )
in
request state effect
keepJusts list =
case list of
[] ->
[]
m :: ms ->
case m of
Nothing ->
keepJusts ms
Just x ->
x :: keepJusts ms
carousel =
initCarousel { color = Color.blue, id = 3389744 }
main =
case carousel.effect of
Nothing ->
text ""
Just effect ->
pre []
[ text (render effect) ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment