Skip to content

Instantly share code, notes, and snippets.

@evancz
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evancz/a16f5508b982bcc5b252 to your computer and use it in GitHub Desktop.
Save evancz/a16f5508b982bcc5b252 to your computer and use it in GitHub Desktop.

@ do syntax

This is a generalization of the async/await syntax, switching out async for @___ and await for do. I don't think we should consider this for 0.15, but it shows where we might want to take things longer term. In this world, a task example looks like this:

  @task
    [ do print 3
    , do print 4
    , do print 5
    ]

A maybe example looks like this:

add : Maybe Int -> Maybe Int -> Maybe Int
add mx my =
  @maybe
    do mx + do my

Making the main module a task

It is kind of interesting because we can give it a meaning when we add @task to a module declaration:

@task module Main where

mb = do mailbox

...

Haxl

This is cool because Haxl type stuff fits in really easily.

  @haxl
    [ do get user "http://example.com/alice"
    , do get user "http://example.com/bob"
    , do get user "http://example.com/candice"
    ]

And things would all go in parallel.

getImage : (Int,Int) -> String -> Task Http.Error String
getImage dimensions tag =
@task
let
searchUrl =
flickrUrl "search" [ ("sort", "random"), ("per_page", "10"), ("tags", tag) ]
photos =
do Http.get photoList searchUrl
photo =
do Task.fromMaybe photoErr (List.head photos)
sizes =
do Http.get sizeList (flickrUrl "getSizes" [ ("photo_id", photo.id) ])
pickSize =
sizes
|> List.sortBy (sizeRating dimensions)
|> List.head
|> Task.fromMaybe sizeErr
in
do pickSize
field : String -> JS.Decoder a -> JS.Decoder a
index : Int -> JS.Decoder a -> JS.Decoder a
person : Decoder Person
person =
@decoder
let position =
@decoder (do index 0 JS.float, do index 1 JS.float)
in
Person
(do ("name" => JS.string))
(do ("age" => JS.int))
(do ("position" => position))
add : Maybe Int -> Maybe Int -> Maybe Int
add maybeLeft maybeRight =
@maybe
do maybeLeft + do maybeRight
person : Generator Person
person =
@generator
Person
(do Gen.string)
(do Gen.int)
(Location (do Gen.int) (do Gen.int))
type alias Person =
{ fullname : String
, age : Int
, location : Location
}
type alias Location =
{ longitude : Int
, latitude : Int
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment