Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,2 @@
foo(); // <-- this is "invoking a function in statement context"
Int x = bar(); // <-- this is "invoking a function in expression context"

View file

@ -0,0 +1,2 @@
foo(1, 2, 3);
Int x = bar(4, 5, 6);

View file

@ -0,0 +1,13 @@
module CallOptArgsFunc {
static Int foo(Int a=0, Int b=99, Int c=-1) {
return a + b + c;
}
void run() {
@Inject Console console;
console.print($"{foo()=}");
console.print($"{foo(1)=}");
console.print($"{foo(1, 2)=}");
console.print($"{foo(1, 2, 3)=}");
}
}

View file

@ -0,0 +1,15 @@
module CallVarArgsFunc {
// Ecstasy does not have a var-args concept; instead, array notation is used
static Int foo(Int[] args = []) {
return args.size;
}
void run() {
@Inject Console console;
console.print($"{foo()=}");
console.print($"{foo([])=}");
console.print($"{foo([1])=}");
console.print($"{foo([1, 2])=}");
console.print($"{foo([1, 2, 3])=}");
}
}

View file

@ -0,0 +1,12 @@
module CallNamedArgsFunc {
static String foo(Int a=1, Int b=2, Int c=3) {
return $"a:{a}, b:{b}, c:{c}";
}
void run() {
@Inject Console console;
console.print($"{foo(c=9, b=8, a=7)=}");
console.print($"{foo(4, c=6, b=5)=}");
console.print($"{foo(c=99)=}");
}
}

View file

@ -0,0 +1,11 @@
module FirstClassFunctions {
@Inject Console console;
void run() {
function Int(String) stringLen = s -> s.size;
function Int(Int, Int) sum = (n1, n2) -> n1+n2;
String[] testData = ["abc", "easy", "as", "123"];
console.print($|total string length of values in {testData} =\
| {testData.map(stringLen).reduce(0, sum)}
);
}
}

View file

@ -0,0 +1,16 @@
module ObtainReturnValues {
(Int, String, Dec) foo() {
return 3, "hello!", 9.87;
}
void run() {
foo(); // ignore return values
Int i1 = foo(); // only use first returned value
(Int i2, String s2) = foo(); // only use first two returned values
(Int i3, String s3, Dec d3) = foo(); // use all returned values
Tuple<Int, String, Dec> t = foo(); // alternatively, get the tuple instead
@Inject Console console;
console.print($"{i3=}, {s3=}, {d3=}, {t=}");
}
}

View file

@ -0,0 +1,16 @@
// Ecstasy does not have any built-in functions. However, there are two keywords
// ("is" and "as") that use a function-like syntax:
module IsAndAs {
Int|String foo() {
return "hello";
}
void run() {
@Inject Console console;
Object o = foo();
if (o.is(String)) { // <- looks like a function call
String s = o.as(String); // <- looks like a function call
console.print($"foo returned the string: {s.quoted()}");
}
}
}

View file

@ -0,0 +1,19 @@
module PartialApplication {
void foo(String s, Int i, Dec d) {
@Inject Console console;
console.print($"inside call to foo({s=}, {i=}, {d=})");
}
void run() {
// note that the "&" obtains the reference to the function, and suppresses the
// invocation thereof, so it is *allowed* in all three of these cases, but it
// is *required* in the third case:
function void(String, Int, Dec) unbound = foo; // or "foo(_, _, _)"
function void(String, Dec) partBound = unbound(_, 99, _);
function void() allBound = &partBound("world", 3.14);
unbound("nothing", 0, 0.0);
partBound("hello", 2.718);
allBound();
}
}