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.
This commit is contained in:
sforman 2023-07-29 07:51:31 -07:00
parent b6b3acf350
commit 9917a2cfba
1 changed files with 27 additions and 12 deletions

View File

@ -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,21 +64,36 @@ push_int i stack = (JoyInt i) :: stack
pop_int : (List JoyType) -> Result String (Int, List JoyType)
pop_int stack =
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"
h :: t ->
case h 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, t)
Ok (i, stack)
_ ->
Err "Not an integer."
-- Printer
joyTermToString : JoyType -> String