LoginSignup
6
1

ElmでFizzBuzzを書いてみる

Last updated at Posted at 2017-07-26

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 のシンタックスハイライトはどうすればいいのか。。。

6
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1