elm 榆树

榆树

main.elm
module Main exposing (main)

import Html exposing (..)


type alias Model =
    { name : String }


type Msg
    = NoOp


main : Program Never Model Msg
main =
    Html.program
        { init = init
        , view = view
        , update = update
        , subscriptions = subscriptions
        }


init : ( Model, Cmd Msg )
init =
    ( { name = "" }, Cmd.none )


view : Model -> Html Msg
view model =
    text "Hello"


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        NoOp ->
            ( model, Cmd.none )


subscriptions : Model -> Sub Msg
subscriptions model =
    Sub.none

elm MyTitleSnip

Cacher的测试片段

fs
var x = 5
var y = 6
var sum = x + y
var sum5 = x + 5

elm 7mli7d.elm.day2.text-handling.elm

7mli7d.elm.day2.text-handling.elm
import Html exposing (Html, text, input, div)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)
import String

main = Html.program { init = init,
       view = view, update = update, subscriptions = \_ -> Sub.none }
       
-- Model

type alias Model = { content : String }

initialModel : Model
initialModel = { content = "" }
init = (initialModel, Cmd.none)

-- Update

type Msg = Change String

update: Msg -> Model -> ( Model, Cmd Msg)
update msg model = case msg of
                     Change c -> ({model | content = c}, Cmd.none)
                   
-- View
shout text = String.toUpper text
whisper text = String.toLower text
echo text = (shout text) ++ " " ++ (whisper text)
myStyle = style [ ("width", "100%") ]

view : Model -> Html Msg
view model = div [] [ input [placeholder "Say something", onInput Change, myStyle ]
  [], div [ myStyle ] [text (echo (toString model.content))] ]

elm 7mli7d.elm.day2.mouse单击-POS-recording.elm

7mli7d.elm.day2.mouse-click-pos-recording.elm
import Html exposing (Html, text, div)
import Mouse exposing (..)

main = Html.program { init = init,
       view = view, update = update, subscriptions = subscriptions }
       
-- Model

type alias Model = { x: Int, y : Int }

initialModel : Model
initialModel = { x = 0, y = 0 }
init = (initialModel, Cmd.none)

-- Update

type Msg = Position Int Int

update: Msg -> Model -> (Model, Cmd a)
update msg model = case msg of
                     Position x y -> ({model | x = x, y = y}, Cmd.none)
                     
-- Subscriptions
subscriptions: Model -> Sub Msg
subscriptions model = Mouse.clicks(\{x, y} -> Position x y)

-- View
-- view: Model -> Html div
view model = div [] [text (toString model)]

elm 7mli7d.elm.day2.mouse单击-counting.elm

7mli7d.elm.day2.mouse-click-counting.elm
import Html exposing (Html, text, div)
import Mouse exposing (..)

main = Html.program { init = init,
       view = view, update = update, subscriptions = subscriptions }
       
-- Model

type alias Model = { count: Int }

initialModel : Model
initialModel = { count = 0 }
init = (initialModel, Cmd.none)

-- Update

type Msg = Position Int Int

update: Msg -> Model -> (Model, Cmd a)
update msg model = case msg of
                     Position x y -> ({model | count = model.count + 1}, Cmd.none)
                     
-- Subscriptions
subscriptions: Model -> Sub Msg
subscriptions model = Mouse.clicks(\{x, y} -> Position x y)

-- View
-- view: Model -> Html div
view model = div [] [text (toString model)]

elm 7mli7d.elm.day2.mouse-counting.elm

7mli7d.elm.day2.mouse-counting.elm
import Html exposing (Html, text, div)
import Mouse exposing (..)

main = Html.program { init = init,
       view = view, update = update, subscriptions = subscriptions }
       
-- Model

type alias Model = { count: Int }

initialModel : Model
initialModel = { count = 0 }
init = (initialModel, Cmd.none)

-- Update

type Msg = Position Int Int

update: Msg -> Model -> (Model, Cmd a)
update msg model = case msg of
                     Position x y -> ({model | count = model.count + 1}, Cmd.none)
                     
-- Subscriptions
subscriptions: Model -> Sub Msg
subscriptions model = Mouse.moves(\{x, y} -> Position x y)

-- View
-- view: Model -> Html div
view model = div [] [text (toString model)]

elm 7mli7d.elm.day2.mouse-tracking.elm

7mli7d.elm.day2.mouse-tracking.elm
import Html exposing (Html, text, div)
-- import Html.App as App
import Mouse exposing (..)

main = Html.program { init = init,
       view = view, update = update, subscriptions = subscriptions }
       
-- Model

type alias Model = { x: Int, y: Int }

initialModel : Model
initialModel = { x = 0, y = 0 }
init = (initialModel, Cmd.none)

-- Update

type Msg = Position Int Int

update: Msg -> Model -> (Model, Cmd a)
update msg model = case msg of
                     Position x y -> ({model | x = x, y = y}, Cmd.none)
                     
-- Subscriptions
subscriptions: Model -> Sub Msg
subscriptions model = Mouse.moves(\{x, y} -> Position x y)

-- View
-- view: Model -> Html div
view model = div [] [text (toString model)]

elm AlgebraicEffectsAttempt.elm

AlgebraicEffectsAttempt.elm
module Elchemy.Platoform.Eff exposing (..)


type alias IO eff =
    { getEmpty : () -> eff
    , reduce : List (eff -> eff)
    }


