Skip to content

Instantly share code, notes, and snippets.

View orionll's full-sized avatar

jkozlov orionll

View GitHub Profile
@orionll
orionll / gist:9708659
Created March 22, 2014 15:07
Effective fold() implementation for IndexedSeq
def myFold[A](seq: IndexedSeq[A])(z: A)(f: (A, A) => A): A = {
// Divide the sequence into to halfes and then combine the results
def recur(from: Int, to: Int): A = {
if (from > to) {
z
} else if (from == to) {
seq(from)
} else {
f(recur(from, from + (to - from) / 2), recur(from + (to - from) / 2 + 1, to))
}
@orionll
orionll / Main.java
Created March 24, 2014 17:02
Pattern matching in Java via exceptions
public class Main {
public static void main(String[] args) {
System.out.println(example());
}
public static String example() {
try {
throw match();
} catch (Int x) {
@orionll
orionll / Main.java
Last active August 29, 2015 13:57
Example which demonstrates how Scala scales out while Java does not
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import com.google.common.base.Stopwatch;
import com.google.common.util.concurrent.Uninterruptibles;
// Тайпкласс Animal (для полноты картины добавил метод hello, иначе был бы совсем тривиальный пример)
// Animal полностью абстрагирован от низлежашего типа A
trait Animal[A] {
def word: String
def talk() { println(word) }
def hello(a: A): String
}
// Обертка над тайпклассом Animal, чтобы можно было писать cat.hello, а не animal.hello(cat)
// AnimalOps полностью дублирует методы, определённые в Animal, но также может содержать еще и дополнительные методы.
import scalaz.NonEmptyList
import scalaz.syntax.monad._
import scalaz.syntax.equal._
import scalaz.syntax.foldable._
import scalaz.std.option._
import scalaz.std.list._
import scalaz.std.list.listSyntax._
import scalaz.std.anyVal._
import scalaz.std.string._
import scalaz.effect.IO
@orionll
orionll / FizzBuzz.scala
Created December 9, 2014 14:57
FizzBuzz implementation with Scalaz
import scalaz.Applicative
import scalaz.syntax.enum._
import scalaz.syntax.show._
import scalaz.std.anyVal._
import scalaz.effect.IO
import scalaz.effect.IO._
object Main extends App {
def fizzBuzz(i: Int): String = if (i % 3 === 0 && i % 5 === 0) "FizzBuzz"
@orionll
orionll / newyear.hs
Created December 29, 2014 17:50
Christmas tree in Haskell
-- Usage:
-- runhaskell newyear.hs n
-- This will print a nice christmas tree. For n=5:
--
-- *
-- * *
-- * * *
-- * * * *
-- * * * * *
-- Happy New Year!
// Java 1.8
roles.addAll(authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList()));
// Java 1.5
for (GrantedAuthority authority : authentication.getAuthorities()) {
roles.add(authority.getAuthority());
}
@orionll
orionll / bubblesort.hs
Created January 1, 2015 13:33
In-place bubble sort
import Data.Array.ST
import Data.Array.Base
import Control.Monad.ST
sortPair arr (i, j) = do x <- readArray arr i
y <- readArray arr j
if (x > y) then do writeArray arr i y
writeArray arr j x
else return ()
import java.util.ArrayList
fun main(args : Array<String>) {
val a: ArrayList<Int> = ArrayList()
a.add(null) // WTF??
println(a)
}