core — examples
← all topics · 500 examples · page 4 of 10 · raw source ↓
Runs `go` → "b"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_ControlFlow.cs::NullCoalescing_ChainPicksFirstNonNull topic: core status: verified
// verified behavior: Test.go(...) == "b"
namespace Test
func pick(a: string?, b: string?) -> string {
return a ?? b ?? "fallback"
}
func go() -> string { return pick(nil, "b") }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_ControlFlow.cs::NullConditional_OnPresentReadsMember topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let s: string? = "hello"
return s?.Length ?? 0
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_ControlFlow.cs::Ternary_DrivesCompoundAssign topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
var n = 4
n += n > 0 ? 4 : -4
return n
}Runs `go` → "zero"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_ControlFlow.cs::Ternary_Nested topic: core status: verified
// verified behavior: Test.go(...) == "zero"
namespace Test
func sign(n: int) -> string {
return n > 0 ? "pos" : (n < 0 ? "neg" : "zero")
}
func go() -> string { return sign(0) }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_ControlFlow.cs::Ternary_SelectsBranch topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let x = -9
return x > 0 ? x : 0 - x
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_ControlFlow.cs::While_AccumulatesSum topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
var i = 1
var total = 0
while i <= 5 {
total += i
i += 1
}
return total
}Runs `go` → "aaa"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_ControlFlow.cs::While_BuildsString topic: core status: verified
// verified behavior: Test.go(...) == "aaa"
namespace Test
func go() -> string {
var s = ""
var n = 3
while n > 0 {
s = s + "a"
n -= 1
}
return s
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_ControlFlow.cs::While_Countdown topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
var n = 10
while n > 0 { n -= 1 }
return n
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_ControlFlow.cs::While_FalseCondition_NeverRuns topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
var n = 99
while false { n = 0 }
return n
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_ControlFlow.cs::While_FlagBasedExit topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func firstSquareOver(limit: int) -> int {
var i = 0
var done = false
var answer = -1
while !done {
if i * i > limit {
answer = i
done = true
}
i += 1
}
return answer
}
func go() -> int { return firstSquareOver(10) }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_ControlFlow.cs::While_NestedMultiplicationTable topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
var i = 1
var sum = 0
while i <= 5 {
var j = 1
while j <= 5 {
sum += i * j
j += 1
}
i += 1
}
return sum
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_ControlFlow.cs::While_TrueWithEarlyReturn topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
var n = 0
while true {
n += 2
if n >= 8 { return n }
}
return -1
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Data.cs::Closure_Accumulator topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
var sum = 0
let addTo = func(value: int) {
sum = sum + value
}
for i in 1..6 {
addTo(i)
}
return sum
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Data.cs::Closure_MutableCapture topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
var total = 0
let inc = func() {
total = total + 1
}
inc()
inc()
return total
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Data.cs::Collection_ForInSum topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let xs = [1, 2, 3, 4, 5]
var total = 0
for x in xs {
total += x
}
return total
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Data.cs::Collection_ListCount topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let xs = [1, 2, 3, 4]
return xs.Count
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Data.cs::Collection_ListLiteralIndex topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let xs = [10, 20, 30]
return xs[0] + xs[1] + xs[2]
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Data.cs::ExpressionBodied_Function topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func double(x: int) -> int = x * 2
func go() -> int {
return double(10)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Data.cs::FunctionPointer_CallThroughLocal topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func add(a: int, b: int) -> int {
return a + b
}
func go() -> int {
let p = &add
return p(3, 4)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Data.cs::Lambda_ArrowExpression topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func apply(x: int, f: Func<int, int>) -> int = f(x)
func go() -> int {
return apply(6, (x) => x * x)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Data.cs::NullCoalescing_Fallback topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func find(id: int) -> string? {
if id == 0 { return nil }
return "x"
}
func go() -> int {
let s = find(0) ?? "fallback"
return s.Length
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Data.cs::NullConditional_Access topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let s: string? = nil
return s?.Length ?? 0
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Data.cs::Tuple_Construction topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let t = (3, 4)
return t.Item1 + t.Item2
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Data.cs::Tuple_DestructuringInLet topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func swap(a: int, b: int) -> (int, int) {
return (b, a)
}
func go() -> int {
let (x, y) = swap(2, 3)
return x + y
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::LetElse_BindsPresentValue topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func lookup(id: int) -> int? {
if id == 0 { return nil }
return 50
}
func go() -> int {
let v = lookup(3) else { return -1 }
return v
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::LetElse_ChainedGuards topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func first(id: int) -> int? {
if id < 0 { return nil }
return 10
}
func second(n: int) -> int? {
if n == 0 { return nil }
return n + 5
}
func go() -> int {
let a = first(2) else { return -1 }
let b = second(a) else { return -2 }
return b
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::LetElse_DivertsOnNil topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func lookup(id: int) -> int? {
if id == 0 { return nil }
return 50
}
func go() -> int {
let v = lookup(0) else { return -99 }
return v
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::LetElse_SecondGuardDiverts topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func first(id: int) -> int? {
return 0
}
func second(n: int) -> int? {
if n == 0 { return nil }
return n + 5
}
func go() -> int {
let a = first(2) else { return -1 }
let b = second(a) else { return -2 }
return b
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Throw_CaughtByCallerBoundary topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func risky(n: int) -> int {
if n < 0 { throw InvalidOperationException("negative") }
return n
}
func go() -> int {
var result = 0
try {
result = risky(-1)
} catch (Exception e) {
result = -42
}
return result
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::TryCatch_BareCatchSwallows topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
var result = 0
try {
result = int.Parse("xyz")
} catch {
result = -7
}
return result
}Runs `go` → "inner"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::TryCatch_Nested_InnerHandlesFirst topic: core status: verified
// verified behavior: Test.go(...) == "inner"
namespace Test
func go() -> string {
var tag = "none"
try {
try {
throw FormatException("x")
} catch (FormatException e) {
tag = "inner"
}
} catch (Exception e) {
tag = "outer"
}
return tag
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::TryCatch_RecoversFromParseFailure topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
var result = 0
try {
result = int.Parse("notanumber")
} catch (FormatException e) {
result = -1
}
return result
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::TryCatch_SuccessPathSkipsCatch topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
var result = 0
try {
result = int.Parse("123")
} catch (FormatException e) {
result = -1
}
return result
}Runs `go` → "negative"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::TryCatch_TypedCatchBindsMessage topic: core status: verified
// verified behavior: Test.go(...) == "negative"
namespace Test
func go() -> string {
var msg = "none"
try {
throw InvalidOperationException("negative")
} catch (InvalidOperationException e) {
msg = e.Message
}
return msg
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Misc.cs::Bcl_ListAddSum topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let xs = List<int>()
xs.Add(1)
xs.Add(2)
xs.Add(3)
var total = 0
for x in xs {
total += x
}
return total
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Misc.cs::Linq_Sum topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let xs = [1, 2, 3, 4]
return xs.Sum()
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Misc.cs::NullableValueLetElse topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func find(id: int) -> int? {
if id == 0 {
return nil
}
return id * 10
}
func lookup(id: int) -> int {
let v = find(id) else {
return 0 - 1
}
return v
}
func go() -> int {
return lookup(5) + lookup(0)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Misc.cs::OutParam_TryParse topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
if int.TryParse("77", out var n) {
return n
}
return 0
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Misc.cs::Pointer_ByRefIntMutation topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func addTen(x: *int) {
x += 10
}
func go() -> int {
var n = 5
addTen(&n)
return n
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Misc.cs::Range_IndexFromEnd topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let xs = [10, 20, 30]
return xs[^1]
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Misc.cs::Range_IndexFromEnd_Second topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let xs = [10, 20, 30]
return xs[^2]
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Misc.cs::Recursion_Factorial topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func fact(n: int) -> int {
if n <= 1 {
return 1
}
return n * fact(n - 1)
}
func go() -> int {
return fact(5)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Misc.cs::Recursion_Fibonacci topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func fib(n: int) -> int {
if n < 2 {
return n
}
return fib(n - 1) + fib(n - 2)
}
func go() -> int {
return fib(7)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Misc.cs::TryCatch_ConvertsException topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
var result = 0
try {
result = int.Parse("not a number")
} catch (Exception e) {
result = 0 - 1
}
return result
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Misc.cs::TryCatch_Succeeds topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
var result = 0
try {
result = int.Parse("123")
} catch {
result = 0 - 1
}
return result
}Runs `go` → 3
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Numerics.cs::Algo_CountSetViaModulo topic: core status: verified
// verified behavior: Test.go(...) == 3
namespace Test
func countMultiples(limit: int, of: int) -> int {
var count = 0
for i in 1..limit {
if i % of == 0 { count += 1 }
}
return count
}
func go() -> int { return countMultiples(10, 3) }Runs `go` → 55
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Numerics.cs::Algo_FibonacciIterative topic: core status: verified
// verified behavior: Test.go(...) == 55
namespace Test
func fib(n: int) -> int {
var a = 0
var b = 1
for i in 0..n {
let t = a + b
a = b
b = t
}
return a
}
func go() -> int { return fib(10) }Runs `go` → 6
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Numerics.cs::Algo_GcdEuclid topic: core status: verified
// verified behavior: Test.go(...) == 6
namespace Test
func gcd(a: int, b: int) -> int {
var x = a
var y = b
while y != 0 {
let t = y
y = x % y
x = t
}
return x
}
func go() -> int { return gcd(48, 18) }Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Numerics.cs::Algo_IsPrime topic: core status: verified
// verified behavior: Test.go(...) == true
namespace Test
func isPrime(n: int) -> bool {
if n < 2 { return false }
var d = 2
while d * d <= n {
if n % d == 0 { return false }
d += 1
}
return true
}
func go() -> bool { return isPrime(97) }Runs `go` → 243
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Numerics.cs::Algo_PowerByMultiplication topic: core status: verified
// verified behavior: Test.go(...) == 243
namespace Test
func ipow(b: int, exp: int) -> int {
var result = 1
for i in 0..exp {
result *= b
}
return result
}
func go() -> int { return ipow(3, 5) }