core — examples
← all topics · 698 examples · page 8 of 14 · raw source ↓
ILEmitterTests_Returns__Returns_Clause_Sees_Cross_Method_Resolution_With_DataType
core runnable verifiedRuns `go` → 2
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Returns.cs::Returns_Clause_Sees_Cross_Method_Resolution_With_DataType topic: core status: verified
// verified behavior: Test.go(...) == 2
namespace Test
class Counter {
returns Counter
n: int
init(n: int) { self.n = n }
func inc() { return Counter(self.n + 1) }
func twice() { return self.inc().inc() }
}
func go() -> int { return Counter(0).twice().n }Runs `go` → 11
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Returns.cs::Returns_Is_Still_Valid_Identifier_Elsewhere topic: core status: verified
// verified behavior: Test.go(...) == 11
namespace Test
func go(returns: int) -> int { return returns + 1 }Runs `go` → 9
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::C02_GenericInterface_TwoParams topic: core status: verified
// verified behavior: Test.go(...) == 9
namespace Test
interface IMap<K, V> { func get(k: K) -> V }
class One<K, V> : IMap<K, V> {
val: V
init(v: V) { self.val = v }
func get(k: K) -> V = self.val
}
func use(m: IMap<string, int>) -> int = m.get("x")
func go() -> int {
let o = One<string, int>(9)
return use(o)
}Runs `go` → 13
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::C04_MultiParam_ReturnsEach topic: core status: verified
// verified behavior: Test.go(...) == 13
namespace Test
interface IPair<A, B> {
func left() -> A
func right() -> B
}
class P<A, B> : IPair<A, B> {
a: A
b: B
init(x: A, y: B) { self.a = x self.b = y }
func left() -> A = self.a
func right() -> B = self.b
}
func go() -> int {
let p = P<int, int>(3, 10)
let ip: IPair<int, int> = p
return ip.left() + ip.right()
}Runs `go` → 15
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::H10_UserFunc_TakesFuncParam topic: core status: verified
// verified behavior: Test.go(...) == 15
namespace Test
func apply(f: Func<int, int>, x: int) -> int = f(x)
func go() -> int = apply((n) => n + 5, 10)Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StringEquality.cs::ChainedEquality topic: core status: verified
// verified behavior: Test.go(...) == true
func go() -> bool {
let a = "a"
let b = "a"
let c = "a"
return a == b && b == c
}Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StringEquality.cs::CharComparisonNotEqual topic: core status: verified
// verified behavior: Test.go(...) == true
func go() -> bool {
let s = "abc"
return s[0] != 'z'
}Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StringEquality.cs::CharComparisonStillCeq topic: core status: verified
// verified behavior: Test.go(...) == true
func go() -> bool {
let s = "a b"
return s[1] == ' '
}Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StringEquality.cs::ConcatEqualsLiteral topic: core status: verified
// verified behavior: Test.go(...) == true
func go() -> bool {
let a = "ab"
let b = "a" + "b"
return a == b
}Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StringEquality.cs::EmptyStringEquality topic: core status: verified
// verified behavior: Test.go(...) == true
func go() -> bool {
let a = ""
let b = "x".Substring(0, 0)
return a == b
}Runs `go` → false
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StringEquality.cs::EqualityFeedingReturn topic: core status: verified
// verified behavior: Test.go(...) == false
func go() -> bool {
let a = "abc"
let b = "abd"
return a == b
}Runs `go` → 1
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StringEquality.cs::EqualityInIfCondition topic: core status: verified
// verified behavior: Test.go(...) == 1
func classify(s: string) -> int {
if s == "fail" { return 1 }
return 0
}
func go() -> int = classify("fail")Runs `go` → 0
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StringEquality.cs::EqualityInIfCondition_NotTaken topic: core status: verified
// verified behavior: Test.go(...) == 0
func classify(s: string) -> int {
if s == "fail" { return 1 }
return 0
}
func go() -> int = classify("ok")Runs `go` → 3
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StringEquality.cs::EqualityInWhileGuard topic: core status: verified
// verified behavior: Test.go(...) == 3
func go() -> int {
var n = 0
var s = "go"
while s == "go" {
n += 1
if n >= 3 { s = "stop" }
}
return n
}Runs `go` → false
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StringEquality.cs::NotEqual_False topic: core status: verified
// verified behavior: Test.go(...) == false
func go() -> bool {
let a = "x"
let b = "x" + ""
return a != b
}Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StringEquality.cs::NotEqual_True topic: core status: verified
// verified behavior: Test.go(...) == true
func go() -> bool {
let a = "x"
let b = "y"
return a != b
}Runs `go` → false
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StringEquality.cs::ParamEqualsLiteral_False topic: core status: verified
// verified behavior: Test.go(...) == false
func eq(s: string) -> bool = s == "hello"
func go() -> bool = eq("world")Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StringEquality.cs::ParamEqualsLiteral_True topic: core status: verified
// verified behavior: Test.go(...) == true
func eq(s: string) -> bool = s == "hello"
func go() -> bool = eq("hello")Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StringEquality.cs::SubstringEqualsLiteral topic: core status: verified
// verified behavior: Test.go(...) == true
func go() -> bool {
let s = "hello"
let h = s.Substring(0, 3)
return h == "hel"
}Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StringEquality.cs::TwoRuntimeStringsEqual topic: core status: verified
// verified behavior: Test.go(...) == true
func go() -> bool {
let a = "foo" + "bar"
let b = "foob" + "ar"
return a == b
}Runs `go` → 11
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::Task_Is_Still_Valid_Identifier_Elsewhere topic: core status: verified
// verified behavior: Test.go(...) == 11
namespace Test
func go(task: int) -> int { return task + 1 }Runs `abs` → 0
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Absolute_Value_Pin topic: core status: verified
// verified behavior: Test.abs(...) == 0
namespace Test
func abs(n: int) -> int {
if n < 0 {
return -n
}
return n
}Runs `test` → 30
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Block_Scoped_Let_Visible_After_Block topic: core status: verified
// verified behavior: Test.test(...) == 30
namespace Test
func test() -> int {
var total = 0
var i = 0
while i < 3 {
let inner = i * 10
total = total + inner
i = i + 1
}
return total
}Runs `choose` → 200
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Both_Branches_Return_Same_Type_Pin topic: core status: verified
// verified behavior: Test.choose(...) == 200
namespace Test
func choose(flag: bool) -> int {
if flag {
return 100
}
return 200
}Runs `test` → 6
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Break_From_Inner_Of_Nested_Loops topic: core status: verified
// verified behavior: Test.test(...) == 6
namespace Test
func test() -> int {
var outer = 0
var i = 0
while i < 3 {
var j = 0
while j < 10 {
if j == 2 {
break
}
outer = outer + 1
j = j + 1
}
i = i + 1
}
return outer
}Runs `clamp` → 7
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Clamp_Idiom_Pin topic: core status: verified
// verified behavior: Test.clamp(...) == 7
namespace Test
func clamp(n: int, lo: int, hi: int) -> int {
if n < lo {
return lo
}
if n > hi {
return hi
}
return n
}Runs `collatz_steps` → 8
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Collatz_Length_Pin topic: core status: verified
// verified behavior: Test.collatz_steps(...) == 8
namespace Test
func collatz_steps(n: int) -> int {
var x = n
var steps = 0
while x > 1 {
if x % 2 == 0 {
x = x / 2
} else {
x = x * 3 + 1
}
steps = steps + 1
}
return steps
}Runs `test` → 101010
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Comparison_All_Operators topic: core status: verified
// verified behavior: Test.test(...) == 101010
namespace Test
func test(a: int, b: int) -> int {
var result = 0
if a == b { result = result + 1 }
if a != b { result = result + 10 }
if a < b { result = result + 100 }
if a > b { result = result + 1000 }
if a <= b { result = result + 10000 }
if a >= b { result = result + 100000 }
return result
}Runs `describe` → "grade: F"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Conditional_Chain_With_StringConcat_Pin topic: core status: verified
// verified behavior: Test.describe(...) == "grade: F"
namespace Test
func describe(score: int) -> string {
var label = ""
if score >= 90 {
label = "A"
}
if score >= 80 && score < 90 {
label = "B"
}
if score >= 70 && score < 80 {
label = "C"
}
if score < 70 {
label = "F"
}
return "grade: " + label
}Runs `test` → 9
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Continue_From_Inner_Of_Nested_Loops topic: core status: verified
// verified behavior: Test.test(...) == 9
namespace Test
func test() -> int {
var total = 0
var i = 0
while i < 3 {
var j = 0
while j < 4 {
j = j + 1
if j == 2 {
continue
}
total = total + 1
}
i = i + 1
}
return total
}Runs `popcount` → 1
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Count_Set_Bits_Pin topic: core status: verified
// verified behavior: Test.popcount(...) == 1
namespace Test
func popcount(n: int) -> int {
var count = 0
var x = n
while x > 0 {
if x % 2 == 1 {
count = count + 1
}
x = x / 2
}
return count
}Runs `test` → "X"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Defer_LIFO_Ordering topic: core status: verified
// verified behavior: Test.test(...) == "X"
namespace Test
func test() -> string {
var s = ""
defer { s = s + "C" }
defer { s = s + "B" }
defer { s = s + "A" }
s = s + "X"
return s
}Runs `test` → ".early"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Defer_Runs_On_Early_Return topic: core status: verified
// verified behavior: Test.test(...) == ".early"
namespace Test
func test() -> string {
var trace = ""
defer { trace = trace + "D" }
if true {
return trace + ".early"
}
return trace
}Runs `digit_sum` → 1
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::DigitSum_Via_While_Pin topic: core status: verified
// verified behavior: Test.digit_sum(...) == 1
namespace Test
func digit_sum(n: int) -> int {
var total = 0
var x = n
while x > 0 {
total = total + x % 10
x = x / 10
}
return total
}Runs `fact` → 3628800
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Factorial_Recursive_Pin topic: core status: verified
// verified behavior: Test.fact(...) == 3628800
namespace Test
func fact(n: int) -> int {
if n <= 1 {
return 1
}
return n * fact(n - 1)
}Runs `fib` → 6765
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Fibonacci_Iterative_Pin topic: core status: verified
// verified behavior: Test.fib(...) == 6765
namespace Test
func fib(n: int) -> int {
if n < 2 {
return n
}
var a = 0
var b = 1
var i = 2
while i <= n {
let next = a + b
a = b
b = next
i = i + 1
}
return b
}Runs `fib` → 55
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Fibonacci_Recursive_Pin topic: core status: verified
// verified behavior: Test.fib(...) == 55
namespace Test
func fib(n: int) -> int {
if n < 2 {
return n
}
return fib(n - 1) + fib(n - 2)
}Runs `test` → 3.5
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Float_Add_Double topic: core status: verified
// verified behavior: Test.test(...) == 3.5
namespace Test
func test(a: double, b: double) -> double = a + bRuns `test` → 2.5
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Float_Div_Double topic: core status: verified
// verified behavior: Test.test(...) == 2.5
namespace Test
func test(a: double, b: double) -> double = a / bRuns `test` → 6.0
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Float_Mul_Double topic: core status: verified
// verified behavior: Test.test(...) == 6.0
namespace Test
func test(a: double, b: double) -> double = a * bRuns `test` → 0.75
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Float_Sub_Double topic: core status: verified
// verified behavior: Test.test(...) == 0.75
namespace Test
func test(a: double, b: double) -> double = a - bRuns `test` → 10
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Function_Literal_Captures_Let topic: core status: verified
// verified behavior: Test.test(...) == 10
namespace Test
func test() -> int {
let x = 10
let read = func() -> int = x
return read()
}Runs `sign` → 0
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Function_Multiple_Returns_Via_If_Else topic: core status: verified
// verified behavior: Test.sign(...) == 0
namespace Test
func sign(n: int) -> int {
if n > 0 { return 1 }
if n < 0 { return -1 }
return 0
}Runs `is_even` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Function_Returning_Bool_From_Comparison topic: core status: verified
// verified behavior: Test.is_even(...) == true
namespace Test
func is_even(n: int) -> bool = n % 2 == 0Runs `gcd` → 7
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::GCD_Euclidean_Pin topic: core status: verified
// verified behavior: Test.gcd(...) == 7
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
}Runs `sum_squares` → 385
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Helper_Function_Side_Effects_Through_Return_Pin topic: core status: verified
// verified behavior: Test.sum_squares(...) == 385
namespace Test
func square(n: int) -> int = n * n
func sum_squares(n: int) -> int {
var total = 0
var i = 1
while i <= n {
total = total + square(i)
i = i + 1
}
return total
}Runs `test` → "zero"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::If_As_Statement_Returns_From_Branch topic: core status: verified
// verified behavior: Test.test(...) == "zero"
namespace Test
func test(n: int) -> string {
if n > 0 {
return "positive"
}
if n < 0 {
return "negative"
}
return "zero"
}Runs `test` → 0
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::If_Mutates_Outer_Var topic: core status: verified
// verified behavior: Test.test(...) == 0
namespace Test
func test(n: int) -> int {
var result = 0
if n > 0 {
result = 1
}
if n < 0 {
result = -1
}
return result
}Runs `isqrt` → 31
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::IntegerSquareRoot_BinarySearch_Pin topic: core status: verified
// verified behavior: Test.isqrt(...) == 31
namespace Test
func isqrt(n: int) -> int {
if n < 2 {
return n
}
var lo = 0
var hi = n
while lo < hi {
let mid = (lo + hi + 1) / 2
if mid * mid <= n {
lo = mid
}
if mid * mid > n {
hi = mid - 1
}
}
return lo
}Runs `is_prime` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::IsPrime_Trial_Division_Pin topic: core status: verified
// verified behavior: Test.is_prime(...) == true
namespace Test
func is_prime(n: int) -> bool {
if n < 2 {
return false
}
if n == 2 {
return true
}
if n % 2 == 0 {
return false
}
var d = 3
while d * d <= n {
if n % d == 0 {
return false
}
d = d + 2
}
return true
}