This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
operators IfThenElse | |
group ifthenelse = "If…Then…Else…" | |
ifthenelse -> arithmetic, relations, brackets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
╒═════════════════════════╕ | |
╎ 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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⟩] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Vector2 { | |
data (x: Double, y: Double) | |
// other methods skipped | |
def length() = √(x² + y²) | |
alias |…| = length | |
} | |
def computeVectorLength(): Double = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = _ | |
} |
NewerOlder