Skip to content

Instantly share code, notes, and snippets.

View abreslav's full-sized avatar

Andrey Breslav abreslav

View GitHub Profile
package sample.hello
import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor.UntypedActor
import akka.actor.UntypedActorContext
import akka.actor.ActorPath
import scala.concurrent.duration.Duration
import java.util.HashMap
import akka.actor.ActorSystem
fun Int.divides(d: Int) = this % d == 0
fun fizzbuzz(n: Int) = when {
n divides 15 -> "fizzbuzz"
n divides 3 -> "fizz"
n divides 5 -> "buzz"
else -> "$n"
}
fun result(max: Int) = (0..max) map { n -> fizzbuzz(n) }
/*
Play with this code at
[http://goo.gl/yPtkb](Kotlin Web Demo)
*/
@abreslav
abreslav / Sample.java
Last active December 16, 2015 03:39
A question about getClass()
/*
* A question about Java (not Kotlin!), first asked by Alexander Udalov (@udalov).
*
* How many calls to getClass() does the code below make?
* Curiously enough, the answer is ONE (see the byte codes below).
*
* Now the questions is: why does javac emit the call to getClass()?
*/
class C {
procedure permutations(var a: array[1..N] of Integer; pos: Integer);
begin
if pos >= N then begin
for i := 1 to N do
Write(a[i], ' ');
WriteLn;
end;
for i := 1 to N do begin
a[i] := i;
@abreslav
abreslav / nlr.kt
Created October 10, 2013 11:24
Non-local returns in Kotlin
// Sometimes you are inside a lambda and want to return a value from some surrounding function (or surrounding lambda)
fun foo(): Any? {
return withNLR(default = 25) {
listOf(1, 2, 3).forEach {
if (it > 2) doReturn(it)
}
}
}
@abreslav
abreslav / kotlin-2013-14.markdown
Last active December 26, 2015 20:39
Kotlin Project Theme(s) for Open Academy - 2013/14

The Kotlin Programming Language

Kotlin is a modern statically typed programming language targeting the Java and JavaScript platforms. It is conceived as a "modern language for industry", keeping a balance between flexibility and readability, expressiveness and safety.

Project Overview

A modern programming language is much more than a grammar. Principal susystems of Kotlin include

  • Compiler front-end
  • Lexer/Parser translates text into a syntax tree, reports syntax errors
package ru.ifmo.rain.adsl;
import java.io.FileOutputStream
fun main(args: Array<String>) {
val generator = Generator(FileOutputStream("out.kt"), args[0], args[1])
generator.run()
}
class Bar {
fun invoke(x: Int): Int = x
}
class Foo {
val get: Bar = Bar()
}
fun foo () {
Ref should exist for this node. Hash: 603812
java.lang.Throwable
at com.intellij.openapi.diagnostic.Logger.error(Logger.java:113)
at com.intellij.vcs.log.graph.mutable.GraphBuilder.createBranch(GraphBuilder.java:123)
at com.intellij.vcs.log.graph.mutable.GraphBuilder.addCurrentCommitAndFinishRow(GraphBuilder.java:107)
at com.intellij.vcs.log.graph.mutable.GraphBuilder.append(GraphBuilder.java:165)
at com.intellij.vcs.log.graph.mutable.GraphBuilder.runBuild(GraphBuilder.java:198)
at com.intellij.vcs.log.graph.mutable.GraphBuilder.build(GraphBuilder.java:29)
at com.intellij.vcs.log.data.DataPack.build(DataPack.java:43)
at com.intellij.vcs.log.data.VcsLogDataHolder.smartRefresh(VcsLogDataHolder.java:404)