Links

Categories

Tags


« | Main | »

Testing ternary operator, lambda expressions

By Jewe | May 31, 2014

Two new operators have been added to JewelScript.

/*
 * test ternary operator and lambda expressions
 */

import stdlib;
using stdlib;

delegate int Comparator(int a, int b);

function string main(const string[] args)
{
    string a = "Hello";
    println((a == "hello") ? "a is hello" : "a is not hello");

    string s = (a == "hello") ? 1 : 2;
    int i = (a == "hello") ? (int)"1" : (int)"2";

    Comparator c = (a, b) => a - b;
    println( c(15, 5) );

    Compare(15, 5, (x, y) => x - y);
    Compare(15, 5, (a, b) => b - a);

    return null;
}

function Compare(int a, int b, Comparator fn)
{
    println(
        (string)a
        + " is "
        + (fn(a, b) < 0 ? "less than" : (fn(a, b) > 0 ? "greater than" : "equal to"))
        + " "
        + b);
}

Topics: code examples, news | Comments Off on Testing ternary operator, lambda expressions

Comments are closed.