This commit is contained in:
commit
568474d52f
1591 files changed
+528213
No files matched your search
@@ -0,0 +1,5 @@
|
||||
# probe: can all() take a generator expression, as in Python? (no: Mojo 1.2.0 has no generator expressions)
|
||||
# expect: compile-error
|
||||
def main():
|
||||
var xs: List[Int] = [1, 2, 3]
|
||||
print(all(x > 0 for x in xs))
|
||||
@@ -0,0 +1,4 @@
|
||||
# probe: does atof() read hexadecimal text such as "0x10"? (no: it raises)
|
||||
def main() raises:
|
||||
print(atof("2.5"))
|
||||
print(atof("0x10"))
|
||||
@@ -0,0 +1,11 @@
|
||||
# probe: the compiler's words for passing one value as mut and as a second argument
|
||||
# expect: compile-error
|
||||
def append_twice(mut s: String, other: String):
|
||||
s += other
|
||||
s += other
|
||||
|
||||
|
||||
def main():
|
||||
var my_string = String("o")
|
||||
append_twice(my_string, my_string)
|
||||
print(my_string)
|
||||
@@ -0,0 +1,25 @@
|
||||
# probe: the compiler's words for copying a type that is not ImplicitlyCopyable
|
||||
# expect: compile-error
|
||||
struct Loud(Copyable, Movable):
|
||||
var name: String
|
||||
|
||||
def __init__(out self, name: String):
|
||||
self.name = name
|
||||
print("init", name)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.name = copy.name + "'"
|
||||
print("copy", copy.name)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.name = move.name^
|
||||
print("move", self.name)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("destroy", self.name)
|
||||
|
||||
|
||||
def main():
|
||||
var a = Loud("a")
|
||||
var b = a
|
||||
print(a.name, b.name)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: 1.1.0 notes: assigning an undeclared name is an error
|
||||
# expect: compile-error
|
||||
def main():
|
||||
x = 5
|
||||
print(x)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: calling a raising function from a non-raising one
|
||||
# expect: compile-error
|
||||
def main():
|
||||
var a = input()
|
||||
print(a)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: the compiler's words for reading a variable before it is set
|
||||
# expect: compile-error
|
||||
def main():
|
||||
var x: Int
|
||||
print(x)
|
||||
@@ -0,0 +1,30 @@
|
||||
# probe: moved out on one path, read after the branch: the compiler's words
|
||||
# expect: compile-error
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def take(var t: L):
|
||||
print("took", t.n)
|
||||
|
||||
|
||||
def run(flag: Bool):
|
||||
var a = L("a")
|
||||
if flag:
|
||||
take(a^)
|
||||
print(a.n)
|
||||
|
||||
|
||||
def main():
|
||||
run(True)
|
||||
@@ -0,0 +1,25 @@
|
||||
# probe: the compiler's words for using a value after ^ moved it
|
||||
# expect: compile-error
|
||||
struct Loud(Copyable, Movable):
|
||||
var name: String
|
||||
|
||||
def __init__(out self, name: String):
|
||||
self.name = name
|
||||
print("init", name)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.name = copy.name + "'"
|
||||
print("copy", copy.name)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.name = move.name^
|
||||
print("move", self.name)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("destroy", self.name)
|
||||
|
||||
|
||||
def main():
|
||||
var a = Loud("a")
|
||||
var b = a^
|
||||
print(a.name, b.name)
|
||||
@@ -0,0 +1,9 @@
|
||||
# probe: comptime for and comptime if in a function body
|
||||
comptime N = 3
|
||||
|
||||
|
||||
def main():
|
||||
comptime for i in range(N):
|
||||
print(i)
|
||||
comptime if N > 2:
|
||||
print("big")
|
||||
@@ -0,0 +1,14 @@
|
||||
# probe: 1.0.0 notes said __del__ still works, deprecated; does it in 1.1.0?
|
||||
struct Old:
|
||||
var v: Int
|
||||
|
||||
def __init__(out self, v: Int):
|
||||
self.v = v
|
||||
|
||||
def __del__(deinit self):
|
||||
print("old-style destructor ran")
|
||||
|
||||
|
||||
def main():
|
||||
var o = Old(1)
|
||||
print(o.v)
|
||||
@@ -0,0 +1,4 @@
|
||||
# probe: 1.1.0 notes: fn is removed
|
||||
# expect: compile-error
|
||||
fn main():
|
||||
print("fn still works")
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: 1.1.0 notes: the read convention is a hard error
|
||||
# expect: compile-error
|
||||
def show(read x: Int):
|
||||
print(x)
|
||||
|
||||
|
||||
def main():
|
||||
show(1)
|
||||
@@ -0,0 +1,11 @@
|
||||
# probe: deprecated in 0.26.2: is @register_passable("trivial") an error or a warning in 1.1.0?
|
||||
@register_passable("trivial")
|
||||
struct P:
|
||||
var v: Int
|
||||
|
||||
def __init__(out self, v: Int):
|
||||
self.v = v
|
||||
|
||||
|
||||
def main():
|
||||
print(P(1).v)
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: is the @value decorator still accepted?
|
||||
@value
|
||||
struct V:
|
||||
var x: Int
|
||||
|
||||
|
||||
def main():
|
||||
print(V(1).x)
|
||||
@@ -0,0 +1,7 @@
|
||||
# probe: what does reading a missing Dict key raise, and how is it printed?
|
||||
def main() raises:
|
||||
var d = {"a": 1}
|
||||
try:
|
||||
print(d["b"])
|
||||
except e:
|
||||
print("caught:", e)
|
||||
@@ -0,0 +1,12 @@
|
||||
# probe: does a Dict iterate in insertion order, and after a pop and re-insert?
|
||||
def main() raises:
|
||||
var d = Dict[Int, String]()
|
||||
d[3] = "c"
|
||||
d[1] = "a"
|
||||
d[2] = "b"
|
||||
for k in d.keys():
|
||||
print(k)
|
||||
_ = d.pop(1)
|
||||
d[1] = "a again"
|
||||
for e in d.items():
|
||||
print(e.key, e.value)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: is there an assert statement, and what does a failing one do?
|
||||
def main():
|
||||
var x = 1
|
||||
assert x == 2, "x is not 2"
|
||||
print("after")
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: is debug_assert checked by default on godbolt's build?
|
||||
def main():
|
||||
print("before")
|
||||
debug_assert(False, "debug_assert fired")
|
||||
print("after")
|
||||
@@ -0,0 +1,7 @@
|
||||
# probe: does std.sys.exit set the exit code after output?
|
||||
from std.sys import exit
|
||||
|
||||
|
||||
def main():
|
||||
print("bye")
|
||||
exit(3)
|
||||
@@ -0,0 +1,10 @@
|
||||
# probe: the order of try, except and finally
|
||||
def main():
|
||||
try:
|
||||
print("try")
|
||||
raise Error("x")
|
||||
except e:
|
||||
print("except", e)
|
||||
finally:
|
||||
print("finally")
|
||||
print("after")
|
||||
@@ -0,0 +1,4 @@
|
||||
# probe: what reaches stdout, stderr, and the exit code when main raises?
|
||||
def main() raises:
|
||||
print("before")
|
||||
raise Error("boom")
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: can a whole-number literal stand left of a Float64 in a comparison? (no: 90 <= x has no overload; x >= 90 does)
|
||||
# expect: compile-error
|
||||
def main():
|
||||
var x = 95.5
|
||||
print(90 <= x)
|
||||
@@ -0,0 +1,14 @@
|
||||
# probe: with contraction off (--fp-mode contract=off, as the pin builds), is x * x - x rounded twice, as IEEE arithmetic one operation at a time, both when x arrives at run time and when it is a literal? (float-contract-runtime is the same program with contraction on, where the run-time line comes out -0.22222222222222224)
|
||||
# args: --fp-mode contract=off
|
||||
# stdin: 2 3\n
|
||||
# expect-stdout: -0.2222222222222222
|
||||
# expect-stdout: -0.2222222222222222
|
||||
def square_less(x: Float64) -> Float64:
|
||||
return x * x - x
|
||||
|
||||
|
||||
def main() raises:
|
||||
var w = input().split(" ")
|
||||
var x = Float64(atol(w[0])) / Float64(atol(w[1]))
|
||||
print(square_less(x))
|
||||
print(square_less(2.0 / 3.0))
|
||||
@@ -0,0 +1,14 @@
|
||||
# probe: with contraction on (--fp-mode contract=fast, Mojo's default; the pin builds with it off), is x * x - x rounded the same when x is known at build time and when it arrives at run time? (no: from stdin the built program fuses it into one rounding, -0.22222222222222224; from a literal the compiler works it out while building, rounded twice, -0.2222222222222222; float-contract-runtime-off is the same program with contraction off)
|
||||
# args: --fp-mode contract=fast
|
||||
# stdin: 2 3\n
|
||||
# expect-stdout: -0.22222222222222224
|
||||
# expect-stdout: -0.2222222222222222
|
||||
def square_less(x: Float64) -> Float64:
|
||||
return x * x - x
|
||||
|
||||
|
||||
def main() raises:
|
||||
var w = input().split(" ")
|
||||
var x = Float64(atol(w[0])) / Float64(atol(w[1]))
|
||||
print(square_less(x))
|
||||
print(square_less(2.0 / 3.0))
|
||||
@@ -0,0 +1,11 @@
|
||||
# probe: with contraction on (--fp-mode contract=fast, Mojo's default; the pin builds with it off), does the compiler fuse a * b - c into one rounding when the operands are literals? (fused, 0.1 * 10.0 - 1.0 is 5.551115123125783e-17; rounded twice it is 0.0)
|
||||
# args: --fp-mode contract=fast
|
||||
def fused(a: Float64, b: Float64, c: Float64) -> Float64:
|
||||
return a * b - c
|
||||
|
||||
|
||||
def main():
|
||||
var a = 0.1
|
||||
var b = 10.0
|
||||
var c = 1.0
|
||||
print(a * b - c, fused(a, b, c))
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: how accurate are cosh, sinh and exp of a Float64 far from zero? (true values: cosh(100) = sinh(100) = 1.3440585709080678e43, exp(100) = 2.6881171418161356e43, cosh(1) = 1.5430806348152437)
|
||||
from std.math import cosh, exp, sinh
|
||||
|
||||
|
||||
def main():
|
||||
var a = 100.0
|
||||
var one = 1.0
|
||||
print(cosh(a), sinh(a), exp(a), cosh(one))
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: which mixes of a Float64 with a whole-number literal does the compiler accept, and what do they print?
|
||||
def main():
|
||||
var x = 95.5
|
||||
print(x >= 90, 2 * x, x * 2, x + 1, 1 + x, x == 95, 3 - x, 10 / x, x ** 2, 2.0 ** x)
|
||||
print(90.0 <= x < 101.0, 0.5 > x, 1.5 <= x)
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: how accurate are log2, log10 and log on a Float64? (true values: 0.5 3.321928094887362 0.3010299956639812 0.6931471805599453)
|
||||
from std.math import log, log2, log10, sqrt
|
||||
|
||||
|
||||
def main():
|
||||
var two = 2.0
|
||||
var ten = 10.0
|
||||
print(log2(sqrt(two)), log2(ten), log10(two), log(two))
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: can an Int and a Float64 be combined without a conversion?
|
||||
def main():
|
||||
var n = 10
|
||||
var x = 2.5
|
||||
print(n * x)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: does a Float64 product past the largest double give inf at run time, and inf - inf nan?
|
||||
def main():
|
||||
var big = 1.0e308
|
||||
var x = big * 10.0
|
||||
print(x, x - x)
|
||||
@@ -0,0 +1,21 @@
|
||||
# probe: a whole-number literal assigned to a Float64 variable, field, List element or Dict entry or key, or handed to a Float64 field by @fieldwise_init: is it a Float64?
|
||||
@fieldwise_init
|
||||
struct P(Copyable, Movable):
|
||||
var x: Float64
|
||||
var n: Int
|
||||
|
||||
|
||||
def main() raises:
|
||||
var f: Float64 = 0.0
|
||||
f = 1
|
||||
var p = P(2, 3)
|
||||
print(f, p.x, p.n)
|
||||
p.x = 4
|
||||
var l: List[Float64] = [0.5]
|
||||
l[0] = 5
|
||||
var d: Dict[String, Float64] = {}
|
||||
d["a"] = 6
|
||||
var k: Dict[Float64, Int] = {1: 7}
|
||||
k[2] = 8
|
||||
print(p.x, l, d["a"], k[1.0], k[2], len(k))
|
||||
print(k)
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: how accurate are ** with a fractional exponent, exp and sqrt on a Float64? (true values: 1.4142135623730951 2.718281828459045 0.36787944117144233 1.4142135623730951)
|
||||
from std.math import exp, sqrt
|
||||
|
||||
|
||||
def main():
|
||||
var two = 2.0
|
||||
var one = 1.0
|
||||
print(two ** 0.5, exp(one), exp(-one), sqrt(two))
|
||||
@@ -0,0 +1,7 @@
|
||||
# probe: what does a Float64 raised to a very large whole exponent do? (1.0000001 ** 1e7 is fine; ** 1e10 stops the program, and so does 0.5 ** 4503599627370497.0)
|
||||
def main():
|
||||
var b = 1.0000001
|
||||
var small = 1.0e7
|
||||
print(b ** small)
|
||||
var huge = 1.0e10
|
||||
print(b ** huge)
|
||||
@@ -0,0 +1,7 @@
|
||||
# probe: how is a Float64 raised to a whole power computed? (by squaring: 1.1 ** 20 is 6.727499949325609, not the correctly rounded 6.727499949325611)
|
||||
def main():
|
||||
var x = 2.5
|
||||
var y = 1.1
|
||||
var n = 3
|
||||
print(x ** 2, x ** 2.0, x ** 3.0, x ** -1.0, y ** 7, y ** 7.0, y ** 20, y ** 20.0)
|
||||
print(10.0 ** -3.0, 2.0 ** 10.0, y ** -3, x ** n, y ** Float64(n))
|
||||
@@ -0,0 +1,12 @@
|
||||
# probe: how does print format Float64 values?
|
||||
def main():
|
||||
var a = 0.1
|
||||
var b = 0.2
|
||||
print(a + b)
|
||||
print(1.0)
|
||||
print(Float64(3))
|
||||
print(1e20)
|
||||
print(1e-7)
|
||||
print(1.0 / 3.0)
|
||||
print(-0.0)
|
||||
print(123456789.125)
|
||||
@@ -0,0 +1,7 @@
|
||||
# probe: does round() keep the sign when the result is zero? (yes: round(-0.3) is -0.0)
|
||||
def main():
|
||||
var a = -0.3
|
||||
var b = -0.5
|
||||
var c = 0.3
|
||||
var d = -0.0
|
||||
print(round(a), round(b), round(c), round(d))
|
||||
@@ -0,0 +1,6 @@
|
||||
# probe: what does round() do on halves?
|
||||
def main():
|
||||
var a = 2.5
|
||||
var b = 3.5
|
||||
var c = -2.5
|
||||
print(round(a), round(b), round(c))
|
||||
@@ -0,0 +1,32 @@
|
||||
# probe: does Float64(atol(m)) doubled or halved e times give m * 2^e exactly? (yes, for m below 2^53: 0.1, 2^1000, a subnormal, -0.0, an overflow to inf, 1e-16, the double below 1)
|
||||
# stdin: 3602879701896397 -55 1 1000 1 -1074 -0 0 1 1024 -1 1024 2028240960365167 -104 4503599627370495 -52\n
|
||||
# expect-stdout: 0.1
|
||||
# expect-stdout: 1.0715086071862673e+301
|
||||
# expect-stdout: 5e-324
|
||||
# expect-stdout: -0.0
|
||||
# expect-stdout: inf
|
||||
# expect-stdout: -inf
|
||||
# expect-stdout: 1e-16
|
||||
# expect-stdout: 0.9999999999999998
|
||||
def scaled(m: String, e: String) raises -> Float64:
|
||||
var x = Float64(atol(m))
|
||||
if m == "-0":
|
||||
x = -x
|
||||
var k = atol(e)
|
||||
while k > 0:
|
||||
x *= 2.0
|
||||
k -= 1
|
||||
while k < 0:
|
||||
x *= 0.5
|
||||
k += 1
|
||||
return x
|
||||
|
||||
|
||||
def main() raises:
|
||||
var w = List[String]()
|
||||
for s in input().split(" "):
|
||||
w.append(String(s))
|
||||
var i = 0
|
||||
while i + 1 < len(w):
|
||||
print(scaled(w[i], w[i + 1]))
|
||||
i += 2
|
||||
@@ -0,0 +1,4 @@
|
||||
# probe: how are inf, -inf and nan printed after a run-time division by zero?
|
||||
def main():
|
||||
var z = 0.0
|
||||
print(1.0 / z, -1.0 / z, z / z)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: does Int(x) truncate toward zero?
|
||||
def main():
|
||||
var a = 3.9
|
||||
var b = -3.9
|
||||
print(Int(a), Int(b))
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: can a float literal stand left of a Float32 in a comparison? (no: 0.1 == x has no overload; left of a Float64 it does)
|
||||
# expect: compile-error
|
||||
def main():
|
||||
var x: Float32 = 0.1
|
||||
print(0.1 == x)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: what type does a float literal take beside a Float32? (the Float32: x == 0.1 is True for a Float32 x holding 0.1)
|
||||
def main():
|
||||
var x: Float32 = 0.1
|
||||
print(x == 0.1, Float64(x) == 0.1, x + 0.1, x * 3.0, 0.1 + x, 1.0 - x, x < 0.1, x / 3)
|
||||
print(Float32(0.1) >= x, x >= 0.1, 2 * x, x - 1)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: how does print format Float32 values?
|
||||
def main():
|
||||
var a = Float32(0.1)
|
||||
print(a)
|
||||
print(Float32(1) / Float32(3))
|
||||
@@ -0,0 +1,7 @@
|
||||
# probe: can a program have a variable at file scope? (no: "global variables are not supported")
|
||||
# expect: compile-error
|
||||
var counter = 0
|
||||
|
||||
|
||||
def main():
|
||||
print(counter)
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: does one request return both the listing and the program's output?
|
||||
# mode: both
|
||||
def add(a: Int, b: Int) -> Int:
|
||||
return a + b
|
||||
|
||||
|
||||
def main():
|
||||
print(add(2, 3))
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: the listing for a small function: labels, and source line numbers on instructions
|
||||
# mode: asm
|
||||
def add(a: Int, b: Int) -> Int:
|
||||
return a + b
|
||||
|
||||
|
||||
def main():
|
||||
print(add(2, 3))
|
||||
@@ -0,0 +1,10 @@
|
||||
# probe: the Compile button's request: -O0, line tables, listing and output in one answer
|
||||
# mode: both
|
||||
# args: -O0 --debug-level=line-tables
|
||||
# expect-stdout: 5
|
||||
def add(a: Int, b: Int) -> Int:
|
||||
return a + b
|
||||
|
||||
|
||||
def main():
|
||||
print(add(2, 3))
|
||||
@@ -0,0 +1,9 @@
|
||||
# probe: with -O0 and line tables, are source lines attached, and is the function list unchanged?
|
||||
# mode: asm
|
||||
# args: -O0 --debug-level=line-tables
|
||||
def add(a: Int, b: Int) -> Int:
|
||||
return a + b
|
||||
|
||||
|
||||
def main():
|
||||
print(add(2, 3))
|
||||
@@ -0,0 +1,9 @@
|
||||
# probe: with --debug-level=line-tables, does godbolt attach source lines to instructions (optimized build)?
|
||||
# mode: asm
|
||||
# args: --debug-level=line-tables
|
||||
def add(a: Int, b: Int) -> Int:
|
||||
return a + b
|
||||
|
||||
|
||||
def main():
|
||||
print(add(2, 3))
|
||||
@@ -0,0 +1,9 @@
|
||||
# probe: is -O0 accepted, and how does the listing change?
|
||||
# mode: asm
|
||||
# args: -O0
|
||||
def add(a: Int, b: Int) -> Int:
|
||||
return a + b
|
||||
|
||||
|
||||
def main():
|
||||
print(add(2, 3))
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: does the Compile button's request (compile and run in one) deliver stdin? (yes, and so does run mode: see input-lines for why two input() calls fail there)
|
||||
# mode: both
|
||||
# args: -O0 --debug-level=line-tables
|
||||
# stdin: 41\n
|
||||
# expect-stdout: 42
|
||||
def main() raises:
|
||||
var s = input()
|
||||
print(atol(s) + 1)
|
||||
@@ -0,0 +1,12 @@
|
||||
# probe: built with --target-cpu x86-64-v3, does the listing of String's ==, which the standard library compares with SIMD, use 256-bit AVX2 (ymm) and never AVX-512 (zmm), whichever of godbolt's machines answers? (without a target CPU, mojo build targets the machine it runs on, and on 2026-11-12 one of godbolt's answered library/mojo/local/demo.mojo with zmm)
|
||||
# mode: both
|
||||
# args: --target-cpu x86-64-v3
|
||||
# stdin: GATTACA GATTACA\n
|
||||
# expect-stdout: True
|
||||
def same(a: String, b: String) -> Bool:
|
||||
return a == b
|
||||
|
||||
|
||||
def main() raises:
|
||||
var w = input().split(" ")
|
||||
print(same(String(w[0]), String(w[1])))
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: what does input() do at the end of input?
|
||||
# stdin:
|
||||
def main() raises:
|
||||
var a = input()
|
||||
print("got [" + a + "]")
|
||||
@@ -0,0 +1,7 @@
|
||||
# probe: does input() read one line per call? (on a terminal, yes; on piped input the first call takes every line and the second raises EOF, which is what godbolt shows)
|
||||
# stdin: 42\nhello\n
|
||||
def main() raises:
|
||||
var a = input()
|
||||
var b = input()
|
||||
print(atol(a) + 1)
|
||||
print(b)
|
||||
@@ -0,0 +1,6 @@
|
||||
# probe: what does atol raise on text that is not a number?
|
||||
def main():
|
||||
try:
|
||||
print(atol("x1"))
|
||||
except e:
|
||||
print("error:", e)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: on piped input, what does one input() return? (the first line, without its line break and with its spaces; the rest of the pipe is read and dropped, so a second input() raises EOF: see input-lines)
|
||||
# stdin: a b\nsecond\n
|
||||
# expect-stdout: [a b]
|
||||
def main() raises:
|
||||
print("[" + input() + "]")
|
||||
@@ -0,0 +1,6 @@
|
||||
# probe: what does a conversion to a narrower whole-number type keep? (the low bits: Int8(300) is 44, UInt8(-1) is 255)
|
||||
# expect-stdout: 44 44 255 4464
|
||||
def main():
|
||||
var n = 300
|
||||
var m = -1
|
||||
print(Int8(n), UInt8(n), UInt8(m), Int16(70000))
|
||||
@@ -0,0 +1,6 @@
|
||||
# probe: what happens on integer division by zero at run time (output, exit code)?
|
||||
def main():
|
||||
var z = 0
|
||||
print("before")
|
||||
print(7 // z)
|
||||
print("after")
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: does // floor toward negative infinity on Int?
|
||||
def main():
|
||||
var a = 7
|
||||
var b = 2
|
||||
print(a // b, (-a) // b, a // (-b), (-a) // (-b))
|
||||
@@ -0,0 +1,4 @@
|
||||
# probe: is a literal too big for Int a compile error?
|
||||
def main():
|
||||
var x: Int = 2 ** 64
|
||||
print(x)
|
||||
@@ -0,0 +1,12 @@
|
||||
# probe: what do % and // by zero do on Int and Int8 at run time? (0, and the program goes on; the notation stops)
|
||||
# expect-stdout: before
|
||||
# expect-stdout: 0 0 0
|
||||
# expect-stdout: 0 0
|
||||
# expect-stdout: after
|
||||
def main():
|
||||
var z = 0
|
||||
var b: Int8 = 0
|
||||
print("before")
|
||||
print(7 % z, -7 % z, 7 // z)
|
||||
print(Int8(7) % b, Int8(7) // b)
|
||||
print("after")
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: what sign does % give with negative operands?
|
||||
def main():
|
||||
var a = 7
|
||||
var m = 3
|
||||
print(a % m, (-a) % m, a % (-m), (-a) % (-m))
|
||||
@@ -0,0 +1,4 @@
|
||||
# probe: is Int(0.1) accepted? (no: a float literal is not made an Int at compile time; Int(x) of a Float64 value drops the fraction)
|
||||
# expect: compile-error
|
||||
def main():
|
||||
print(Int(0.1))
|
||||
@@ -0,0 +1,7 @@
|
||||
# probe: what is Int.MAX, and what is Int.MAX + 1 at run time?
|
||||
def main():
|
||||
var m = Int.MAX
|
||||
print(m)
|
||||
m += 1
|
||||
print(m)
|
||||
print(Int.MIN)
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: does Int8 wrap on overflow at run time?
|
||||
def main():
|
||||
var x: Int8 = 127
|
||||
x += 1
|
||||
print(x)
|
||||
var y: Int8 = -128
|
||||
y -= 1
|
||||
print(y)
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: what do ** and shifts give on Int at run time?
|
||||
def main():
|
||||
var b = 2
|
||||
var e = 10
|
||||
print(b ** e)
|
||||
var one = 1
|
||||
var neg = -16
|
||||
print(one << 3, neg >> 2)
|
||||
@@ -0,0 +1,6 @@
|
||||
# probe: what does / do on two Int values, positive and negative?
|
||||
def main():
|
||||
var a = 7
|
||||
var b = 2
|
||||
print(a / b)
|
||||
print(-a / b)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: does UInt8 wrap below zero?
|
||||
def main():
|
||||
var u: UInt8 = 0
|
||||
u -= 1
|
||||
print(u)
|
||||
@@ -0,0 +1,6 @@
|
||||
# probe: is parallelize importable from std.algorithm in 1.1.0?
|
||||
from std.algorithm import parallelize
|
||||
|
||||
|
||||
def main():
|
||||
print("parallelize imported")
|
||||
@@ -0,0 +1,6 @@
|
||||
# probe: is vectorize importable from std.algorithm in 1.1.0?
|
||||
from std.algorithm import vectorize
|
||||
|
||||
|
||||
def main():
|
||||
print("vectorize imported")
|
||||
@@ -0,0 +1,24 @@
|
||||
# probe: the manual's ASAP example: which values are destroyed, in what order, inside one expression?
|
||||
@fieldwise_init
|
||||
struct Number(Writable):
|
||||
var value: Int
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print(t"Destroying Number(value={self.value})")
|
||||
|
||||
def __add__(self, other: Number) -> Number:
|
||||
return Number(self.value + other.value)
|
||||
|
||||
def __sub__(self, other: Number) -> Number:
|
||||
return Number(self.value - other.value)
|
||||
|
||||
def __mul__(self, other: Number) -> Number:
|
||||
return Number(self.value * other.value)
|
||||
|
||||
|
||||
def main():
|
||||
var a = Number(1)
|
||||
var b = Number(2)
|
||||
var c = Number(3)
|
||||
var d = Number(4)
|
||||
print(a + (b - c) * d)
|
||||
@@ -0,0 +1,31 @@
|
||||
# probe: Stage 4 prediction: an unread value dies after its declaration; a value borrowed by print lives until print returns
|
||||
# expect-stdout: init a
|
||||
# expect-stdout: init b
|
||||
# expect-stdout: bye b
|
||||
# expect-stdout: using a
|
||||
# expect-stdout: bye a
|
||||
# expect-stdout: end
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.n = copy.n + "2"
|
||||
print("copy", copy.n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def main():
|
||||
var a = L("a")
|
||||
var b = L("b")
|
||||
print("using", a.n)
|
||||
print("end")
|
||||
@@ -0,0 +1,31 @@
|
||||
# probe: used in both branches: does it die right after its last use in each?
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def run(flag: Bool):
|
||||
var a = L("a")
|
||||
if flag:
|
||||
print("then uses", a.n)
|
||||
print("then after use")
|
||||
else:
|
||||
print("else uses", a.n)
|
||||
print("else after use")
|
||||
print("after if")
|
||||
|
||||
|
||||
def main():
|
||||
run(True)
|
||||
print("---")
|
||||
run(False)
|
||||
@@ -0,0 +1,26 @@
|
||||
# probe: declared inside a branch and used there: does it die within the branch?
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def run(flag: Bool):
|
||||
if flag:
|
||||
var a = L("a")
|
||||
print("then uses", a.n)
|
||||
print("then after use")
|
||||
print("after if")
|
||||
|
||||
|
||||
def main():
|
||||
run(True)
|
||||
@@ -0,0 +1,31 @@
|
||||
# probe: used in the then branch only: in the else branch, where does it die (before the else body, after it, or after the if)?
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def run(flag: Bool):
|
||||
var a = L("a")
|
||||
print("before if")
|
||||
if flag:
|
||||
print("then uses", a.n)
|
||||
print("then after use")
|
||||
else:
|
||||
print("else body")
|
||||
print("after if")
|
||||
|
||||
|
||||
def main():
|
||||
run(True)
|
||||
print("---")
|
||||
run(False)
|
||||
@@ -0,0 +1,25 @@
|
||||
# probe: which lifecycle methods run for .copy(), ^, and the end of main?
|
||||
struct Loud(Copyable, Movable):
|
||||
var name: String
|
||||
|
||||
def __init__(out self, name: String):
|
||||
self.name = name
|
||||
print("init", name)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.name = copy.name + "'"
|
||||
print("copy", copy.name)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.name = move.name^
|
||||
print("move", self.name)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("destroy", self.name)
|
||||
|
||||
|
||||
def main():
|
||||
var a = Loud("x")
|
||||
var b = a.copy()
|
||||
var c = a^
|
||||
print("holding", b.name, c.name)
|
||||
@@ -0,0 +1,29 @@
|
||||
# probe: an early return: is the value destroyed before the return on that path?
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def run(flag: Bool) -> Int:
|
||||
var a = L("a")
|
||||
if flag:
|
||||
print("returning early")
|
||||
return 1
|
||||
print("late uses", a.n)
|
||||
return 2
|
||||
|
||||
|
||||
def main():
|
||||
print(run(True))
|
||||
print("---")
|
||||
print(run(False))
|
||||
@@ -0,0 +1,32 @@
|
||||
# probe: in what order are a struct's fields destroyed?
|
||||
struct Loud(Copyable, Movable):
|
||||
var name: String
|
||||
|
||||
def __init__(out self, name: String):
|
||||
self.name = name
|
||||
print("init", name)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.name = copy.name + "'"
|
||||
print("copy", copy.name)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.name = move.name^
|
||||
print("move", self.name)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("destroy", self.name)
|
||||
|
||||
|
||||
struct Pair(Movable):
|
||||
var first: Loud
|
||||
var second: Loud
|
||||
|
||||
def __init__(out self, var first: Loud, var second: Loud):
|
||||
self.first = first^
|
||||
self.second = second^
|
||||
|
||||
|
||||
def main():
|
||||
var p = Pair(Loud("x"), Loud("y"))
|
||||
print("made pair", p.first.name, p.second.name)
|
||||
@@ -0,0 +1,26 @@
|
||||
# probe: is a variable destroyed at its last use, before later statements run?
|
||||
struct Loud(Copyable, Movable):
|
||||
var name: String
|
||||
|
||||
def __init__(out self, name: String):
|
||||
self.name = name
|
||||
print("init", name)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.name = copy.name + "'"
|
||||
print("copy", copy.name)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.name = move.name^
|
||||
print("move", self.name)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("destroy", self.name)
|
||||
|
||||
|
||||
def main():
|
||||
var a = Loud("a")
|
||||
print("a made")
|
||||
var b = Loud("b")
|
||||
print("b made, a used last at:", a.name)
|
||||
print("end of main")
|
||||
@@ -0,0 +1,25 @@
|
||||
# probe: when a List of values is destroyed, in what order do its elements go?
|
||||
struct Loud(Copyable, Movable):
|
||||
var name: String
|
||||
|
||||
def __init__(out self, name: String):
|
||||
self.name = name
|
||||
print("init", name)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.name = copy.name + "'"
|
||||
print("copy", copy.name)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.name = move.name^
|
||||
print("move", self.name)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("destroy", self.name)
|
||||
|
||||
|
||||
def main():
|
||||
var l = List[Loud]()
|
||||
l.append(Loud("one"))
|
||||
l.append(Loud("two"))
|
||||
print("list built", len(l))
|
||||
@@ -0,0 +1,22 @@
|
||||
# probe: declared inside a loop body: does each pass destroy its own value at its last use?
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def main():
|
||||
for i in range(2):
|
||||
var a = L(String(i))
|
||||
print("pass uses", a.n)
|
||||
print("pass after use")
|
||||
print("after loop")
|
||||
@@ -0,0 +1,22 @@
|
||||
# probe: declared before a loop, read in every pass: does it die after the loop, or in the last pass?
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def main():
|
||||
var a = L("a")
|
||||
for i in range(2):
|
||||
print("pass", i, a.n)
|
||||
print("pass after use", i)
|
||||
print("after loop")
|
||||
@@ -0,0 +1,22 @@
|
||||
# probe: read before a loop and never after: does it die before the loop starts?
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def main():
|
||||
var a = L("a")
|
||||
print("uses", a.n)
|
||||
for i in range(2):
|
||||
print("pass", i)
|
||||
print("after loop")
|
||||
@@ -0,0 +1,37 @@
|
||||
# probe: Stage 4 prediction: x^ into a var argument runs no constructor; into a new variable the move constructor runs
|
||||
# expect-stdout: init a
|
||||
# expect-stdout: copy a
|
||||
# expect-stdout: took a2
|
||||
# expect-stdout: bye a2
|
||||
# expect-stdout: move a
|
||||
# expect-stdout: kept a
|
||||
# expect-stdout: bye a
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.n = copy.n + "2"
|
||||
print("copy", copy.n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def take(var t: L):
|
||||
print("took", t.n)
|
||||
|
||||
|
||||
def main():
|
||||
var a = L("a")
|
||||
var c = a.copy()
|
||||
take(c^)
|
||||
var d = a^
|
||||
print("kept", d.n)
|
||||
@@ -0,0 +1,34 @@
|
||||
# probe: moved out on one path only: on the other path, when does it die?
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def take(var t: L):
|
||||
print("took", t.n)
|
||||
|
||||
|
||||
def run(flag: Bool):
|
||||
var a = L("a")
|
||||
if flag:
|
||||
take(a^)
|
||||
print("then after take")
|
||||
else:
|
||||
print("else body")
|
||||
print("after if")
|
||||
|
||||
|
||||
def main():
|
||||
run(True)
|
||||
print("---")
|
||||
run(False)
|
||||
@@ -0,0 +1,9 @@
|
||||
# probe: a mut argument changes the caller's variable
|
||||
def bump(mut x: Int):
|
||||
x += 1
|
||||
|
||||
|
||||
def main():
|
||||
var n = 1
|
||||
bump(n)
|
||||
print(n)
|
||||
@@ -0,0 +1,22 @@
|
||||
# probe: reassigned in every pass and read after the loop: when does each old value die?
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def main():
|
||||
var a = L("0")
|
||||
for i in range(2):
|
||||
a = L(String(i + 1))
|
||||
print("pass", i)
|
||||
print("after loop", a.n)
|
||||
@@ -0,0 +1,24 @@
|
||||
# probe: when a variable is reassigned, when is the old value destroyed?
|
||||
struct Loud(Copyable, Movable):
|
||||
var name: String
|
||||
|
||||
def __init__(out self, name: String):
|
||||
self.name = name
|
||||
print("init", name)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.name = copy.name + "'"
|
||||
print("copy", copy.name)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.name = move.name^
|
||||
print("move", self.name)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("destroy", self.name)
|
||||
|
||||
|
||||
def main():
|
||||
var a = Loud("first")
|
||||
a = Loud("second")
|
||||
print("after reassign:", a.name)
|
||||
@@ -0,0 +1,29 @@
|
||||
# probe: passing to a var argument: with ^, and with .copy()
|
||||
struct Loud(Copyable, Movable):
|
||||
var name: String
|
||||
|
||||
def __init__(out self, name: String):
|
||||
self.name = name
|
||||
print("init", name)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.name = copy.name + "'"
|
||||
print("copy", copy.name)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.name = move.name^
|
||||
print("move", self.name)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("destroy", self.name)
|
||||
|
||||
|
||||
def take(var t: Loud):
|
||||
print("took", t.name)
|
||||
|
||||
|
||||
def main():
|
||||
var a = Loud("a")
|
||||
take(a.copy())
|
||||
take(a^)
|
||||
print("end")
|
||||
@@ -0,0 +1,7 @@
|
||||
# probe: what happens when a List is indexed past its end at run time?
|
||||
def main():
|
||||
var l = [1, 2, 3]
|
||||
var i = 3
|
||||
print("before")
|
||||
print(l[i])
|
||||
print("after")
|
||||
@@ -0,0 +1,36 @@
|
||||
# probe: a list in brackets assigned to a List variable, a struct field, a List element or a Dict entry: is it a List (append works), and do its items take the element type?
|
||||
@fieldwise_init
|
||||
struct Box(Copyable, Movable):
|
||||
var items: List[Int]
|
||||
var weights: List[Float64]
|
||||
|
||||
|
||||
def main() raises:
|
||||
var a = List[Int]()
|
||||
a = [1, 2]
|
||||
a.append(3)
|
||||
print(a)
|
||||
var f = List[Float64]()
|
||||
f = [1, 2]
|
||||
f.append(3)
|
||||
print(f)
|
||||
var b = Box(List[Int](), List[Float64]())
|
||||
b.items = [4, 5]
|
||||
b.items.append(6)
|
||||
b.weights = [7, 8]
|
||||
print(b.items, b.weights)
|
||||
var g: List[List[Int]] = [[1], [2]]
|
||||
g[0] = [9, 9]
|
||||
g[0].append(10)
|
||||
g.append([11])
|
||||
print(g)
|
||||
var d = Dict[String, List[Int]]()
|
||||
d["k"] = [1]
|
||||
d["k"].append(2)
|
||||
print(d["k"])
|
||||
var h: Dict[String, List[Float64]] = {"x": [1, 2]}
|
||||
h["x"].append(3)
|
||||
print(h["x"])
|
||||
var c = Box([1], [2])
|
||||
c.items.append(3)
|
||||
print(c.items, c.weights)
|
||||
@@ -0,0 +1,31 @@
|
||||
# probe: where does a list in brackets become a List? (an annotated var, an argument, a returned value, an assignment, a Dict value)
|
||||
# expect-stdout: 6
|
||||
# expect-stdout: [1, 2, 3]
|
||||
# expect-stdout: [9, 9] 2
|
||||
# expect-stdout: [2, 4]
|
||||
# expect-stdout: [[1, 2], [3]]
|
||||
# expect-stdout: [1.0, 2.5]
|
||||
def total(xs: List[Int]) -> Int:
|
||||
var t = 0
|
||||
for x in xs:
|
||||
t += x
|
||||
return t
|
||||
|
||||
|
||||
def evens() -> List[Int]:
|
||||
return [2, 4]
|
||||
|
||||
|
||||
def main() raises:
|
||||
print(total([1, 2, 3]))
|
||||
var a: List[Int] = [1, 2, 3]
|
||||
print(a)
|
||||
a = [9, 9]
|
||||
var g: Dict[Int, List[Int]] = {}
|
||||
g[0] = [1, 2]
|
||||
print(a, len(g[0]))
|
||||
print(evens())
|
||||
var c: List[List[Int]] = [[1, 2], [3]]
|
||||
print(c)
|
||||
var f: List[Float64] = [1, 2.5]
|
||||
print(f)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: is a run-time negative index allowed on a List?
|
||||
def main():
|
||||
var l = [1, 2, 3]
|
||||
var i = -1
|
||||
print(l[i])
|
||||
@@ -0,0 +1,9 @@
|
||||
# probe: append, sort, pop and len on a List
|
||||
def main():
|
||||
var l = [3, 1, 2]
|
||||
l.append(5)
|
||||
print(len(l))
|
||||
sort(l)
|
||||
print(l)
|
||||
print(l.pop())
|
||||
print(l)
|
||||
@@ -0,0 +1,31 @@
|
||||
# probe: List[T](l[lo:hi]) is a new List holding copies of the items, not a view of l: changes to either afterwards stay apart; empty, whole and open-ended slices
|
||||
def main():
|
||||
var l: List[Int] = [1, 2, 3, 4, 5]
|
||||
var m = List[Int](l[1:3])
|
||||
print(m, len(m))
|
||||
l[1] = 20
|
||||
m.append(9)
|
||||
print(l, m)
|
||||
var e = List[Int](l[2:2])
|
||||
print(e, len(e))
|
||||
e.append(7)
|
||||
print(e)
|
||||
var w = List[Int](l[0:len(l)])
|
||||
print(w)
|
||||
var s: List[String] = ["a", "b", "c"]
|
||||
var t = List[String](s[1:3])
|
||||
s[1] = "z"
|
||||
print(t, s)
|
||||
var g: List[List[Int]] = [[1], [2, 3]]
|
||||
var h = List[List[Int]](g[0:2])
|
||||
g[1].append(4)
|
||||
h[0].append(5)
|
||||
print(g, h)
|
||||
var fl: List[Float64] = [1.5, 2.5]
|
||||
print(List[Float64](fl[0:1]))
|
||||
var x = List[Int](l[:2])
|
||||
var y = List[Int](l[3:])
|
||||
print(x, y)
|
||||
var _value1: List[Int] = List[Int](l[1:4])
|
||||
l = _value1^
|
||||
print(l)
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: what happens when a List is sliced past its end (List[Int](l[1:9]) on a List of three)?
|
||||
def main():
|
||||
var l: List[Int] = [1, 2, 3]
|
||||
var hi = 9
|
||||
print("before")
|
||||
var m = List[Int](l[1:hi])
|
||||
print(m)
|
||||
print("after")
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: what happens when a List is sliced from a negative start (List[Int](l[-1:2]))?
|
||||
def main():
|
||||
var l: List[Int] = [1, 2, 3]
|
||||
var lo = -1
|
||||
print("before")
|
||||
var m = List[Int](l[lo:2])
|
||||
print(m)
|
||||
print("after")
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: what happens when a List is sliced with its start past its end (List[Int](l[2:1]))?
|
||||
def main():
|
||||
var l: List[Int] = [1, 2, 3]
|
||||
var lo = 2
|
||||
print("before")
|
||||
var m = List[Int](l[lo:1])
|
||||
print(m)
|
||||
print("after")
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: is List[Int](1, 2, 3) a constructor? (no: only brackets make a list of values)
|
||||
# expect: compile-error
|
||||
def main():
|
||||
var xs = List[Int](1, 2, 3)
|
||||
print(xs)
|
||||
Loaded 100 of 1591 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user