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 / 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.
@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 / gist:8e6a019fcbbc474b7461
Last active February 29, 2016 16:42
IDE build errors
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
@gavinking
gavinking / xxxxByKey.ceylon
Last active March 22, 2016 10:22
reduceByKey() and foldByKey()
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)
@gavinking
gavinking / MaybeMonad.ceylon
Last active March 26, 2016 10:44
Emulating the Maybe monad in Ceylon
/*
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