core — examples
← all topics · 500 examples · page 10 of 10 · raw source ↓
Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TranspilerTests.cs::Transpiles_Closure_Captures_Var topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func counter() -> int {
var count = 0
let increment = func() { count += 1 }
increment()
increment()
return count
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TranspilerTests.cs::Transpiles_CompoundAssignment topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Math
pub func sumTo(n: int) -> int {
var total = 0
var i = 0
while i <= n {
total += i
i += 1
}
return total
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TranspilerTests.cs::Transpiles_Function_Literal topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace FnTest
func run(items: List<int>) {
items.Sort(func(a: int, b: int) -> int {
return a - b
})
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TranspilerTests.cs::Transpiles_IndexExpression topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Arr
pub func first(items: List<int>) -> int {
return items[0]
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TranspilerTests.cs::Transpiles_IndexFromEnd topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Last
pub func last(items: List<int>) -> int {
return items[^1]
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TranspilerTests.cs::Transpiles_Nullable_Parameter topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func process(name: string?) -> int {
return 0
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TranspilerTests.cs::Transpiles_PlainString_NoInterpolation topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Plain
pub func msg() -> string {
return "hello world"
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TranspilerTests.cs::Transpiles_RangeExpression topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Slice
pub func take(s: string, n: int) -> string {
return s[..n]
}
pub func skip(s: string, n: int) -> string {
return s[n..]
}
pub func sub(s: string, a: int, b: int) -> string {
return s[a..b]
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TranspilerTests.cs::Transpiles_While_For_And_Spawn topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Worker
pub func sumTo(limit: int) -> int {
var i = 0
var total = 0
while i <= limit {
total = total + i
i = i + 1
}
return total
}
pub func sumAll(values: List<int>) -> int {
var total = 0
for value in values {
total = total + value
}
return total
}
pub func start(values: List<string>) -> Job {
return spawn {
for value in values {
Console.WriteLine(value)
}
}
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: EmitterStressTests.cs::Parse_KeywordAsVariableName_ReportsError topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func run() -> int {
let pub = 42
return pub
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Emit_LambdaWithTupleReturnType topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func run() {
var tasks = List<Task<(List<string>, List<string>)>>()
tasks.Add(Task.Run(func() -> (List<string>, List<string>) { return (List<string>(), List<string>()) }))
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::LocalLet_Reassignment_ReportsError topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func bad() -> int {
let x = 1
x = 2
return x
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::NullConditional_PropertyAccess topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func safeLen(s: string) -> string {
return s?.Length ?? "0"
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Parse_LambdaInsideMethodCallChain topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func run() {
let items = List<string>()
for item in items.Where(func(s: string) -> bool { return s != "" }).ToList() {
let x = item
}
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Parse_LambdaWithComplexGenericReturnType topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func run() {
var xs = List<Task<(List<string>, List<string>)>>()
xs.Add(Task.Run(func() -> (List<string>, List<string>) { return (List<string>(), List<string>()) }))
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Misc.cs::BclStatic topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
return Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_GenericDerive.cs::GenericEquals_AcrossFunctionBoundary topic: core status: unverified
// verified behavior: Test.go(...) == true
func same(a: Pair<int, int>, b: Pair<int, int>) -> bool = a.Equals(b)
func go() -> bool {
let x = Pair<int, int> { first: 9, second: 9 }
let y = Pair<int, int> { first: 9, second: 9 }
return same(x, y)
}Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_GenericDerive.cs::GetHashCode_EqualValuesEqualHash topic: core status: unverified
// verified behavior: Test.go(...) == true
func go() -> bool {
let a = Pair<int, int> { first: 7, second: 8 }
let b = Pair<int, int> { first: 7, second: 8 }
return a.GetHashCode() == b.GetHashCode()
}Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_GenericDerive.cs::IntPair_Equal topic: core status: unverified
// verified behavior: Test.go(...) == true
func go() -> bool {
let a = Pair<int, int> { first: 3, second: 4 }
let b = Pair<int, int> { first: 3, second: 4 }
return a.Equals(b)
}Runs `go` → false
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_GenericDerive.cs::IntPair_NotEqual topic: core status: unverified
// verified behavior: Test.go(...) == false
func go() -> bool {
let a = Pair<int, int> { first: 3, second: 4 }
let b = Pair<int, int> { first: 3, second: 5 }
return a.Equals(b)
}Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_GenericDerive.cs::MixedTypeArgs_Equal topic: core status: unverified
// verified behavior: Test.go(...) == true
func go() -> bool {
let a = Pair<string, bool> { first: "y", second: true }
let b = Pair<string, bool> { first: "y", second: true }
return a.Equals(b)
}Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_GenericDerive.cs::ObjectEquals_DelegatesToTyped topic: core status: unverified
// verified behavior: Test.go(...) == true
func go() -> bool {
let a = Pair<int, int> { first: 1, second: 1 }
let b = Pair<int, int> { first: 1, second: 1 }
return a.Equals(b)
}Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_GenericDerive.cs::StrPair_Equal topic: core status: unverified
// verified behavior: Test.go(...) == true
func go() -> bool {
let a = Pair<string, int> { first: "x", second: 1 }
let b = Pair<string, int> { first: "x", second: 1 }
return a.Equals(b)
}Runs `go` → false
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_GenericDerive.cs::StrPair_NotEqual topic: core status: unverified
// verified behavior: Test.go(...) == false
func go() -> bool {
let a = Pair<string, int> { first: "x", second: 1 }
let b = Pair<string, int> { first: "y", second: 1 }
return a.Equals(b)
}Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_GenericDerive.cs::TwoInstantiations_Independent topic: core status: unverified
// verified behavior: Test.go(...) == true
func ints() -> bool {
let a = Pair<int, int> { first: 1, second: 2 }
let b = Pair<int, int> { first: 1, second: 2 }
return a.Equals(b)
}
Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Integration.cs::Json_ComputesNestingDepth topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
func go() -> int {
let r = parse("[1, [2, [3, 4]]]")
if r.IsError { return -1 }
return depth(r.Value)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Integration.cs::Json_CountsAllNodes topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
func go() -> int {
let r = parse("[1, [2, 3], 4]")
if r.IsError { return -1 }
return countNodes(r.Value)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Integration.cs::Json_EmptyArray topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
func go() -> int {
let r = parse("[]")
if r.IsError { return -1 }
return countNodes(r.Value)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Integration.cs::Json_NegativeNumbers topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
func go() -> int {
let r = parse("[-2, -3]")
if r.IsError { return 999 }
return sumNumbers(r.Value)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Integration.cs::Json_SumIgnoresNonNumbers topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
func go() -> int {
let r = parse("[1, true, \"hi\", null, [2, 3]]")
if r.IsError { return -1 }
return sumNumbers(r.Value)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Integration.cs::Json_SumsNestedNumbers topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
func go() -> int {
let r = parse("[1, [2, 3], 4]")
if r.IsError { return -1 }
return sumNumbers(r.Value)
}Runs `go` → "unterminated array"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Integration.cs::Json_UnterminatedArrayIsError topic: core status: unverified
// verified behavior: Test.go(...) == "unterminated array"
func go() -> string {
let r = parse("[1, 2")
return r.IsError ? r.Error : "ok"
}Runs `go` → "unterminated string"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Integration.cs::Json_UnterminatedStringIsError topic: core status: unverified
// verified behavior: Test.go(...) == "unterminated string"
func go() -> string {
let r = parse("\"abc")
return r.IsError ? r.Error : "ok"
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Integration.cs::Json_WhitespaceTolerant topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
func go() -> int {
let r = parse(" [ 1 ,\t2 ,\n 3 ] ")
if r.IsError { return -1 }
return sumNumbers(r.Value)
}Runs `go` → 41
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_PtrEmbedIface.cs::PointerEmbed_BumpOnceThenValue topic: core status: unverified
// verified behavior: Test.go(...) == 41
func once(b: IBumper) -> int {
b.bump()
return b.value()
}
Runs `go` → "n=42"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_PtrEmbedIface.cs::PointerEmbed_TwiceThenInterpolate topic: core status: unverified
// verified behavior: Test.go(...) == "n=42"
func twice(b: IBumper) -> int {
b.bump()
b.bump()
return b.value()
}
Rejected at compile time: ES2130
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::RegularFunc_Var_Capture_Is_Not_ES2130 topic: core status: unverified
// verified behavior: reports diagnostic ES2130
namespace Test
func go() {
var counter = 0
let bump = func() { counter = counter + 1 }
bump()
}Runs `test` → 7
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Arrow_Lambda_Zero_Params topic: core status: unverified
// verified behavior: Test.test(...) == 7
namespace Test
func test() -> int {
let seven = () => 7
return seven()
}Rejected at compile time: ES2145
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: IndexDiagnosticTests.cs::IndexingList_DoesNotError topic: core status: unverified
// verified behavior: reports diagnostic ES2145
namespace Test
func f(xs: List<int>) -> int { return xs[0] }Rejected at compile time: ES2145
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: IndexDiagnosticTests.cs::IndexingString_DoesNotError topic: core status: unverified
// verified behavior: reports diagnostic ES2145
namespace Test
func first(s: string) -> char { return s[0] }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: MixedLanguageTests.cs::CSharp_Class_Constructed_In_Esharp topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func make(n: int) -> Box = Box(n)
func valueOf(b: Box) -> int = b.NCompiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: MixedLanguageTests.cs::CSharp_Class_Field_Read_From_Esharp_Function topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func sum(v: Vec) -> int = v.X + v.YCompiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: MixedLanguageTests.cs::CSharp_Ctor_With_Multiple_Args_Constructed_In_Esharp topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func make(x: int, y: int, label: string) -> Rect = Rect(x, y, label)
func area(r: Rect) -> int = r.X * r.YCompiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: MixedLanguageTests.cs::CSharp_Enum_Switch_In_Esharp topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func label(c: Color) -> string {
if c == Color.Red {
return "red"
}
if c == Color.Green {
return "green"
}
return "other"
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: MixedLanguageTests.cs::CSharp_Property_Read_From_Esharp topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func name_of(p: Person) -> string = p.NameCompiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: OptionalArgTests.cs::ConstructorDefault_Used_When_Omitted topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let c = Counter()
return c.Value
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: OptionalArgTests.cs::InstanceMethodDefault_Used_When_Omitted topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let c = Counter(100)
return c.bump()
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: RefChoiceCaseViewTests.cs::Json_Transparent_EndToEnd topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
func go() -> string {
Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TranspilerTests.cs::Reports_Syntax_Diagnostics topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace Broken
pub func nope( -> void {
return
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TranspilerTests.cs::Transpiles_Defer topic: core status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace IO
pub func process(path: string) -> string {
let file = File.Open(path)
defer { file.Close() }
return file.ReadAll()
}