Skip to content

Instantly share code, notes, and snippets.

@lambduli
Created May 10, 2021 11:55
Show Gist options
  • Save lambduli/7995a31e620dfa6760d0db13d46d0bb6 to your computer and use it in GitHub Desktop.
Save lambduli/7995a31e620dfa6760d0db13d46d0bb6 to your computer and use it in GitHub Desktop.
99 Bottles Of Beer in Elm
module Main exposing (Model, Msg(..), break, init, main, toParagraph, update, verseToHtml, view)
import Browser
import Html exposing (Html, br, div, p, text)
import List exposing (append, map, singleton)
import String exposing (fromInt)
break : Html msg
break =
br [] []
toParagraph : String -> Html msg
toParagraph =
text >> singleton >> p []
verseToHtml : List (Html msg) -> Html msg
verseToHtml paragraphs =
div
[]
(append
paragraphs
[ break ]
)
formatLine : Int -> String -> String
formatLine amount line =
if amount == 1 then
"1 bottle" ++ " " ++ line
else if amount == 0 then
"no more bottles" ++ " " ++ line
else
fromInt amount ++ " bottles" ++ " " ++ line
singVerse : Int -> Html msg
singVerse num =
verseToHtml
[ formatLine num "of beer on the wall" |> toParagraph
, formatLine num "of beer" |> toParagraph
, "Take one down, pass it around" |> toParagraph
, formatLine (num - 1) "of beer on the wall" |> toParagraph
]
singAbout : Int -> List (Html msg)
singAbout num =
if num > 0 then
singVerse num :: singAbout (num - 1)
else
[]
---- MODEL ----
type alias Model =
{}
init : ( Model, Cmd Msg )
init =
( {}, Cmd.none )
---- UPDATE ----
type Msg
= NoOp
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
( model, Cmd.none )
---- VIEW ----
view : Model -> Html Msg
view model =
div [] (singAbout 99)
---- PROGRAM ----
main : Program () Model Msg
main =
Browser.element
{ view = view
, init = \_ -> init
, update = update
, subscriptions = always Sub.none
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment