Skip to content

core — examples

← all topics · 500 examples · page 2 of 10 · raw source ↓

Runs `go` → 25

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Protocol_AcrossFiles_DeclaredInA_ImplInB_UsedInC   topic: core   status: verified
// verified behavior: Test.go(...) == 25

namespace Test

interface IShape {
    func area() -> int
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Protocol_EmitsInterfaceType   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

interface IDrawable {
    func draw(x: int) -> string
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::ProtocolType_ResolvesInMethodSignature   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

interface IWidget {
    func render() -> string
}

func display(w: IWidget) -> int {
    return 0
}

Runs `abs` → 0

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::ShortFormOpcodes_Abs_BranchesWork   topic: core   status: verified
// verified behavior: Test.abs(...) == 0

namespace Test

func abs(x: int) -> int {
    if x < 0 {
        return 0 - x
    }
    return x
}

Runs `sumTo` → 5050

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::ShortFormOpcodes_SumTo_ProducesCorrectResult   topic: core   status: verified
// verified behavior: Test.sumTo(...) == 5050

namespace Test

func sumTo(n: int) -> int {
    var total = 0
    var i = 0
    while i <= n {
        total += i
        i += 1
    }
    return total
}

Runs `run` → 7

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Spawn_SimpleJob_RunsAndJoinsCleanly   topic: core   status: verified
// verified behavior: Test.run(...) == 7

namespace Test

func run() -> int {
    let j = spawn {
        let x = 1
    }
    j.Join()
    return 7
}

Runs `sumTail` → 50005000

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::TailCall_AccumulatorPattern   topic: core   status: verified
// verified behavior: Test.sumTail(...) == 50005000

namespace Test

func sumTail(n: int, acc: int) -> int {
    if n <= 0 {
        return acc
    }
    return sumTail(n - 1, acc + n)
}

Runs `countdown` → 0

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::TailCall_RecursiveFunction_NoStackOverflow   topic: core   status: verified
// verified behavior: Test.countdown(...) == 0

namespace Test

func countdown(n: int) -> int {
    if n <= 0 {
        return 0
    }
    return countdown(n - 1)
}

Runs `pick` → 99

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Ternary_BasicConditional   topic: core   status: verified
// verified behavior: Test.pick(...) == 99

namespace Test

func abs(x: int) -> int {
    return x >= 0 ? x : 0 - x
}

func pick(flag: bool) -> int {
    return flag ? 42 : 99
}

ILEmitterTests__Ternary_InExpression

core runnable verified

Runs `clamp` → 10

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Ternary_InExpression   topic: core   status: verified
// verified behavior: Test.clamp(...) == 10

namespace Test

func clamp(x: int, lo: int, hi: int) -> int {
    let v = x < lo ? lo : x
    return v > hi ? hi : v
}

Runs `test` → 5

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Ternary_WithNullCoalescing   topic: core   status: verified
// verified behavior: Test.test(...) == 5

namespace Test

func test() -> int {
    let a = 5 > 0 ? 5 : 0
    let s = nil ?? "fallback"
    return a
}

Runs `run` → 99

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Throw_Simple_InvalidOperation   topic: core   status: verified
// verified behavior: Test.run(...) == 99

namespace Test

func run() -> int {
    var result = 0
    try {
        throw InvalidOperationException("bad")
    } catch (Exception e) {
        result = 99
    }
    return result
}

Runs `run` → 100

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Try_BodyRunsWithoutException   topic: core   status: verified
// verified behavior: Test.run(...) == 100

namespace Test

func run() -> int {
    var result = 0
    try {
        result = int.Parse("100")
    } catch (Exception e) {
        result = -1
    }
    return result
}

Runs `run` → 42

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Try_CatchAll_BareCatch   topic: core   status: verified
// verified behavior: Test.run(...) == 42

namespace Test

func run() -> int {
    var result = 0
    try {
        result = int.Parse("nope")
    } catch {
        result = 42
    }
    return result
}

Runs `run` → -1

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Try_CatchSpecificException_FormatException   topic: core   status: verified
// verified behavior: Test.run(...) == -1

namespace Test

func run() -> int {
    var result = 0
    try {
        result = int.Parse("not a number")
    } catch (FormatException e) {
        result = -1
    }
    return result
}

Runs `test` → 21

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Tuple_ReturnAndDestructure   topic: core   status: verified
// verified behavior: Test.test(...) == 21

namespace Test

func swap(a: int, b: int) -> (int, int) {
    return (b, a)
}

func test() -> int {
    let (x, y) = swap(1, 2)
    return x * 10 + y
}

ILEmitterTests__Tuple_TwoElements

core runnable verified

Runs `getFirst` → 42

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Tuple_TwoElements   topic: core   status: verified
// verified behavior: Test.getFirst(...) == 42

namespace Test

func pair(a: int, b: string) -> (int, string) {
    return (a, b)
}

func getFirst() -> int {
    let (x, y) = pair(42, "hello")
    return x
}

Runs `test` → 2

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::TupleReturn_WithListElements   topic: core   status: verified
// verified behavior: Test.test(...) == 2

namespace Test

func split() -> (List<int>, List<string>) {
    var nums = List<int>()
    var strs = List<string>()
    nums.Add(1)
    strs.Add("a")
    return (nums, strs)
}

func test() -> int {
    let (nums, strs) = split()
    return nums.Count + strs.Count
}

Rejected at compile time: ES2140

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Adversarial.cs::DefiniteReturn_FallThrough_IsError   topic: core   status: verified
// verified behavior: reports diagnostic ES2140

namespace Test

func go(n: int) -> int {
    if n > 0 {
        return 1
    }
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Adversarial.cs::DefiniteReturn_InfiniteLoopTerminates_Clean   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

func go() -> int {
    var i = 0
    while true {
        i += 1
        if i >= 5 {
            return i
        }
    }
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Adversarial.cs::Generics_ListOfNullableInt   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

func go() -> int {
    let xs = List<int?>()
    xs.Add(7)
    xs.Add(nil)
    return xs[0] ?? 0
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Adversarial.cs::Generics_NestedTuple   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

func go() -> int {
    let t = (1, (2, 3))
    return t.Item1 + t.Item2.Item1 + t.Item2.Item2
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Adversarial.cs::Generics_NullableDoubleCoalesceAndConditional   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

func pick(v: double?) -> double = v ?? 1.5

func go() -> double {
    return pick(nil) + pick(2.5)
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Adversarial.cs::Generics_TupleListForDestructure   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

func go() -> int {
    let pairs = List<(int, int)>()
    pairs.Add((1, 2))
    pairs.Add((3, 4))
    var total = 0
    for (a, b) in pairs {
        total += a * b
    }
    return total
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Adversarial.cs::Lexer_ExponentLargeMantissa   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

func go() -> double = 6.022e23

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Adversarial.cs::Lexer_ExponentLowercaseE   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

func go() -> double = 1.0e10

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Adversarial.cs::Lexer_ExponentNegative   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

func go() -> double = 1.5e-3

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Adversarial.cs::Lexer_ExponentUppercaseE   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

func go() -> double = 2E8

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Adversarial.cs::Lexer_UnderscoreSeparators   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

func go() -> int = 1_000_000

Runs `go` → "ok-miss"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Choice.cs::LetElse_Guard   topic: core   status: verified
// verified behavior: Test.go(...) == "ok-miss"

namespace Test

func find(id: int) -> string? {
    if id == 0 {
        return nil
    }
    return "ok"
}

func lookup(id: int) -> string {
    let v = find(id) else {
        return "miss"
    }
    return v
}

func go() -> string {
    return lookup(5) + "-" + lookup(0)
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.cs::Added_ListLiteral_Count   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
func go() -> int {
    let xs = [9, 8, 7, 6]
    return xs.Count
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.cs::Added_ListOfBools_Index   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
func go() -> bool {
    let xs = [false, true]
    return xs[1]
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.cs::Added_ListOfDoubles_Index   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
func go() -> double {
    let xs = [1.5, 2.5, 3.5]
    return xs[1]
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.cs::Added_ListOfInts_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 t = 0
    for x in xs { t += x }
    return t
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.cs::Added_ListOfInts_LastViaIndexFromEnd   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
func go() -> int {
    let xs = [3, 5, 7]
    return xs[^1]
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.cs::Added_ListOfStrings_Concat   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
func go() -> string {
    let xs = ["a", "b"]
    return xs[0] + "," + xs[1]
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.cs::Added_NestedListOfLists   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
func go() -> int {
    var outer = List<List<int>>()
    var inner = List<int>()
    inner.Add(1)
    inner.Add(2)
    outer.Add(inner)
    return outer[0].Count
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.cs::Algo_CountChar   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
func countChar(s: string, target: char) -> int {
    var count = 0
    for c in s {
        if c == target { count += 1 }
    }
    return count
}
func go() -> int { return countChar("banana", 'a') }

Runs `go` → "a-b-c"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.cs::Algo_JoinWithSeparator   topic: core   status: verified
// verified behavior: Test.go(...) == "a-b-c"

namespace Test
func go() -> string {
    let xs = ["a", "b", "c"]
    var result = ""
    var first = true
    for x in xs {
        if first {
            result = x
            first = false
        } else {
            result = result + "-" + x
        }
    }
    return result
}

Runs `go` → "olleh"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.cs::Algo_ReverseString   topic: core   status: verified
// verified behavior: Test.go(...) == "olleh"

namespace Test
func reverse(s: string) -> string {
    var result = ""
    var i = s.Length - 1
    while i >= 0 {
        result = result + s[i].ToString()
        i -= 1
    }
    return result
}
func go() -> string { return reverse("hello") }

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.cs::Linq_Count   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_Collections.cs::Linq_Max   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
func go() -> int {
    let xs = [10, 30, 20]
    return xs.Max()
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.cs::Linq_Min   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
func go() -> int {
    let xs = [30, 10, 20]
    return xs.Min()
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.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, 5]
    return xs.Sum()
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.cs::List_AddThenCount   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)
    return xs.Count
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.cs::List_Contains   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
func go() -> bool {
    let xs = [1, 2, 3]
    return xs.Contains(2)
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.cs::List_Count   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
func go() -> int {
    let xs = [1, 2, 3]
    return xs.Count
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.cs::List_ForInSum   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
func go() -> int {
    let xs = [10, 20, 30]
    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_Collections.cs::List_MutateElement   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
func go() -> int {
    let xs = [1, 2, 3]
    xs[0] = 99
    return xs[0]
}

Runs `go` → "b"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.cs::List_StringElements   topic: core   status: verified
// verified behavior: Test.go(...) == "b"

namespace Test
func go() -> string {
    let xs = ["a", "b", "c"]
    return xs[1]
}