Skip to content

Instantly share code, notes, and snippets.

@quintesse
Last active September 2, 2015 00:40
Show Gist options
  • Save quintesse/cd91333d357d8376617b to your computer and use it in GitHub Desktop.
Save quintesse/cd91333d357d8376617b to your computer and use it in GitHub Desktop.
Ceylon Web Runner: Destructuring examples
void tupleVar([Integer, Float, String] tuple) {
value [i1, f1, s1] = tuple;
value [i2, Float f2, s2] = tuple;
value [Integer i3, Float f3, String s3] = tuple;
}
void tupleLiteral() {
value [i1, f1, s1] = [0, 1.0, "foo"];
value [i2, Float f2, s2] = [0, 1.0, "foo"];
value [Integer i3, Float f3, String s3] = [0, 1.0, "foo"];
}
void tupleGeneric() {
class Foo<T>() {}
class FooSub<T>() extends Foo<T>() {}
value [f1] = [FooSub<Integer>()];
value [FooSub<Integer> f2] = [FooSub<Integer>()];
value [Foo<Integer> f3] = [FooSub<Integer>()];
}
void entryVar(Integer->String entry) {
value i1->s1 = entry;
value Integer i2->s2 = entry;
value Integer i3->String s3 = entry;
}
void entryLiteral() {
value i1->s1 = 0->"foo";
value Integer i2->s2 = 0->"foo";
value Integer i3->String s3 = 0->"foo";
}
void entryGeneric() {
class Foo<T>() {}
class FooSub<T>() extends Foo<T>() {}
value i1->f1 = 0->FooSub<Integer>();
value Integer i2->FooSub<Integer> f2 = 0->FooSub<Integer>();
value Integer i3->Foo<Integer> f3 = 0->FooSub<Integer>();
}
void destructuringLet([String, Float, Integer] tuple, String->Object entry) {
value x1 = let ([s, f, i]=tuple) s.size + f*i;
value y2 = let ([String s, Float f, Integer i]=tuple) s.size + f*i;
value e1 = let (k->v=entry) k+v.string;
value f2 = let (String k->Object v=entry) k+v.string;
}
void variadicDestructuring([String, String, String*] strings,
[Integer, Float, String] tup,
[Float+] floats) {
value [x, y, *rest] = strings;
value [i, *pair] = tup;
value [Float ff, String ss] = pair;
value [z, *zs] = floats;
}
void destructureTupleInEntry(String->[Float,Float] entry) {
value s->[x, y] = entry;
value z = let (s_->[x_, y_] = entry) x_*y_;
}
void destructureNestedTuple([String,[Integer,Float],String->String] tuple) {
value [s, [i, f], k -> v] = tuple;
value x = let ([s_, [i_,f_], k_ -> v_] = tuple) k_+v_;
}
void destructureInFor({[String, Float, String->String]*} iter) {
for ([x, y, s1->s2] in iter) {
String s = x;
Float f = y;
String->String e = s1->s2;
}
for ([String x, Float y, String s1 -> String s2] in iter) {
String s = x;
Float f = y;
String->String e = s1->s2;
}
}
void destructureInForComprehensions({[String, Float, String->String]*} iter) {
value xs = { for ([x, y, s1->s2] in iter) [s1->s2, y, x] };
value ys = { for ([String x, Float y, String s1 -> String s2] in iter) [s1->s2, y, x] };
value xys = { for ([x1, y1, sk1->sv1] in iter) for ([x2, y2, sk2->sv2] in iter) [x1, y2, sk1->sv2] };
}
void destructureIf([Float, Integer]? maybePair, String[] names, <String->Object>? maybeEntry) {
if (exists [x, i] = maybePair) {
Float c = x;
Integer j = i;
}
if (exists k->v = maybeEntry) {
String key = k;
Object item = v;
}
if (nonempty [name, *rest] = names) {
String n = name;
String[] ns = rest;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment