From ae7c062bdf4ab8337f0eedae78b38df24e62822c Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Thu, 30 Mar 2017 23:09:25 -0400 Subject: - Nat encoding/decoding is now implemented in the standard library. --- stdlib/source/lux.lux | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'stdlib/source/lux.lux') diff --git a/stdlib/source/lux.lux b/stdlib/source/lux.lux index fc606bc36..964cf5b57 100644 --- a/stdlib/source/lux.lux +++ b/stdlib/source/lux.lux @@ -1544,8 +1544,7 @@ #Nil ($' Monad Maybe) {#wrap - (lambda' return [x] - (#Some x)) + (lambda' [x] (#Some x)) #bind (lambda' [f ma] @@ -2070,10 +2069,32 @@ (-> Bool Text) (if x "true" "false")) -(def:''' (Nat/encode x) +(def:''' (digit-to-text digit) #Nil (-> Nat Text) - (_lux_proc ["nat" "encode"] [x])) + (_lux_case digit + +0 "0" + +1 "1" +2 "2" +3 "3" + +4 "4" +5 "5" +6 "6" + +7 "7" +8 "8" +9 "9" + _ (_lux_proc ["io" "error"] ["undefined"]))) + +(def:''' (Nat/encode value) + #Nil + (-> Nat Text) + (_lux_case value + +0 + "+0" + + _ + (let' [loop (_lux_: (-> Nat Text Text) + (lambda' recur [input output] + (if (_lux_proc ["nat" "="] [input +0]) + (_lux_proc ["text" "append"] ["+" output]) + (recur (_lux_proc ["nat" "/"] [input +10]) + (_lux_proc ["text" "append"] [(digit-to-text (_lux_proc ["nat" "%"] [input +10])) + output])))))] + (loop value "")))) (def:''' (Int/encode x) #Nil -- cgit v1.2.3