Skip to content

Instantly share code, notes, and snippets.

@amitaibu
Last active August 31, 2015 20:17
Show Gist options
  • Save amitaibu/bd130e24e586167ef92e to your computer and use it in GitHub Desktop.
Save amitaibu/bd130e24e586167ef92e to your computer and use it in GitHub Desktop.
{
"version": "1.0.0",
"summary": "helpful summary of your project, less than 80 characters",
"repository": "https://github.com/USER/PROJECT.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "2.1.0 <= v < 3.0.0",
"evancz/elm-html": "4.0.1 <= v < 5.0.0",
"evancz/elm-http": "1.0.0 <= v < 2.0.0",
"evancz/elm-markdown": "1.1.5 <= v < 2.0.0",
"evancz/task-tutorial": "1.0.2 <= v < 2.0.0"
},
"elm-version": "0.15.1 <= v < 0.16.0"
}
import TaskTutorial exposing (getCurrentTime, print)
import Task exposing (Task, andThen)
import Graphics.Element exposing (show)
port runner : Task x ()
port runner =
getCurrentTime `andThen` print
main =
show "Open the Developer Console to see the current timestamp."
import TaskTutorial exposing (getCurrentTime, print)
import Task exposing (Task, andThen)
import Graphics.Element exposing (Element, show)
main : Signal Element
main =
Signal.map show contentMailbox.signal
contentMailbox : Signal.Mailbox String
contentMailbox =
Signal.mailbox ""
port updateContent : Task x ()
port updateContent =
Signal.send contentMailbox.address "hello!"
import Http
import Markdown
import Html exposing (Html)
import Task exposing (Task, andThen)
main : Signal Html
main =
Signal.map Markdown.toHtml readme.signal
-- set up mailbox
-- the signal is piped directly to main
-- the address lets us update the signal
readme : Signal.Mailbox String
readme =
Signal.mailbox ""
-- send some markdown to our readme mailbox
report : String -> Task x ()
report markdown =
Signal.send readme.address markdown
-- get the readme *and then* send the result to our mailbox
port fetchReadme : Task Http.Error ()
port fetchReadme =
Http.getString readmeUrl `andThen` report
-- the URL of the README.md that we desire
readmeUrl : String
readmeUrl =
"https://raw.githubusercontent.com/elm-lang/core/master/README.md"
import TaskTutorial exposing (getCurrentTime, print)
import Task exposing (Task, andThen, succeed)
import Time exposing (Time)
import Graphics.Element exposing (show)
fibonacci : Int -> Int
fibonacci n = case n of
1 -> 1
2 -> 1
_ -> fibonacci (n-1) + fibonacci (n-2)
getDuration : Task x Time
getDuration =
getCurrentTime
`andThen` \start -> succeed (fibonacci 20)
`andThen` \fib -> getCurrentTime
`andThen` \end -> succeed (end - start)
port runner : Task x ()
port runner =
getDuration `andThen` print
main =
show "Open the Developer Console to see the time it took to calcualte 20 steps of fibonacci."
import Graphics.Element exposing (show)
import Http
import Json.Decode as Json
import Task exposing (Task, andThen, onError, succeed)
import TaskTutorial exposing (print)
get : Task Http.Error (List String)
get =
Http.get (Json.list Json.string) "http://example.com/hat-list.json"
safeGet : Task x (List String)
safeGet =
get `onError` (\err -> succeed [])
port runner : Task x ()
port runner =
safeGet `andThen` print
main =
show "Open the Developer Console to see a failed HTTP requested handled."
import Graphics.Element exposing (show)
import Http
import Json.Decode as Json
import Task exposing (Task, andThen, toResult)
import TaskTutorial exposing (print)
get : Task Http.Error (List String)
get =
Http.get (Json.list Json.string) "http://example.com/hat-list.json"
safeGet : Task x (Result Http.Error (List String))
safeGet =
Task.toResult get
port runner : Task x ()
port runner =
safeGet `andThen` print
main =
show "Open the Developer Console to see a failed HTTP requested handled."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment