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 / 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 / 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 / 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 / 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 / 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 / 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 / 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
@gavinking
gavinking / README.md
Last active October 17, 2016 07:18
Ceylon Web Runner: Ceylon Services

Ceylon Services Example

This tiny example demonstrates the use of Ceylon [service providers][] in the JavaScript environment. Service providers are an abstraction of, and [interoperate][] with, Java's [service loaders][], but are not tied to the Java platform, and can even work cross-platform.