Elm Bounded Counter

I love the way Elm deals with ‘signals’, it’s one of the cleanest approaches to input I’ve seen. To help get a grip on the concept, here’s a fun little challenge I was given. Reimplement the count function so that it’s bounded below by zero.

module BoundedCounter where

import Graphics.Element exposing (..)
import Keyboard

main = Signal.map show boundedCount

keyTuple = Signal.map2 (,) (Keyboard.isDown 65) (Keyboard.isDown 90)

boundedAccumulator (a,z) acc =
    if | a            -> acc + 1
       | z && acc > 0 -> acc - 1
       | otherwise    -> acc

boundedCount = Signal.foldp boundedAccumulator 0 keyTuple

This results in,