← 모든 예제
예제 16
Missing values
"no value" is always said, never a silent 0 — empty
- 분류
- Pure: 어디서나 실행
- 권한
- 없음
- 하는 일
- none
- 실행 방법
- t0 run examples/16_missing_values.tz
- 언어
>=0.1.1, <0.2.0
소스
// Purpose: "no value" is always said, never a silent 0 — empty // statistics, indicators before they have enough data, out-of-range // reads, text that is not a number. // Category: pure // Permissions: none // Side effects: none // Run: t0 run examples/16_missing_values.tz lang ">=0.1.1, <0.2.0"; import std.stats; import std.ind; import std.decimal; import std.text; fn describe(value: Option<Float>) -> String { return match value { Some(x) => to_string(x), None => "no value", }; } fn main() { let empty: List<Float> = []; print("mean of nothing: " + describe(stats.mean(empty))); let one: List<Float> = [42.0]; print("stdev of one value: " + describe(stats.stdev(one))); let short: List<Float> = [1.0, 2.0]; print("a 3-period SMA of 2 values: " + to_string(ind.sma(short, 3))); print("its latest value: " + describe(ind.latest(ind.sma(short, 3)))); let xs = [10, 20, 30]; print("xs.get(7): " + to_string(xs.get(7))); print("decimal.parse(\"12,5\"): " + to_string(decimal.parse("12,5"))); print("text.parse_int(\"seven\"): " + to_string(text.parse_int("seven"))); let fallback = xs.get(7).unwrap_or(-1); print("an explicit default, chosen by the program: " + to_string(fallback)); } // expect-output: mean of nothing: no value // expect-output: stdev of one value: no value // expect-output: a 3-period SMA of 2 values: [None, None] // expect-output: its latest value: no value // expect-output: xs.get(7): None // expect-output: decimal.parse("12,5"): None // expect-output: text.parse_int("seven"): None // expect-output: an explicit default, chosen by the program: -1