Skip to content

Instantly share code, notes, and snippets.

@sgalles
sgalles / gist:3a9e575ad9f1a68328f1
Last active August 29, 2015 14:12
Ceylon fibonaccIterable with Curry+Let+Destructuring
value fiboLoop = curry(loop<Integer[2]>)([0,1]);
{Integer[2]+} fibonaccIterable =
fiboLoop((pair) => let([current, next] = pair) [next, current+next] );
printAll(fibonaccIterable.take(100)*.first);
@sgalles
sgalles / ceylon.sh
Created June 5, 2014 20:18
Ceylon start script with cygwin support
#!/bin/sh
# resolve links - $0 may be a softlink
PRG="$0"
while [ -h "$PRG" ]; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
"Create a comparator from an Iterable of Comparators"
shared class Comparing<Element>({Comparison(Element,Element)+} comparators){
alias Comparator => Comparison(Element,Element);
Comparator accumulating(Comparator partial, Comparator elem) {
Comparison newPartial(Element x, Element y){
value comparison = partial(x,y);
return comparison != equal then comparison else elem(x,y);
"zip two already sorted `Iterables` using the `comparing` method
to match the elements of the Iterables. All elements are kept.
The original sort (ascending) of the two Iterables must be consistent with the `comparing`
method."
shared {[First?, Second?]*} smartZipOr<First, Second>
( Comparison comparing(First x, Second y))
@sgalles
sgalles / comparing.ceylon
Last active August 29, 2015 13:57
Ceylon class that mimic the new Comparator#comparing method from Java 8
"Create comparators a la Java 8 Comparator#comparing"
class Comparing<Element>(_comparator){
variable Comparison(Element,Element) _comparator;
shared Comparison(Element,Element) comparator => _comparator;
shared Comparing<Element> thenComparing(Comparison comparing(Element x, Element y)){
value currentComparator = comparator;
import com.yammer.dropwizard {...}
import com.yammer.dropwizard.config { ... }
import com.fasterxml.jackson.annotation {jsonProperty}
import org.hibernate.validator.constraints {notEmpty}
import java.lang { JavaString = String , ObjectArray}
import ceylon.interop.java { ... }
import java.util.concurrent.atomic { ... }
import javax.ws.rs { get = gET, ... }
import com.google.common.base { Optional }
import com.yammer.metrics.core { HealthCheck }
@sgalles
sgalles / mavenToCeylonRepository.gradle
Last active August 29, 2015 13:56
This is a quick and dirty Gradle script that can be used to pull maven artifacts from a repository and locally create a complete Ceylon repository with the same transitive dependencies. The script also create fatflat.jar : it is a jar with all dependencies merged that can be used to peform flat classpath tests.
// usage : save this script as 'build.gradle' and run 'gradle' in the directory of this script
configurations { compile }
dependencies{
//compile 'com.yammer.dropwizard:dropwizard-core:0.6.2'
compile 'com.octo.android.robospice:robospice-spring-android:1.4.9'
//compile 'org.hibernate:hibernate-core:4.3.0.Final'
}
repositories{ mavenCentral() }
@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 {}
@sgalles
sgalles / androidactivity.ceylon
Created November 13, 2013 22:34
Nothing more that a proof of concept, this is a basic Android activity written in Ceylon (uses the new syntax for Java interop static inner class/fields of Ceylon 1.0.0). Works with and Android SDK 19.
import android.app { Activity }
import android.os { Bundle }
import android.view { Menu }
import android.widget { TextView }
import java.lang { JString=String }
shared class Foo() extends Activity() {
shared actual void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);