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 / 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 / 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 / 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 / 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 / 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
}
"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 / 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 / memoize.ceylon
Last active September 7, 2017 15:21
A useful generic function to memoize a function or method, or canonicalize a class. (Tested on Ceylon 1.1 - may not work in 1.0.)
"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
@gavinking
gavinking / mapLagging.ceylon
Last active January 3, 2016 02:09
A cool way to define moving averages, demonstrating a very advanced use of reified generics.
import ceylon.collection {
LinkedList
}
import ceylon.math.float {
sin,
pi
}
"Map a function of arity `n` to a stream of values. In each
@gavinking
gavinking / genericVectors.ceylon
Last active January 1, 2016 17:19
Generalization of Vec over element types.
"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 {}