From 9917a2cfba1bd4206ee87a35aabc25678e9f13bb Mon Sep 17 00:00:00 2001 From: sforman Date: Sat, 29 Jul 2023 07:51:31 -0700 Subject: [PATCH] Refactor pop_int. I don't like passing the stack through isnt_int but that let's you chain with andThen. There's probably a clever or idiomatic way to not do that and couple the stack to the result without passing it through the type checker function but I don't know what it is right now, and this works. --- implementations/Elm/src/Joy.elm | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/implementations/Elm/src/Joy.elm b/implementations/Elm/src/Joy.elm index c0fa384..c743880 100644 --- a/implementations/Elm/src/Joy.elm +++ b/implementations/Elm/src/Joy.elm @@ -1,8 +1,8 @@ module Joy exposing (doit) -import String exposing (replace, words) -import Result exposing (andThen) import Bitwise +import Result exposing (andThen) +import String exposing (replace, words) type JoyType @@ -64,18 +64,33 @@ push_int i stack = (JoyInt i) :: stack pop_int : (List JoyType) -> Result String (Int, List JoyType) -pop_int stack = - case stack of - [] -> Err "Not enough values on Stack" - h :: t -> - case h of - JoyInt i -> - Ok (i, t) - _ -> - Err "Not an integer." +pop_int stack = pop_any stack |> andThen isnt_int + +-- case stack of +-- [] -> Err "Not enough values on Stack" +-- h :: t -> +-- case h of +-- JoyInt i -> +-- Ok (i, t) +-- _ -> +-- Err "Not an integer." +pop_any : (List JoyType) -> Result String (JoyType, List JoyType) +pop_any stack = + case stack of + [] -> + Err "Not enough values on Stack" + item :: rest -> + Ok (item, rest) +isnt_int : (JoyType, List JoyType) -> Result String (Int, List JoyType) +isnt_int (item, stack) = + case item of + JoyInt i -> + Ok (i, stack) + _ -> + Err "Not an integer." -- 2.11.0