Skip to content

Instantly share code, notes, and snippets.

View orionll's full-sized avatar

jkozlov orionll

View GitHub Profile
@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)
}
fun main(args : Array<String>) {
val arr = array(1, 2, 3)
println(foldLeft(0, arr, {x, y -> x + y}))
}
fun foldLeft<A, B>(z: B, array: Array<A>, f: (A, B) -> B): B {
[tailRecursive] fun go(i: Int, z: B) =
if (i == array.size()) z
else go(i + 1, f(array.get(i), z))
@orionll
orionll / ListFiles.scala
Last active August 29, 2015 14:15
List files purely
import java.nio.file.DirectoryStream
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import scalaz.concurrent.Task
import scalaz.std.anyVal._
import scalaz.stream.Cause
import scalaz.stream.Process
import scalaz.stream.io
@orionll
orionll / Rpc.hs
Last active August 29, 2015 14:16
Remote Procedure Call using Free monads
{-# LANGUAGE DeriveFunctor #-}
import Control.Monad.Free
data Response =
Str { string :: String }
| Boolean { bool :: Bool }
| List { stringList :: [String] }
data Request a = Request String [String] (Response -> a) deriving Functor
@orionll
orionll / MyBenchmark.java
Created March 26, 2015 12:08
Arrays.asList().contains() vs Ints.contains
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import com.google.common.primitives.Ints;
Benchmark (value) Mode Cnt Score Error Units
MyBenchmark.benchmarkReturnOrdinal 3 avgt 5 2,152 ? 0,018 ns/op
MyBenchmark.benchmarkReturnOrdinal 2 avgt 5 2,193 ? 0,338 ns/op
MyBenchmark.benchmarkReturnOrdinal 1 avgt 5 2,150 ? 0,017 ns/op
MyBenchmark.benchmarkReturnReference 3 avgt 5 2,408 ? 0,022 ns/op
MyBenchmark.benchmarkReturnReference 2 avgt 5 2,432 ? 0,150 ns/op
MyBenchmark.benchmarkReturnReference 1 avgt 5 2,407 ? 0,017 ns/op
@orionll
orionll / StrLength.hs
Created April 27, 2015 11:59
Calculate string length with the Reader monad
import qualified Control.Monad.Trans.Reader as R
strLength :: R.Reader String Int
strLength = do
s <- R.ask
case s of
[] -> return 0
(_:cs) -> fmap (+1) (R.local (const cs) strLength)
main = do