type Eff x y
    = Unhandled (Maybe x)
    | Handled y
    | None


eff : x -> Eff x y
eff =
    (Unhandled << Just)


type alias Effect eff =
    { getEmpty : eff -> eff
    , apply : eff -> eff
    }


type alias Prints x =
    { x | prints : Eff String () }


print : String -> Effect (Prints x)
print prompt =
    { getEmpty = \x -> { x | prints = None }
    , apply = \x -> { x | prints = eff prompt }
    }


type alias ReadsFile x =
    { x | readsFile : Eff String String }


readFile : String -> Effect (ReadsFile x)
readFile name =
    { getEmpty = \x -> { x | readsFile = None }
    , apply = \x -> { x | readsFile = eff name }
    }


empty : IO {}
empty =
    { getEmpty = always {}
    , reduce = []
    }


andThen : Effect eff -> IO eff -> IO eff
andThen a io =
    { getEmpty = a.getEmpty << io.getEmpty
    , reduce = a.apply :: io.reduce
    }


test =
    empty
        |> andThen (print "a")
        |> andThen (readFile "file.txt")
        |> handle .readsFil someFun
        |> handle .prints (Debug.log "Logging")
        |> handle .someOther -- This would fail. There was no such effect

elm TypeSafeGenServer.elm

TypeSafeGenServer.elm
module SingletonIncrementer exposing (..)

{-| Experiment on implementing type-safe OTP compliant GenServer
-}

import Platform exposing (Task(..))
import Task
import Time


--- Here starts an exemplary API implementation


type OTPErrors
    = NetSplit
    | ProcessNotFound


type alias Process success =
    Task OTPErrors success

{-| Executes a cast command which can modify the state and always returns a Result type -}
cast :
    msg
    -> (state -> state)
    -> Process Result
cast msg response =
    Debug.crash "Crash"

{-| Executes a cast command which can rely on further commands. 
Everything in Process state will get executed _after_ the Process Ressult returns. -}
castCmd :
    msg
    -> (state -> Process state)
    -> Process Result
castCmd msg response =
    Debug.crash "Crash"

{-| Executes a call command which can modify the state and return a result of any type to the caller -}
    msg
    -> (state -> ( reply, state ))
    -> Process reply
call msg =
    Debug.crash "Crash"

{-| Executes a call command which can modify the state and return a result of any type to the caller
Everything in Process state will get executed _before_ the Process reply returns -}
callCmd :
    msg
    -> (state -> Process ( reply, state ))
    -> Process reply
callCmd msg =
    Debug.crash "Crash"


type alias GenServer state msg reply =
    { call :
        msg
        -> (state -> ( reply, state ))
        -> Process reply
    , callCmd :
        msg
        -> (state -> Process ( reply, state ))
        -> Process reply
    , cast :
        msg
        -> (state -> state)
        -> Process Result
    , castCmd :
        msg
        -> (state -> Process state)
        -> Process Result
    }


singleton : Process state -> GenServer state msg reply
singleton init =
    { call = call, cast = cast, callCmd = callCmd, castCmd = castCmd }



-- After this line everything is a GenServer examplary definition


process : GenServer number Msg reply
process =
    singleton <| Task.succeed 0


type Msg
    = Add
    | Increment
    | Decrement
    | Reset
    | SetToTimeNow
    | Set
    | Get


add : number -> Process Result
add a =
    process.cast Add <| (+) a


increment : Process Result
increment =
    process.cast Increment <| (+) 1


decrement : Process Result
decrement =
    process.cast Decrement <| (-) 1


reset : Process Result
reset =
    process.cast Reset <| always 0


set : number -> Process Result
set to =
    process.cast Set <| always to


setToNow : Process Result
setToNow =
    process.castCmd SetToTimeNow <| always Time.now


get : Process number
get =
    process.call Get <| \state -> ( state, state )



------ After this line only testing functions are defined


(>>=) =
    flip Task.andThen


testFlow : Process number
testFlow =
    reset
        >>= always increment
        >>= always decrement
        >>= always get
        >>= (\a -> set <| a + 10)
        >>= always setToNow
        >>= always get

elm GenServer.elm

GenServer.elm
module SingletonIncrementer exposing (..)

import Platform exposing (Task(..))
import Task


cast :
    (a -> msg)
    -> (a -> state -> state)
    -> Task Never ()
cast msg response =
    Debug.crash "Crash"


call :
    (a -> msg)
    -> (a -> state -> ( return, state ))
    -> Task Never return
call msg =
    Debug.crash "Crash"


type GenServerResponse reply state
    = Reply reply state
    | Noreply state
    | Stop


type GenServerAction cast call
    = Cast cast
    | Call call



-- After this line everything is defined by hand


type Cast
    = Add Int
    | Increment ()
    | Decrement ()
    | Reset ()


type Call
    = Get ()


add a =
    cast Add (\msg state -> msg + state)


increment =
    cast Increment (\_ state -> state + 1)


decrement =
    cast Decrement (\_ state -> state - 1)


reset =
    cast Reset (\_ state -> 0)


get =
    call Get (\_ state -> ( state, state ))


------ After this line only tests are defined


testFlow =
    reset
        |> Task.andThen (always increment)
        |> Task.andThen (always increment)
        |> Task.andThen (always decrement)
        |> Task.andThen (always get)
        |> Task.map (\int -> int + 1)