Skip to content

Instantly share code, notes, and snippets.

@Villane
Villane / IfThenElse.ops
Last active December 16, 2015 17:59
Syntax sugar: if type () -> X (nullary function returning X) is expected and we have an expression of type X, convert the expression into a lambda: () -> expr This allows custom functions to implement control structures like if-then-else, where the branches are evaluated only if needed
operators IfThenElse
group ifthenelse = "If…Then…Else…"
ifthenelse -> arithmetic, relations, brackets
@Villane
Villane / Range.sl
Last active December 15, 2015 23:39
Another Slang example. As there are no mutable members / variables (yet), we must use functional concepts for things like iteration. This program prints 12345 twice. Due to use of names like next() and hasNext(), the content of the loop should be before the while statement. The loop will be continued with the Range returned from i.next() and so …
╒═════════════════════════╕
╎ Range class ╎
╘═════════════════════════╛
val class Range (from: Int, to: Int) {
def hasNext() = from < to
def valid() = from <= to
def current() = from
def next() = Range(from + 1, to)
}
@Villane
Villane / complex.ops
Created April 8, 2013 20:48
An example of complex numbers syntax in Slang
// Add some prefix operators to the grammar
group complexPre = "ℜ…", "ℑ…", "Re…", "Im…"
// Make them bind tighter than anything, so we don't need parens
control -> complexPre
relations -> complexPre
arithmetic -> complexPre
brackets -> complexPre
@Villane
Villane / gist:3767858
Created September 22, 2012 21:13
2x2 Matrix implementation in Slang
val class Mat2 Double⁴ {
// instead of Double⁴ we could also write ⟨a: Double, b: Double, c: Double, d: Double⟩
// then we'd have access to the elements with a, b, c, d instead of A₀, A₁, A₂, A₃
private alias A = data // `data` is alias for `this`, but with type ⟨Double,Double,Double,Double⟩ or Double⁴
def subscript(i: Int) = A// a vector Double⁴ already has a subscript method, we expose it
def +(M: Mat2) = Mat2[A + M] // expose Double⁴.+, with the right type (PS there's implicit Mat2 -> Double⁴)
def -(M: Mat2) = Mat2[A - M] // expose Double⁴.-, with the right type
def *(x: Double) = Mat2[A * ⟨x,x,x,x⟩]
@Villane
Villane / gist:1859745
Created February 18, 2012 15:16
Hmm... what if Numeric types had an apply method
//Just had an idea. What if numeric types such as Int and Float had an apply method.
implicit def juxtaposition_int(x: Int) = new {
def apply(y: Int) = x * y
}
2 ( 1 + 1 ) == 4
@Villane
Villane / gist:1680464
Created January 26, 2012 02:04
foldLeft for int arrays in Slang (there are no generics yet) (syntax highlight set to Scala because it's most similar)
def foldLeft(z: Int, a: [Int], f: (Int, Int) -> Int) =
loop(i := 0, acc := z) while i < #a do
(i + 1, f(acc, aᵢ))
yield acc
def (a: [Int]) = foldLeft(0, a, (x, y) -> x + y)
@Villane
Villane / gist:1088163
Created July 17, 2011 22:26
Klang: Classes + some math syntax experiments
class Vector2 {
data (x: Double, y: Double)
// other methods skipped
def length() = √(x² + y²)
alias || = length
}
def computeVectorLength(): Double = {
@Villane
Villane / gist:1079151
Created July 12, 2011 22:34
Klang: Function Passing
// apply the function 'f' twice to the parameter 'n'
def twice(n: Int, f: (Int) -> Int) = f(f(n))
// add one to the parameter
def addOne(n: Int) = n + 1
// return an initial value and a function
def getValueAndFunction(): (Int, myFunc: (Int) -> Int) = (1, addOne)
// run twice(...) with the initial value and function from getValueAndFunction()
@Villane
Villane / GenericTypeResolver.java
Created March 15, 2010 18:55
Licensed under Apache Software License 2.0
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.HashMap;
import java.util.Map;
import javax.persistence._
import org.eclipse.persistence.annotations.Converter
import org.eclipse.persistence.annotations.Convert
class Entity {
@Column(nullable=false, updatable=false)
@Converter(name="jodadatetime", converterClass=classOf[JodaTimeConverter])
@Convert("jodadatetime")
var created: org.joda.time.DateTime = _
}