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 / README.md
Last active February 7, 2018 09:42
Ceylon Web Runner: Records

Typesafe Records for Ceylon

The example demonstrates the use of Ceylon's powerful type system to define typesafe heterogenous maps, otherwise known as record types.

  • records.ceylon defines a tiny library for creating records
  • keys.ceylon defines example typesafe keys
@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 / README.md
Created August 13, 2017 14:56 — forked from ceylonwebide/README.md
Ceylon Web Runner: Solar System

Ceylon Solar System Example

This little example application demonstrates the use of Ceylon as an alternative to client-side JavaScript. In particular, the example shows:

  • a simple, typesafe, object-oriented program,
  • dependence on an external [cross-platform math module][xmath] written in Ceylon and hosted on
@gavinking
gavinking / proxy.ceylon
Last active December 6, 2016 19:13
Using Rank-2 polymorphic generic functions and JavaScript Proxy to create a typesafe proxy API for Ceylon objects
import ceylon.collection {
ArrayList
}
"Create a typesafe proxy for the given [[instance]], that
intercepts method calls and attribute evaluations."
Instance proxy<Instance>(Instance instance)(call, get) {
"Intercepts method invocations. Delegate to the method
of [[target]] by calling `method(args)`."
@gavinking
gavinking / main.ceylon
Created December 1, 2016 20:53
Ceylon Web Runner: Converting JavaScript Date to Ceylon DateTime
import ceylon.time { Instant, DateTime }
shared void run() {
dynamic {
//create a JavaScript Date
dynamic date = Date();
print(date);
//convert it to a Ceylon DateTime
DateTime dt = Instant(date.getTime()).dateTime();
print(dt);
@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.

@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 / 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 / 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 / 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