This tiny example demonstrates the use of Ceylon [service providers][] in the JavaScript environment. Service providers are an abstraction of, and [interoperate][] with, Java's [service loaders][], but are not tied to the Java platform, and can even work cross-platform.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent { | |
Callable, | |
ScheduledThreadPoolExecutor | |
} | |
void execute(Integer numberOfTasks) { | |
value n = 1G; // same number of iterations as Java | |
value delta = 1.0 / n; | |
value startTime = process.nanoseconds; | |
value sliceSize = n / numberOfTasks; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Three generic functions for building tuple comparators. */ | |
"A comparator function for any `Comparable` type." | |
Comparison comparator<Type>(Type x, Type y) | |
given Type satisfies Comparable<Type> | |
=> x<=>y; | |
"A comparator function for instances of `[]`." | |
Comparison emptyComparator([] x, [] y) => equal; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//a function that produces a tuple | |
[String, String?, String] parseName(String name) { | |
value it = name.split().iterator(); | |
"first name is required" | |
assert (is String first = it.next()); | |
"last name is required" | |
assert (is String second = it.next()); | |
if (is String third = it.next()) { | |
return [first, second, third]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Encoding of natural numbers at | |
type level." | |
interface Nat of Zero|Succ<Nat> {} | |
"The natural number `0`." | |
interface Zero satisfies Nat {} | |
"The natural number `N+1` for a | |
given natural number `N`." | |
interface Succ<N> | |
satisfies Nat | |
given N satisfies Nat {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Encoding of natural numbers at | |
type level." | |
interface Nat of Zero|Succ<Nat> {} | |
"The natural number `0`." | |
interface Zero satisfies Nat {} | |
"The natural number `N+1` for a | |
given natural number `N`." | |
interface Succ<N> | |
satisfies Nat | |
given N satisfies Nat {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ceylon.collection { | |
LinkedList | |
} | |
import ceylon.math.float { | |
sin, | |
pi | |
} | |
"Map a function of arity `n` to a stream of values. In each |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Description Location Resource Path Type | |
Access restriction: The constructor 'CoreException(IStatus)' is not API (restriction on required library '/Users/gavin/ceylon-ide-eclipse/plugins/com.redhat.ceylon.eclipse.ui/target/repo/plugins/org.eclipse.equinox.common-3.6.200.v20130402-1505.jar') line 123 AndroidBuildHookProvider.java /com.redhat.ceylon.eclipse.android.plugin/src/com/redhat/ceylon/eclipse/android/plugin Java Problem | |
Access restriction: The constructor 'Status(int, String, int, String, Throwable)' is not API (restriction on required library '/Users/gavin/ceylon-ide-eclipse/plugins/com.redhat.ceylon.eclipse.ui/target/repo/plugins/org.eclipse.equinox.common-3.6.200.v20130402-1505.jar') line 123 AndroidBuildHookProvider.java /com.redhat.ceylon.eclipse.android.plugin/src/com/redhat/ceylon/eclipse/android/plugin Java Problem | |
Access restriction: The constructor 'Status(int, String, String, Throwable)' is not API (restriction on required library '/Users/gavin/ceylon-ide-eclipse/plugins/com.redhat.ceylon.ecl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shared Map<Key,Item> reduceByKey<Key,Item>( | |
{<Key->Item>+} stream, | |
Item accumulating(Item x, Item y)) | |
given Key satisfies Object | |
given Item satisfies Object | |
=> stream.summarize(Entry.key, | |
(Item? partial, entry) | |
=> let (_->item = entry) | |
if (exists partial) | |
then accumulating(partial, item) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Demonstrates how to do monadic-style processing of | |
optional values in Ceylon. Represents an optional | |
type by boxing non-null values as singletons, and | |
the null value as the empty sequence. Monadic | |
operations are inherited from the stream interface, | |
with the caveat that m.flatMap(f) must be written | |
as m.flatMap(lift(f)) or as m.map(f).coalesced. | |
For the record: this is not idiomatic Ceylon. In |