usage/JSON_GPU.bend

qasim-bend-kit@0.1.0.1 · 39 lines · 0 definitions

1import Base
2import qasim-bend-kit@0.1.0.1/libs/JSON.bend as Json
3
4def show_result(result: Result<&1, &1, Json.Error, Json.Value>) -> String:
5    match result:
6        case Fail{_}:
7            "ERROR"
8        case Done{value}:
9            Json.stringify(value)
10
11def show_batch(batch: Json.BatchResult) -> String:
12    match batch:
13        case Json.DocumentResult{result}:
14            show_result(result)
15        case Json.DocumentResults{left, right}:
16            show_batch(left) ++ "|" ++ show_batch(right)
17
18def show_optional(batch: Maybe<&1, Json.BatchResult>) -> String:
19    match batch:
20        case None{}:
21            "EMPTY"
22        case Some{results}:
23            show_batch(results)
24
25def check(ok: Bool) -> IO(Unit):
26    match ok:
27        case True{}:
28            IO.print("GPU entry point: results and order match")
29        case False{}:
30            IO.die(Unit, 1, "GPU entry point returned unexpected results")
31
32# A native executable uses the GPU when available. --gpu off exercises the
33# CPU fallback. JavaScript executes this example sequentially.
34def main() -> IO(Unit):
35    do IO<Unit>:
36        actual : String = show_optional(Json.parse_many_gpu(
37            "null" <> "true" <> "[1,]" <> "-12.50e+3" <>
38            "{\"x\":[false,\"ok\"]}" <> Nil{}))
39        check(String.eq(actual, "null|true|ERROR|-12.50e+3 |{\"x\":[false,\"ok\"]}"))