Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The world modelled in Ceylon
abstract class Thing() {
string => className(this).split('.'.equals).last?.string else "";
}
abstract class World(shared Thing+ contents) {}
abstract class Ocean() extends Thing() {}
abstract class Land() of Continent|Island extends Thing() {}
abstract class Continent() of america|africa|europe|asia extends Land() {}
abstract class Island() of hawaii|fiji|canaria extends Land() {}
object pacific extends Ocean() {}
object atlantic extends Ocean() {}
object indian extends Ocean() {}
object america extends Continent() {}
object africa extends Continent() {}
object europe extends Continent() {}
object asia extends Continent() {}
object hawaii extends Island() {}
object fiji extends Island() {}
object canaria extends Island() {}
object earth extends World(
pacific, atlantic, indian,
america, africa, europe, asia,
hawaii, fiji, canaria) {
string => contents.string;
}
alias SquareMeters => Float;
alias Meters => Float;
SquareMeters size(Thing thing) {
switch (thing)
case (pacific) { return 1.0; }
case (atlantic) { return 2.0; }
case (indian) { return 3.0; }
case (america) { return 4.0; }
case (africa) { return 5.0; }
case (europe) { return 6.0; }
case (asia) { return 7.0; }
case (hawaii) { return 8.0; }
case (fiji) { return 9.0; }
case (canaria) { return 10.0; }
else { return 0.0; } // Thing is not enumerated
}
Meters deepestDepth(Ocean ocean) {
switch(ocean)
case (pacific) { return 10.0; }
case (atlantic) { return 8.4; }
case (indian) { return 8.2; }
else { return 0.0; } // Ocean is not enumerated (other planets may have them too)
}
Meters highestElevation(Continent continent) {
switch(continent)
case (america) { return 1.0; }
case (africa) { return 2.0; }
case (europe) { return 3.0; }
case (asia) { return 10.0; } // Continent is enumerated
}
shared void run() {
print("The Earth: ``earth``");
value earthOceans = [for (thing in earth.contents) if (is Ocean thing) thing];
print("Earth's oceans: ``earthOceans``");
print("All islands: `` `Island`.caseValues ``");
print("Deepest ocean on Earth: ``earthOceans.sort(byDecreasing(deepestDepth)).first else "?"``");
print("Highest continent: `` `Continent`.caseValues.sort(byDecreasing(highestElevation)).first else "?"``");
print("Size of all continents: ``sum([0.0, *`Continent`.caseValues.map(size)])`` sqm");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment