Skip to content

Instantly share code, notes, and snippets.

View Dierk's full-sized avatar

Dierk König Dierk

View GitHub Profile
@Dierk
Dierk / ModularGroovyTraits
Created April 6, 2014 19:23
Using modular traits in Groovy 2.3.0-beta-1
trait HasId {
long id
}
trait HasVersion {
long version
}
trait Persistent {
boolean save() { println "saving ${this.dump()}" }
}
trait Entity implements Persistent, HasId, HasVersion {
@Dierk
Dierk / GroovyDelegatingTrait
Created April 8, 2014 20:48
Using Groovy traits with @DeleGate AST transform
// has all methods of List
trait ListLike {
@Delegate List linkedList = new LinkedList()
}
// stores anything but exposes only Strings
class StringList implements ListLike {
String get(int index) { linkedList.get(index).toString() }
}
module realworld.experiments.ExampleTests where
test_that_is_expected_to_pass = 1 == 1
test_that_is_expected_to_fail = 1 == 2
//@Grab('org.spockframework:spock-core:0.7-groovy-2.0')
import spock.lang.*
import java.lang.reflect.Field
class FregeSpec extends Specification {
@Shared Class testClass = realworld.experiments.ExampleTests
@Shared List<Field> testFields = testClass.fields.findAll { Field it -> it.modifiers == 25 && it.name.startsWith("test") }
@Unroll
@Dierk
Dierk / gist:358a2bf24cdcd10044e8
Created October 27, 2014 22:24
A NullSafe Monad in Groovy with Java 8
import java.util.function.Function // requires Java 8
class NullSafe<T> {
protected final T t
protected NullSafe(T t) {this.t = t}
static NullSafe<T> enter(T t) { new NullSafe<T>(t) } // enter or "return" is like a constructor
// in the general case, applicative would have the type Function<T, NullSafe<T>> but we don't need that here
NullSafe<T> rightShift (Function<T,T> applicative) {
@Dierk
Dierk / lab4-frege-template.fr
Created November 16, 2014 22:24
Frege template for the recursive functions lab (section 8 "interactive programs") with quickcheck support
module Lab4 where
------------------------------------------------------------------------------------------------------------------------------
-- RECURSIVE FUNCTIONS, Frege version by Dierk Koenig
------------------------------------------------------------------------------------------------------------------------------
-- import frege.prelude.Math(pi) -- uncomment this when trying to use pi
-- ===================================
-- Ex. 0
@Dierk
Dierk / ParsingTemplate.fr
Created November 16, 2014 20:42
Frege Template for the monadic parser exercises of the FP101x course
-- Functional parsing library from chapter 8 of Programming in Haskell,
-- Graham Hutton, Cambridge University Press, 2007.
-- Template file for the homework in the FP101x EdX course.
-- Read also: http://www.cs.nott.ac.uk/~gmh/monparsing.pdf
-- adapted to Frege by Dierk Koenig
-- uncomment code as needed
module Parsing where
import Data.Char
@Dierk
Dierk / CountdownTemplate.fr
Created November 20, 2014 22:11
Template for the Countdown problem as stated in homework 10 for the FP101x course at edX
module Countdown where
-- Countdown example from chapter 11 of Programming in Haskell,
-- Graham Hutton, Cambridge University Press, 2007.
-- Template for homework 10 in the FP101x course
-- Frege adaptions by Dierk Koenig
-- How Java's Date looks like through the Frege glasses
data Date = native java.util.Date where
@Dierk
Dierk / lab6-frege-template.fr
Created December 4, 2014 13:00
Lab 6 for the FP101x course about Rose trees, functors, monoids, and foldables in Frege
------------------------------------------------------------------------------------------------------------------------------
-- ROSE TREES, FUNCTORS, MONOIDS, FOLDABLES, Frege version by Dierk Koenig
------------------------------------------------------------------------------------------------------------------------------
module Lab6 where
import frege.prelude.Math(round, sin)
data Rose a = Rose a [Rose a]
derive Show Rose a
@Dierk
Dierk / FunctionRecord.fr
Created March 18, 2015 09:02
Protype-base "subclassing" with immutable records
module FunctionRecord where
data FRecord = FRecord {
parent :: FRecord,
name :: String,
toString :: (FRecord -> String)
}
prototype = FRecord { parent = prototype, name = undefined, toString = (\r -> r.name) }
dierk = prototype.{ name = "Dierk" }