Skip to content

Instantly share code, notes, and snippets.

View machisuji's full-sized avatar

Markus Kahl machisuji

  • OpenProject GmbH
  • Cardiff, Wales, UK
View GitHub Profile
int myFunction(int x)
{
if (x > 10)
return x;
if (x < 10)
return x + 1;
if (x = 10)
return 0;
}
value1 = if condition
# stuff
# stuff
true
else
# stuff
# stuff
false
end
@machisuji
machisuji / params.scala
Last active September 3, 2015 23:57
optional parameters in Scala that are not a pain to write
import scala.language.implicitConversions
case class Optional[+T](value: Option[T])
val omit = Optional(None)
implicit def valueToOptional[T](value: T): Optional[T] = Optional(Some(value))
implicit def optionalToOption[T](opt: Optional[T]): Option[T] = opt.value
def add(a: Int, b: Optional[Int] = omit, c: Optional[Int] = omit): Int =
@machisuji
machisuji / Split alternating elements
Created February 25, 2010 22:03
Splitting a list into two lists containing alternating elements of the original list.
// way faster than split2m
def split2f[T](list: Seq[T]): (Seq[T], Seq[T]) = {
val it = Iterator.iterate(true)(!_)
list.partition(_ => it.next)
}
// next approach, incredibly slow
// @TODO learn functional programming
def split2m[T](list: Seq[T]): (Seq[T], Seq[T]) =
list.foldLeft((Seq[T](), Seq[T]())) {
(tuple: (Seq[T], Seq[T]), element: T) =>
@machisuji
machisuji / Java
Created May 30, 2010 07:30 — forked from rkh/Scala
import wandledi.java.Controller;
public class WandlediApp extends Controller {
public void index() {
getWriter().println("Hello World!");
}
public static void main(String[] args) {
runStandAlone(true);
@machisuji
machisuji / implicits.scala
Created July 16, 2010 17:54
trailing conditionals
implicit def trailingConditionals[T](any: => T) = new {
def provided(expr: Boolean) = if (expr) Some(any) else None
def unless(expr: Boolean) = if (!expr) Some(any) else None
}
implicit def verbosePlus(i: Int) = new {
def plus(j: Int): Int = {
println("Adding " + i + " and " + j);
i + j
}
def form() {
<any name="author"/> merge <any value="Bobby"/>
// vs
get("name" -> "author").setAttribute("value", "Bobby")
// vs
<where name="author" setValue="Bobby"/>!
// vs
<any name="author"/> update {
"value" -> "Bobby" // [,\n"foo" -> "bar" ...
}
def javaUsage() {
val model = new Model
model.onChange(new ChangeListener {
override def changed() {
// react
}
})
}
def scalaUsage() {
overriding method + in trait MapLike of type [B1 >: String](kv: (String, B1))scala.collection.mutable.Map[String,B1];
[error] method + has incompatible type
[error] override def +[B1 >: String](kv: (String, B1)): Parameters = {
[error] ^
if(isComposite) {
_compositeKeyMembers.set(Some(new ArrayBuffer[SelectElement]))
println("set members to empty ArrayBuffer")
}
var res = proxy.invokeSuper(o, args);
if(isComposite) {
val ck = res.asInstanceOf[CompositeKey]
println("members before: " + ck._members)
ck._members = Some(_compositeKeyMembers.get.get.map(new SelectElementReference[Any](_)(NoOpOutMapper)))