Skip to content

Instantly share code, notes, and snippets.

@ceylonwebide
ceylonwebide / index.json
Last active September 26, 2015 18:43
IMPORTANT: Official Ceylon Web IDE Examples
{
"sets": [
{
"title": "Try out a sample:",
"items": [
{
"title": "Hello World",
"gist": "1b5a174b7fda6eb0e3a6"
},
{
@ceylonwebide
ceylonwebide / hello_world.ceylon
Last active March 12, 2021 13:26
EXAMPLE: Hello World
//$webrun_wrapped
shared void run() {
// The classic "hello world" in Ceylon
print("Hello world!");
// Click 'Run' above to run the program.
// It is compiled to JavaScript and then runs directly in your browser!
// You can try the more examples from the list on the right of this page
@ceylonwebide
ceylonwebide / basics.ceylon
Created September 26, 2015 18:12
EXAMPLE: Basics
//$webrun_wrapped
shared void run() {
// Any object can be converted to a string using ".string".
// There are no special primitive types, even numbers are objects.
Integer i = 3;
String s = i.string;
"Declarations can be documented using the `doc` annotation.
Note that string literals can span several lines."
Boolean b = (i == 3);
@ceylonwebide
ceylonwebide / null_and_union.ceylon
Created September 26, 2015 18:17
EXAMPLE: Null values and union types
//$webrun_wrapped
shared void run() {
// The following is an error because null is not a String.
//String s = null; //ERROR!
// We have to write "String?" instead:
String? s1 = null;
String? s2 = "This is a String.";
print(s1);
@ceylonwebide
ceylonwebide / conditions.ceylon
Created September 26, 2015 18:18
EXAMPLE: Conditions and assertions
//$webrun_wrapped
shared void run() {
//You can use normal conditions as in any other language
value nums = 1..10;
if (nums.size == 10) {
print("ok, normal condition");
}
if (nums.size == 10 && nums.first == 1) {
print("another normal condition");
}
@ceylonwebide
ceylonwebide / classes_and_functions.ceylon
Created September 26, 2015 18:18
EXAMPLE: Classes and functions 1
//$webrun_wrapped
shared void run() {
// Functions use the familiar C/Java syntax:
Float mean(Float x, Float y) {
return ((x*x + y*y) / 2) ^ 0.5;
}
print("mean(11.0, 307.0) = ``mean(11.0, 307.0)``");
// Short form for functions consisting of a single expression
Float mean2(Float x, Float y) => ((x*x + y*y) / 2) ^ 0.5;
@ceylonwebide
ceylonwebide / interfaces.ceylon
Created September 26, 2015 18:19
EXAMPLE: Interfaces and mixin inheritance
//$webrun_wrapped
shared void run() {
"This interface represents a customer of the shop."
interface Customer {
"The name of the customer."
shared formal String name;
// Interfaces may define concrete implementations of methods
// and getters/setters.
@ceylonwebide
ceylonwebide / classes_and_functions2.ceylon
Created September 26, 2015 18:20
EXAMPLE: Classes and functions 2
//$webrun_wrapped
shared void run() {
// We can use a reference to a function as a value:
Float average(Float x, Float y) => (x + y) * 0.5;
value f1 = average;
print("f1(1.0, 7.0) = ``f1(1.0, 7.0)``");
// The type of f1 is Float(Float, Float):
Float(Float, Float) f2 = f1;
@ceylonwebide
ceylonwebide / collections.ceylon
Created September 26, 2015 18:21
EXAMPLE: Collections and sequence comprehensions
//$webrun_wrapped
shared void run() {
// A constant, read-only sequence of values:
String[] animals = [ "elephant", "parrot", "giraffe" ];
String[] none = []; // empty sequence
// Accessing the sequence returns an optional value (String?)
print("First: ``animals.first else "none"``");
print("Second: ``animals[1] else "none"``");
print("Fourth: ``animals[3] else "none"``");
@ceylonwebide
ceylonwebide / named_arguments.ceylon
Created September 26, 2015 18:22
EXAMPLE: Named argument syntax
//$webrun_wrapped
shared void run() {
void order(String product, Integer count=1, Float discount=0.0,
{String*} comments={}) {
String commentStr = ", ".join { for (c in comments) "'``c``'" };
print("Order '``product``', quantity ``count``, discount ``
discount``, comments: ``commentStr``");
}
// Ceylon has an alternative syntax for invoking functions or