def flat

bend-kit-llm@0.13.0.1 · llm.bend:122

import bend-kit-llm@0.13.0.1/llm.bend as Llm
import Base
import bend-kit-json@0.7.0.0/json.bend as Json
import bend-kit-bytes@0.3.3.0/bytes.bend as Bytes
import bend-kit-encoding@0.5.0.0/encoding.bend as Enc

122# The leaves under the stack's values, last first. f: at least the values left.
123def flat(f: Nat, stack: List<&1, String & Json.Val>, acc: List<&2, Leaf>) -> List<&2, Leaf>:
124  match f:
125    case 0n:
126      acc
127    case 1n+g:
128      match stack:
129        case Nil{}:
130          acc
131        case Con{(p, v), t}:
132          match v:
133            case Json.Null{}:
134              flat(g, t, acc)
135            case Json.Flag{on}:
136              flat(g, t, Con{Leaf{p, Bool.pick(String, on, "true", "false")}, acc})
137            case Json.Num{s}:
138              flat(g, t, Con{Leaf{p, Bytes.to_string(s)}, acc})
139            case Json.Str{s}:
140              flat(g, t, Con{Leaf{p, Enc.utf8.decode(s)}, acc})
141            case Json.Arr{xs}:
142              flat(g, push.arr(xs, p, 0, t), acc)
143            case Json.Obj{kvs}:
144              flat(g, push.obj(kvs, p, t), acc)

Uses (12)

Used by (1)