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
value1 = if condition
# stuff
# stuff
true
else
# stuff
# stuff
false
end
int myFunction(int x)
{
if (x > 10)
return x;
if (x < 10)
return x + 1;
if (x = 10)
return 0;
}
class Person(firstName: String, lastName: String, age: Int) {
def fullName(implicit val titleProvider: () => String): String = {
val title = titleProvider()
LIST_STUFF_IN_SCOPE()
/*
This should list:
firstName, lastName, age, title, titleProvider
*/
@machisuji
machisuji / blocksOrMethods.st
Created June 11, 2013 22:50
Using methods instead of anonymous blocks.
append: aString to: anotherString
^ anotherString, ' ', aString
"..."
#('I' 'like' 'milk') inject: '' into: self class blockFor: #append.
" as opposed to "
#('I' 'like' 'milk') inject: '' into: [:sentence :word | self append: word to: sentence]
@machisuji
machisuji / add.rb
Created June 3, 2013 17:56
Parens are optional for both method invocation and definition in Ruby.
def add a, b
a + b
end
three = add 1, 2
Counting objects: 55, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (49/49), done.
error: RPC failed; result=22, HTTP code = 411
fatal: The remote end hung up unexpectedly
Writing objects: 100% (51/51), 7.83 MiB | 6.21 MiB/s, done.
Total 51 (delta 11), reused 0 (delta 0)
fatal: The remote end hung up unexpectedly
fatal: expected ok/error, helper said '4004s˥?kM+?J?W??z?/?k?ǣ)#?[??ww??J?a??'
import play.api.libs.json._
implicit object userToJson extends Writes[User] {
def writes(user: User): JsonValue = JsonObject(Seq(
"possiblyTotallyUnrelatedFieldName" -> JsString(user.name)
//, ...
))
}
def test = Action {
val page = new Page("public/test.html") {
$("p").clone(3)
}
Ok(page)
}
class Pool[T](values: Iterator[T], width: Int, height: Int) {
class Cell[T](value: T, neighbours: => Seq[Cell[T]]) {
lazy val Seq(top, right, bottom, left) = neighbours
}
val cells = for (x <- 0 until width) yield (
for (y <- 0 until height) yield
new Cell(values.next, Seq(cellAt(x, y - 1), cellAt(x + 1, y), cellAt(x, y + 1), cellAt(x - 1, y))))
def cellAt(x: Int, y: Int): Cell[T] = cells(translate(x, width))(translate(y, height))
val in: InputStream
val buffer: Array[Byte]
val stream: Stream[Array[Byte]] =
Stream.continually(in.read(buffer)).takeWhile(-1 !=).map(buffer take)
val iterator: Iterator[Array[Byte]] =
Iterator.continually(in.read(buffer)).takeWhile(-1 !=).map(buffer take)
stream.foreach(println) // the byte arrays stay
iterator.foreach(println) // the byte arrays go (some time)