mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-05 09:30:20 +00:00
add set function and make all collection functions generic
This commit is contained in:
parent
5cbc389c44
commit
7bf3dd2f42
4 changed files with 140 additions and 77 deletions
|
@ -21,6 +21,7 @@ GuiFormatter := { nodes : List Elem }
|
||||||
Formatter {
|
Formatter {
|
||||||
init: init,
|
init: init,
|
||||||
list: list,
|
list: list,
|
||||||
|
set: set,
|
||||||
dict: dict,
|
dict: dict,
|
||||||
tag: tag,
|
tag: tag,
|
||||||
tuple: tuple,
|
tuple: tuple,
|
||||||
|
@ -47,20 +48,52 @@ GuiFormatter := { nodes : List Elem }
|
||||||
init : {} -> GuiFormatter
|
init : {} -> GuiFormatter
|
||||||
init = \{} -> @GuiFormatter { nodes: [] }
|
init = \{} -> @GuiFormatter { nodes: [] }
|
||||||
|
|
||||||
list : List elem, (elem -> Inspector GuiFormatter) -> Inspector GuiFormatter
|
list : list, Inspect.ElemWalkFn GuiFormatter list elem, (elem -> Inspector GuiFormatter) -> Inspector GuiFormatter
|
||||||
list = \content, toInspector ->
|
list = \content, walkFn, toInspector ->
|
||||||
f0 <- Inspect.custom
|
f0 <- Inspect.custom
|
||||||
# Use a temporary buffer for the children nodes
|
# Use a temporary buffer for the children nodes
|
||||||
(@GuiFormatter { nodes }) =
|
(@GuiFormatter { nodes }) =
|
||||||
init {}
|
init {}
|
||||||
|> \f1 ->
|
|> \f1 ->
|
||||||
f2, elem <- List.walk content f1
|
f2, elem <- walkFn content f1
|
||||||
elem
|
elem
|
||||||
|> toInspector
|
|> toInspector
|
||||||
|> Inspect.apply f2
|
|> Inspect.apply f2
|
||||||
|
|
||||||
addNode f0 (Col nodes)
|
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 : Str, List (Inspector GuiFormatter) -> Inspector GuiFormatter
|
||||||
tag = \name, fields ->
|
tag = \name, fields ->
|
||||||
f0 <- Inspect.custom
|
f0 <- Inspect.custom
|
||||||
|
@ -104,24 +137,6 @@ record = \fields ->
|
||||||
|
|
||||||
addNode f0 (Col nodes)
|
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 : Bool -> Inspector GuiFormatter
|
||||||
bool = \b ->
|
bool = \b ->
|
||||||
if b then
|
if b then
|
||||||
|
|
|
@ -3,6 +3,7 @@ interface Inspect
|
||||||
Formatter,
|
Formatter,
|
||||||
init,
|
init,
|
||||||
list,
|
list,
|
||||||
|
set,
|
||||||
dict,
|
dict,
|
||||||
tag,
|
tag,
|
||||||
tuple,
|
tuple,
|
||||||
|
@ -28,26 +29,28 @@ interface Inspect
|
||||||
Inspect,
|
Inspect,
|
||||||
inspect,
|
inspect,
|
||||||
toInspector,
|
toInspector,
|
||||||
DictWalkFn,
|
KeyValWalkFn,
|
||||||
|
ElemWalkFn,
|
||||||
]
|
]
|
||||||
imports []
|
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
|
Formatter has
|
||||||
init : {} -> f | f has Formatter
|
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
|
tag : Str, List (Inspector f) -> Inspector f | f has Formatter
|
||||||
tuple : 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
|
record : List { key : Str, value : Inspector f } -> Inspector f | f has Formatter
|
||||||
bool : Bool -> Inspector f | f has Formatter
|
bool : Bool -> Inspector f | f has Formatter
|
||||||
str : Str -> 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.
|
# TODO: is there a clear/better way to make the following apis.
|
||||||
# Specifying the walk function makes this feel very verbose.
|
# Including the walk fn just makes this look verbose.
|
||||||
# This is needed to make dicts special so that can print like giant records instead of lists of tuples.
|
list : list, ElemWalkFn state list elem, (elem -> Inspector f) -> Inspector f | f has Formatter
|
||||||
dict : dict, DictWalkFn state dict key value, (key -> Inspector f), (value -> 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
|
u8 : U8 -> Inspector f | f has Formatter
|
||||||
i8 : I8 -> Inspector f | f has Formatter
|
i8 : I8 -> Inspector f | f has Formatter
|
||||||
|
|
|
@ -15,6 +15,7 @@ LogFormatter := { bytes : List U8 }
|
||||||
Formatter {
|
Formatter {
|
||||||
init: init,
|
init: init,
|
||||||
list: list,
|
list: list,
|
||||||
|
set: set,
|
||||||
dict: dict,
|
dict: dict,
|
||||||
tag: tag,
|
tag: tag,
|
||||||
tuple: tuple,
|
tuple: tuple,
|
||||||
|
@ -41,12 +42,12 @@ LogFormatter := { bytes : List U8 }
|
||||||
init : {} -> LogFormatter
|
init : {} -> LogFormatter
|
||||||
init = \{} -> @LogFormatter { bytes: [] }
|
init = \{} -> @LogFormatter { bytes: [] }
|
||||||
|
|
||||||
list : List elem, (elem -> Inspector LogFormatter) -> Inspector LogFormatter
|
list : list, Inspect.ElemWalkFn (LogFormatter, Bool) list elem, (elem -> Inspector LogFormatter) -> Inspector LogFormatter
|
||||||
list = \content, toInspector ->
|
list = \content, walkFn, toInspector ->
|
||||||
f0 <- Inspect.custom
|
f0 <- Inspect.custom
|
||||||
write f0 (Str.toUtf8 "[")
|
write f0 (Str.toUtf8 "[")
|
||||||
|> \f1 ->
|
|> \f1 ->
|
||||||
(f2, prependSep), elem <- List.walk content (f1, Bool.false)
|
(f2, prependSep), elem <- walkFn content (f1, Bool.false)
|
||||||
f3 =
|
f3 =
|
||||||
if prependSep then
|
if prependSep then
|
||||||
write f2 (Str.toUtf8 ", ")
|
write f2 (Str.toUtf8 ", ")
|
||||||
|
@ -60,6 +61,44 @@ list = \content, toInspector ->
|
||||||
|> .0
|
|> .0
|
||||||
|> write (Str.toUtf8 "]")
|
|> 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 : Str, List (Inspector LogFormatter) -> Inspector LogFormatter
|
||||||
tag = \name, fields ->
|
tag = \name, fields ->
|
||||||
if List.isEmpty fields then
|
if List.isEmpty fields then
|
||||||
|
@ -111,25 +150,6 @@ record = \fields ->
|
||||||
|> .0
|
|> .0
|
||||||
|> write (Str.toUtf8 "}")
|
|> 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 : Bool -> Inspector LogFormatter
|
||||||
bool = \b ->
|
bool = \b ->
|
||||||
if b then
|
if b then
|
||||||
|
|
|
@ -15,6 +15,7 @@ PrettyLogFormatter := { bytes : List U8, indents : List U8 }
|
||||||
Formatter {
|
Formatter {
|
||||||
init: init,
|
init: init,
|
||||||
list: list,
|
list: list,
|
||||||
|
set: set,
|
||||||
dict: dict,
|
dict: dict,
|
||||||
tag: tag,
|
tag: tag,
|
||||||
tuple: tuple,
|
tuple: tuple,
|
||||||
|
@ -41,13 +42,13 @@ PrettyLogFormatter := { bytes : List U8, indents : List U8 }
|
||||||
init : {} -> PrettyLogFormatter
|
init : {} -> PrettyLogFormatter
|
||||||
init = \{} -> @PrettyLogFormatter { bytes: [], indents: [] }
|
init = \{} -> @PrettyLogFormatter { bytes: [], indents: [] }
|
||||||
|
|
||||||
list : List elem, (elem -> Inspector PrettyLogFormatter) -> Inspector PrettyLogFormatter
|
list : list, Inspect.ElemWalkFn (PrettyLogFormatter, Bool) list elem, (elem -> Inspector PrettyLogFormatter) -> Inspector PrettyLogFormatter
|
||||||
list = \content, toInspector ->
|
list = \content, walkFn, toInspector ->
|
||||||
f0 <- Inspect.custom
|
f0 <- Inspect.custom
|
||||||
write f0 (Str.toUtf8 "[\n")
|
write f0 (Str.toUtf8 "[\n")
|
||||||
|> indent
|
|> indent
|
||||||
|> \f1 ->
|
|> \f1 ->
|
||||||
(f2, prependSep), elem <- List.walk content (f1, Bool.false)
|
(f2, prependSep), elem <- walkFn content (f1, Bool.false)
|
||||||
f3 =
|
f3 =
|
||||||
if prependSep then
|
if prependSep then
|
||||||
write f2 (Str.toUtf8 ",\n")
|
write f2 (Str.toUtf8 ",\n")
|
||||||
|
@ -65,6 +66,54 @@ list = \content, toInspector ->
|
||||||
|> writeIndent
|
|> writeIndent
|
||||||
|> write (Str.toUtf8 "]")
|
|> 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 : Str, List (Inspector PrettyLogFormatter) -> Inspector PrettyLogFormatter
|
||||||
tag = \name, fields ->
|
tag = \name, fields ->
|
||||||
if List.isEmpty fields then
|
if List.isEmpty fields then
|
||||||
|
@ -128,30 +177,6 @@ record = \fields ->
|
||||||
|> writeIndent
|
|> writeIndent
|
||||||
|> write (Str.toUtf8 "}")
|
|> 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 : Bool -> Inspector PrettyLogFormatter
|
||||||
bool = \b ->
|
bool = \b ->
|
||||||
if b then
|
if b then
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue