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:
parent
b6b3acf350
commit
9917a2cfba
|
|
@ -1,8 +1,8 @@
|
||||||
module Joy exposing (doit)
|
module Joy exposing (doit)
|
||||||
|
|
||||||
import String exposing (replace, words)
|
|
||||||
import Result exposing (andThen)
|
|
||||||
import Bitwise
|
import Bitwise
|
||||||
|
import Result exposing (andThen)
|
||||||
|
import String exposing (replace, words)
|
||||||
|
|
||||||
|
|
||||||
type JoyType
|
type JoyType
|
||||||
|
|
@ -64,18 +64,33 @@ push_int i stack = (JoyInt i) :: stack
|
||||||
|
|
||||||
|
|
||||||
pop_int : (List JoyType) -> Result String (Int, List JoyType)
|
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"
|
-- case stack of
|
||||||
h :: t ->
|
-- [] -> Err "Not enough values on Stack"
|
||||||
case h of
|
-- 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 ->
|
JoyInt i ->
|
||||||
Ok (i, t)
|
Ok (i, stack)
|
||||||
_ ->
|
_ ->
|
||||||
Err "Not an integer."
|
Err "Not an integer."
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue