Skip to content

Instantly share code, notes, and snippets.

View gavinking's full-sized avatar
💭
Say more, more clearly, with @ceylon.

Gavin King gavinking

💭
Say more, more clearly, with @ceylon.
View GitHub Profile
@gavinking
gavinking / serialization.ceylon
Last active August 29, 2015 14:10
playing with serialization
import ceylon.language.serialization {
serialization,
Deconstructor
}
import ceylon.language.meta.model {
ClassModel,
Type
}
import ceylon.language.meta.declaration {
ValueDeclaration,
@gavinking
gavinking / calendar.ceylon
Created May 8, 2015 20:31
An internationalized version of @chochos' calendar example, with a few changes, just for fun.
import ceylon.locale {
systemLocale
}
import ceylon.time {
today,
date
}
import ceylon.time.base {
monthOf
}
@gavinking
gavinking / parsePropertiesFile.ceylon
Created June 29, 2015 11:42
function to parse properties files
"Parse a properties file."
void parsePropertiesFile(String textContent,
void handleEntry(String key, String text)) {
value lines = textContent.lines.iterator();
while (!is Finished rawline = lines.next()) {
value builder = StringBuilder();
builder.append(rawline);
variable value lastline = rawline;
while (lastline.endsWith("\\"), //line continuation
!is Finished nextline = lines.next()) {
@gavinking
gavinking / balancedBrackets.ceylon
Last active August 29, 2015 14:27
my version of @luolong's solution to Rosetta Code balanced brackets
shared void balancedBrackets() {
value nextRandom = random();
for (length in 1..10) {
value text = generate(nextRandom, length);
print("``text.padTrailing(20)`` - ``if (balanced(text)) then "OK" else "NOT OK"``");
}
}
Boolean balanced(String input)
=> !input
@gavinking
gavinking / permutations.ceylon
Last active August 29, 2015 14:27
permutations of a list, adapted from http://stackoverflow.com/a/28241384/2889760
shared void run() {
for (perm in permutations("ABCD").indexed) {
print(perm);
}
}
shared {Element[]*} permutations<Element>
(List<Element> list)
=> object satisfies {Element[]*} {
@gavinking
gavinking / formatFloat.ceylon
Last active October 5, 2015 18:14
format a floating point number
shared String formatFloat(
"The floating point value to format."
Float float,
"The minimum number of allowed decimal places.
If `minDecimalPlaces<=0`, the result may have no
decimal point."
Integer minDecimalPlaces=1,
"The maximum number of allowed decimal places.
"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])
@gavinking
gavinking / maven-log.txt
Last active November 2, 2015 15:24
Failing build
Gavins-New-MacBook-Pro:ceylon-dist gavin$ ant -Dbuild-against=last-release clean-ide eclipse
Buildfile: /Users/gavin/ceylon-dist/build.xml
clean-ide:
siblings:
clean:
clean-item:
@gavinking
gavinking / verbosity.md
Last active December 4, 2015 15:01
silly verbosity comparison

Someone just told me that implementing an interface in F# is less vebose than in Ceylon. Let's see if that's true. Pick an example given on MSDN. (Yes, I know it's silly to make such trivial comparisons.)

F#

@gavinking
gavinking / fizzbuzz.ceylon
Last active December 21, 2015 01:19
See how I use a comprehension to pass a lazily evaluated stream of fizzbuzzes to printAll()? Note that a more efficient implementation of printAll() could eaily avoid the string concatenation, thus taking much better advantage of the laziness.
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));