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
Element[] quicksort<Element>({Element*} elements)
given Element satisfies Comparable<Element> =>
if (exists first = elements.first)
then [first.largerThan, first.notLargerThan]
.map(compose(quicksort<Element>, elements.rest.filter))
.interpose([first])
.reduce(concatenate<Element>)
else [];
@jvasileff
jvasileff / buzzwordCompliance.ceylon
Created January 20, 2015 18:45
Ceylon + Spring: Declarative Transactions, DI, and AOP
import ceylon.collection {
LinkedList
}
import ceylon.dbc {
Sql,
newConnectionFromDataSource
}
import ceylon.interop.java {
javaClass
}
@renatoathaydes
renatoathaydes / ScalaVSCeylon.scala
Last active October 17, 2018 08:29
Scala VS Ceylon
// This is a comparison between Scala and Ceylon based on this previous comparison I made between Haskell and Groovy:
// https://gist.github.com/renatoathaydes/5078535
// Ex 1. If we have two lists, [2,5,10] and [8,10,11] and we want to get the products of all the possible
// combinations between numbers in those lists, here's what we'd do.
/* SCALA */
for { x <- List(2,5,10); y <- List(8,10,11) } yield x*y
@sgalles
sgalles / i18nUsecase.ceylon
Created November 15, 2013 22:41
Something I've always wanted to do in Java : union of enums. Here, applied to a simple use case : exhaustive (checked by compiler) i18n of an app, with keys coming from different enums.
// enum 1 of application i18n keys
abstract class AppKeyi18n()
of title | description {}
object title extends AppKeyi18n() {}
object description extends AppKeyi18n() {}
// enum 2 of user i18n keys
abstract class UserKeyi18n()
of fieldText1 | fieldText2 | fieldText3 {}
@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));