Elixirの勉強しているとちょくちょく名前を聞く Elm というフレームワーク。
気になっていたので触ってみました。
デモ: https://ellie-app.com/3R2XX5r5nsYa1/2
コード
module Main exposing (..)
import Html exposing (Html, button, div, text, program)
import Html.Events exposing (onClick)
-- モデル
type alias Model =
{count: Int, text: String}
init : ( Model, Cmd Msg )
init =
( {count = 1, text = "1"}, Cmd.none )
-- メッセージ
type Msg
= Increment
-- VIEW
view : Model -> Html Msg
view model =
div []
[
div [] [text model.text]
, div [] [ button [ onClick Increment ] [ text "count up" ] ]
]
-- 更新
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Increment ->
( {model | count = model.count + 1, text = fizzbuzz model.count}, Cmd.none )
-- サブスクリプション(購読)
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- FIzzBuzz
fizzbuzz : Int -> String
fizzbuzz count =
case ( count % 3, count % 5) of
(0, 0) -> "FizzBuzz"
(0, _) -> "Fizz"
(_, 0) -> "Buzz"
_ -> toString count
-- MAIN
main =
program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
Elm Tutorialのコードを元に書いてみました
感想
Haskell は触ったことがないので、ちょっとだけ戸惑いましたが、
慣れると楽しくかけそうですね。
ただHtmlにデザインをあてるのが大変そうですが、そのへんはどうなんでしょうか・・・・
あと Qiita のシンタックスハイライトはどうすればいいのか。。。