OSDN Git Service

Refactor pop_int.
authorsforman <sforman@hushmail.com>
Sat, 29 Jul 2023 14:51:31 +0000 (07:51 -0700)
committersforman <sforman@hushmail.com>
Sat, 29 Jul 2023 14:51:31 +0000 (07:51 -0700)
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

index c0fa384..c743880 100644 (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,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."