This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Usage: | |
-- runhaskell newyear.hs n | |
-- This will print a nice christmas tree. For n=5: | |
-- | |
-- * | |
-- * * | |
-- * * * | |
-- * * * * | |
-- * * * * * | |
-- Happy New Year! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList | |
fun main(args : Array<String>) { | |
val a: ArrayList<Int> = ArrayList() | |
a.add(null) // WTF?? | |
println(a) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |