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
@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)))
@machisuji
machisuji / OptionOptions.scala
Created March 11, 2011 07:03
How should you do it?
val msg: Option[String] = flash.get("msg")
// Do it like this?
$(".flashMessage") { node =>
if (msg.isDefined) {
node.text = msg.get
} else node.hide()
}
// Or like this?
Thread.start {
println("Waiting for Godot")
synchronized ("Godot") {
"Godot".wait()
}
println("Godot never came")
}
println("Press <return> to continue")
new BufferedReader(new InputStreamReader(System.in)).readLine()
["/Volumes/finn/fitusagereporting/vendor/plugins/reporting_engine/lib/report/result.rb:160:in `sort!'",
"/Volumes/finn/fitusagereporting/vendor/plugins/reporting_engine/lib/report/result.rb:158:in `sort!'",
"/Volumes/finn/fitusagereporting/vendor/plugins/reporting_engine/lib/report/walker.rb:76:in `sort'",
"/Volumes/finn/fitusagereporting/vendor/plugins/reporting_engine/lib/report/walker.rb:36:in `headers'",
"/Volumes/finn/fitusagereporting/app/views/usage_reports/_usage_report_table.rhtml:42:in `_app_views_usage_reports__usage_report_table_rhtml__1158284054_2182735320_2781310'",
"/Users/markus/.rvm/gems/ree-1.8.7-2011.03/gems/actionpack-3.0.4/lib/action_view/template.rb:135:in `send'",
"/Users/markus/.rvm/gems/ree-1.8.7-2011.03/gems/actionpack-3.0.4/lib/action_view/template.rb:135:in `render'",
"/Users/markus/.rvm/gems/ree-1.8.7-2011.03/gems/activesupport-3.0.4/lib/active_support/notifications.rb:54:in `instrument'",
"/Users/markus/.rvm/gems/ree-1.8.7-2011.03/gems/actionpack-3.0.4/lib/action_view/tem