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 / 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 {}
@gavinking
gavinking / vectors.ceylon
Last active December 31, 2015 02:08
Demonstration of how we can "fake" a dependently typed list in Ceylon.
"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 {}
@gavinking
gavinking / composition.ceylon
Last active December 27, 2015 11:39
We can use tuples to define functions with multiple return values. This code example shows how we can use the spread operator and function composition with such functions.
//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];
}
@gavinking
gavinking / tuplecomparatorconstructionkit.ceylon
Last active December 26, 2015 06:39
This demonstrates how we can abstract over tuple types of unknown length, without loss of typesafety, to do something useful, in this case, build comparator functions for tuple types.
/* 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;
@gavinking
gavinking / gist:6918556
Last active December 25, 2015 04:39
Parallel algorithm for π, originally by Russel Winder
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;
@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));
@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 / 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:
"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 / 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.