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 void run() => printAll { for (i in 0..50) fizzbuzz(i) }; | |
String fizzbuzz(Integer i) | |
=> (i%15==0 then "fizzbuzz") | |
else (i%3==0 then "fizz") | |
else (i%5==0 then "buzz") | |
else i.string; | |
void printAll({String*} strings) => print(", ".join(strings)); |
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
"A generic function that produces a memoized version | |
of the given [[function|fun]]. Works for any function | |
arity." | |
Callable<Return,Args> memoize<Return,Args>(Callable<Return,Args> fun) | |
given Args satisfies Anything[] { | |
value cache = HashMap<Args,Return&Object|Finished>(); | |
function callFun(Args args) { | |
//we'll use finished as a convenient | |
//unit value to represent the case | |
//that the function returned null |
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.language.serialization { | |
serialization, | |
Deconstructor | |
} | |
import ceylon.language.meta.model { | |
ClassModel, | |
Type | |
} | |
import ceylon.language.meta.declaration { | |
ValueDeclaration, |
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
"Finds the (last) longest substring that contains | |
at most two unique characters" | |
String longest2UniqueCharSubstring(String s) | |
=> let (init = [[0, 0, 0, 0], 0, '\0', '\0'], | |
mpos = s.fold(init)((acc,ch) | |
=> let ([[mb,me,cb,ce],cb1,ch0,ch1] = acc, | |
ce1 = ce+1, | |
max = (Integer b, Integer e) | |
=> me-mb > e-b then [mb,me,b,e] | |
else [b,e,b,e]) |
OlderNewer