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/88bf5ef55583f180750f to your computer and use it in GitHub Desktop.
Save evancz/88bf5ef55583f180750f to your computer and use it in GitHub Desktop.
-- WIRING - how does this feel?
main : Task x (Signal Html)
main =
Stream.mailbox
`andThen` \queryMailbox ->
Task.runStream (Stream.sample getImage Window.dimensions queryMailbox.stream)
`andThen` \results ->
Signal.map3 (view queryMailbox.address)
Window.height
(Stream.toSignal "" queryMailbox.stream)
(Stream.toSignal "waiting.gif" <| Stream.filterMap Result.toMaybe results)
-- FLICKR - how does getImage feel?
flickrUrl : String -> List (String, String) -> String
getImage : (Int,Int) -> String -> Task Http.Error String
getImage dimensions tag =
let
args = [ ("sort", "random"), ("per_page", "10"), ("tags", tag) ]
getPhotoList =
Http.get decodePhotoList (flickrUrl "search" args)
choosePhoto photoList =
Task.fromMaybe photoErr (List.head photoList)
getSizeList photo =
Http.get decodeSizeList (flickrUrl "getSizes" [ ("photo_id", photo.id) ])
chooseSize sizeList =
sizeList
|> List.sortBy (sizeRating dimensions)
|> List.head
|> Task.fromMaybe sizeErr
in
getPhotoList
`andThen` choosePhoto
`andThen` getSizeList
`andThen` chooseSize
-- OTHER STUFF - same in all versions
view : Address String -> Int -> String -> String -> Html
photoErr : Http.Error
sizeErr : Http.Error
type alias Photo = { id : String, title : String }
type alias Size = { source : String, width : Int, height : Int }
decodePhotoList : JS.Decoder (List Photo)
decodeSizeList : JS.Decoder (List Size)
sizeRating : (Int,Int) -> Size -> Int
-- WIRING - how does this feel?
query := Stream.mailbox
results : Stream (Result Http.Error String)
results :=
Task.runStream (Stream.sample getImage Window.dimensions query.stream)
main : Signal Html
main =
Signal.map3 (view query.address)
Window.height
(Stream.toSignal "" query.stream)
(Stream.toSignal "waiting.gif" <| Stream.filterMap Result.toMaybe results)
-- FLICKR - how does getImage feel?
getImage : (Int,Int) -> String -> Task Http.Error String
getImage dimensions tag =
tasklet
args = [ ("sort", "random"), ("per_page", "10"), ("tags", tag) ]
photoList :=
Http.get decodePhotoList (flickrUrl "search" args)
photo :=
Task.fromMaybe photoErr (List.head photoList)
sizeList :=
Http.get decodeSizeList (flickrUrl "getSizes" [ ("photo_id", photo.id) ])
in
sizeList
|> List.sortBy (sizeRating dimensions)
|> List.head
|> Task.fromMaybe sizeErr
-- OTHER STUFF - same in all versions
view : Address String -> Int -> String -> String -> Html
flickrUrl : String -> List (String, String) -> String
photoErr : Http.Error
sizeErr : Http.Error
type alias Photo = { id : String, title : String }
type alias Size = { source : String, width : Int, height : Int }
decodePhotoList : JS.Decoder (List Photo)
decodeSizeList : JS.Decoder (List Size)
sizeRating : (Int,Int) -> Size -> Int
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment