add set function and make all collection functions generic

This commit is contained in:
Brendan Hansknecht 2023-06-22 18:29:02 -07:00
parent 5cbc389c44
commit 7bf3dd2f42
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
4 changed files with 140 additions and 77 deletions

View file

@ -21,6 +21,7 @@ GuiFormatter := { nodes : List Elem }
Formatter {
init: init,
list: list,
set: set,
dict: dict,
tag: tag,
tuple: tuple,
@ -47,20 +48,52 @@ GuiFormatter := { nodes : List Elem }
init : {} -> GuiFormatter
init = \{} -> @GuiFormatter { nodes: [] }
list : List elem, (elem -> Inspector GuiFormatter) -> Inspector GuiFormatter
list = \content, toInspector ->
list : list, Inspect.ElemWalkFn GuiFormatter list elem, (elem -> Inspector GuiFormatter) -> Inspector GuiFormatter
list = \content, walkFn, toInspector ->
f0 <- Inspect.custom
# Use a temporary buffer for the children nodes
(@GuiFormatter { nodes }) =
init {}
|> \f1 ->
f2, elem <- List.walk content f1
f2, elem <- walkFn content f1
elem
|> toInspector
|> Inspect.apply f2
addNode f0 (Col nodes)
set : set, Inspect.ElemWalkFn GuiFormatter set elem, (elem -> Inspector GuiFormatter) -> Inspector GuiFormatter
set = \content, walkFn, toInspector ->
f0 <- Inspect.custom
# Use a temporary buffer for the children nodes
(@GuiFormatter { nodes }) =
init {}
|> \f1 ->
f2, elem <- walkFn content f1
elem
|> toInspector
|> Inspect.apply f2
addNode f0 (Col nodes)
dict : dict, Inspect.KeyValWalkFn GuiFormatter dict key value, (key -> Inspector GuiFormatter), (value -> Inspector GuiFormatter) -> Inspector GuiFormatter
dict = \d, walkFn, keyToInspector, valueToInspector ->
f0 <- Inspect.custom
# Use a temporary buffer for the children nodes
(@GuiFormatter { nodes }) =
init {}
|> \f1 ->
f2, key, value <- walkFn d f1
(@GuiFormatter { nodes: innerNodes }) =
init {}
|> \x -> Inspect.apply (keyToInspector key) x
|> addNode (Text ":")
|> \x -> Inspect.apply (valueToInspector value) x
addNode f2 (Row innerNodes)
addNode f0 (Col nodes)
tag : Str, List (Inspector GuiFormatter) -> Inspector GuiFormatter
tag = \name, fields ->
f0 <- Inspect.custom
@ -104,24 +137,6 @@ record = \fields ->
addNode f0 (Col nodes)
dict : dict, Inspect.DictWalkFn GuiFormatter dict key value, (key -> Inspector GuiFormatter), (value -> Inspector GuiFormatter) -> Inspector GuiFormatter
dict = \d, walkFn, keyToInspector, valueToInspector ->
f0 <- Inspect.custom
# Use a temporary buffer for the children nodes
(@GuiFormatter { nodes }) =
init {}
|> \f1 ->
f2, key, value <- walkFn d f1
(@GuiFormatter { nodes: innerNodes }) =
init {}
|> \x -> Inspect.apply (keyToInspector key) x
|> addNode (Text ":")
|> \x -> Inspect.apply (valueToInspector value) x
addNode f2 (Row innerNodes)
addNode f0 (Col nodes)
bool : Bool -> Inspector GuiFormatter
bool = \b ->
if b then

View file

@ -3,6 +3,7 @@ interface Inspect
Formatter,
init,
list,
set,
dict,
tag,
tuple,
@ -28,26 +29,28 @@ interface Inspect
Inspect,
inspect,
toInspector,
DictWalkFn,
KeyValWalkFn,
ElemWalkFn,
]
imports []
DictWalkFn state dict key value : dict, state, (state, key, value -> state) -> state
KeyValWalkFn state container key value : container, state, (state, key, value -> state) -> state
ElemWalkFn state container elem : container, state, (state, elem -> state) -> state
Formatter has
init : {} -> f | f has Formatter
list : List elem, (elem -> Inspector f) -> Inspector f | f has Formatter
tag : Str, List (Inspector f) -> Inspector f | f has Formatter
tuple : List (Inspector f) -> Inspector f | f has Formatter
record : List { key : Str, value : Inspector f } -> Inspector f | f has Formatter
bool : Bool -> Inspector f | f has Formatter
str : Str -> Inspector f | f has Formatter
# I am not yet sold on this function. Is there a better api.
# Specifying the walk function makes this feel very verbose.
# This is needed to make dicts special so that can print like giant records instead of lists of tuples.
dict : dict, DictWalkFn state dict key value, (key -> Inspector f), (value -> Inspector f) -> Inspector f | f has Formatter
# TODO: is there a clear/better way to make the following apis.
# Including the walk fn just makes this look verbose.
list : list, ElemWalkFn state list elem, (elem -> Inspector f) -> Inspector f | f has Formatter
set : set, ElemWalkFn state set elem, (elem -> Inspector f) -> Inspector f | f has Formatter
dict : dict, KeyValWalkFn state dict key value, (key -> Inspector f), (value -> Inspector f) -> Inspector f | f has Formatter
u8 : U8 -> Inspector f | f has Formatter
i8 : I8 -> Inspector f | f has Formatter

View file

@ -15,6 +15,7 @@ LogFormatter := { bytes : List U8 }
Formatter {
init: init,
list: list,
set: set,
dict: dict,
tag: tag,
tuple: tuple,
@ -41,12 +42,12 @@ LogFormatter := { bytes : List U8 }
init : {} -> LogFormatter
init = \{} -> @LogFormatter { bytes: [] }
list : List elem, (elem -> Inspector LogFormatter) -> Inspector LogFormatter
list = \content, toInspector ->
list : list, Inspect.ElemWalkFn (LogFormatter, Bool) list elem, (elem -> Inspector LogFormatter) -> Inspector LogFormatter
list = \content, walkFn, toInspector ->
f0 <- Inspect.custom
write f0 (Str.toUtf8 "[")
|> \f1 ->
(f2, prependSep), elem <- List.walk content (f1, Bool.false)
(f2, prependSep), elem <- walkFn content (f1, Bool.false)
f3 =
if prependSep then
write f2 (Str.toUtf8 ", ")
@ -60,6 +61,44 @@ list = \content, toInspector ->
|> .0
|> write (Str.toUtf8 "]")
set : set, Inspect.ElemWalkFn (LogFormatter, Bool) set elem, (elem -> Inspector LogFormatter) -> Inspector LogFormatter
set = \content, walkFn, toInspector ->
f0 <- Inspect.custom
write f0 (Str.toUtf8 "{")
|> \f1 ->
(f2, prependSep), elem <- walkFn content (f1, Bool.false)
f3 =
if prependSep then
write f2 (Str.toUtf8 ", ")
else
f2
elem
|> toInspector
|> Inspect.apply f3
|> \f4 -> (f4, Bool.true)
|> .0
|> write (Str.toUtf8 "}")
dict : dict, Inspect.KeyValWalkFn (LogFormatter, Bool) dict key value, (key -> Inspector LogFormatter), (value -> Inspector LogFormatter) -> Inspector LogFormatter
dict = \d, walkFn, keyToInspector, valueToInspector ->
f0 <- Inspect.custom
write f0 (Str.toUtf8 "{")
|> \f1 ->
(f2, prependSep), key, value <- walkFn d (f1, Bool.false)
f3 =
if prependSep then
write f2 (Str.toUtf8 ", ")
else
f2
Inspect.apply (keyToInspector key) f3
|> write (Str.toUtf8 ": ")
|> \x -> Inspect.apply (valueToInspector value) x
|> \f4 -> (f4, Bool.true)
|> .0
|> write (Str.toUtf8 "}")
tag : Str, List (Inspector LogFormatter) -> Inspector LogFormatter
tag = \name, fields ->
if List.isEmpty fields then
@ -111,25 +150,6 @@ record = \fields ->
|> .0
|> write (Str.toUtf8 "}")
dict : dict, Inspect.DictWalkFn (LogFormatter, Bool) dict key value, (key -> Inspector LogFormatter), (value -> Inspector LogFormatter) -> Inspector LogFormatter
dict = \d, walkFn, keyToInspector, valueToInspector ->
f0 <- Inspect.custom
write f0 (Str.toUtf8 "{")
|> \f1 ->
(f2, prependSep), key, value <- walkFn d (f1, Bool.false)
f3 =
if prependSep then
write f2 (Str.toUtf8 ", ")
else
f2
Inspect.apply (keyToInspector key) f3
|> write (Str.toUtf8 ": ")
|> \x -> Inspect.apply (valueToInspector value) x
|> \f4 -> (f4, Bool.true)
|> .0
|> write (Str.toUtf8 "}")
bool : Bool -> Inspector LogFormatter
bool = \b ->
if b then

View file

@ -15,6 +15,7 @@ PrettyLogFormatter := { bytes : List U8, indents : List U8 }
Formatter {
init: init,
list: list,
set: set,
dict: dict,
tag: tag,
tuple: tuple,
@ -41,13 +42,13 @@ PrettyLogFormatter := { bytes : List U8, indents : List U8 }
init : {} -> PrettyLogFormatter
init = \{} -> @PrettyLogFormatter { bytes: [], indents: [] }
list : List elem, (elem -> Inspector PrettyLogFormatter) -> Inspector PrettyLogFormatter
list = \content, toInspector ->
list : list, Inspect.ElemWalkFn (PrettyLogFormatter, Bool) list elem, (elem -> Inspector PrettyLogFormatter) -> Inspector PrettyLogFormatter
list = \content, walkFn, toInspector ->
f0 <- Inspect.custom
write f0 (Str.toUtf8 "[\n")
|> indent
|> \f1 ->
(f2, prependSep), elem <- List.walk content (f1, Bool.false)
(f2, prependSep), elem <- walkFn content (f1, Bool.false)
f3 =
if prependSep then
write f2 (Str.toUtf8 ",\n")
@ -65,6 +66,54 @@ list = \content, toInspector ->
|> writeIndent
|> write (Str.toUtf8 "]")
set : set, Inspect.ElemWalkFn (PrettyLogFormatter, Bool) set elem, (elem -> Inspector PrettyLogFormatter) -> Inspector PrettyLogFormatter
set = \content, walkFn, toInspector ->
f0 <- Inspect.custom
write f0 (Str.toUtf8 "{\n")
|> indent
|> \f1 ->
(f2, prependSep), elem <- walkFn content (f1, Bool.false)
f3 =
if prependSep then
write f2 (Str.toUtf8 ",\n")
else
f2
elemInspector = toInspector elem
f3
|> writeIndent
|> \x -> Inspect.apply elemInspector x
|> \f4 -> (f4, Bool.true)
|> .0
|> write (Str.toUtf8 "\n")
|> outdent
|> writeIndent
|> write (Str.toUtf8 "}")
dict : dict, Inspect.KeyValWalkFn (PrettyLogFormatter, Bool) dict key value, (key -> Inspector PrettyLogFormatter), (value -> Inspector PrettyLogFormatter) -> Inspector PrettyLogFormatter
dict = \d, walkFn, keyToInspector, valueToInspector ->
f0 <- Inspect.custom
write f0 (Str.toUtf8 "{\n")
|> indent
|> \f1 ->
(f2, prependSep), key, value <- walkFn d (f1, Bool.false)
f3 =
if prependSep then
write f2 (Str.toUtf8 ",\n")
else
f2
writeIndent f3
|> \x -> Inspect.apply (keyToInspector key) x
|> write (Str.toUtf8 ": ")
|> \x -> Inspect.apply (valueToInspector value) x
|> \f4 -> (f4, Bool.true)
|> .0
|> write (Str.toUtf8 "\n")
|> outdent
|> writeIndent
|> write (Str.toUtf8 "}")
tag : Str, List (Inspector PrettyLogFormatter) -> Inspector PrettyLogFormatter
tag = \name, fields ->
if List.isEmpty fields then
@ -128,30 +177,6 @@ record = \fields ->
|> writeIndent
|> write (Str.toUtf8 "}")
dict : dict, Inspect.DictWalkFn (PrettyLogFormatter, Bool) dict key value, (key -> Inspector PrettyLogFormatter), (value -> Inspector PrettyLogFormatter) -> Inspector PrettyLogFormatter
dict = \d, walkFn, keyToInspector, valueToInspector ->
f0 <- Inspect.custom
write f0 (Str.toUtf8 "{\n")
|> indent
|> \f1 ->
(f2, prependSep), key, value <- walkFn d (f1, Bool.false)
f3 =
if prependSep then
write f2 (Str.toUtf8 ",\n")
else
f2
writeIndent f3
|> \x -> Inspect.apply (keyToInspector key) x
|> write (Str.toUtf8 ": ")
|> \x -> Inspect.apply (valueToInspector value) x
|> \f4 -> (f4, Bool.true)
|> .0
|> write (Str.toUtf8 "\n")
|> outdent
|> writeIndent
|> write (Str.toUtf8 "}")
bool : Bool -> Inspector PrettyLogFormatter
bool = \b ->
if b then