Skip to content

Instantly share code, notes, and snippets.

View klaeufer's full-sized avatar
:octocat:

Konstantin Läufer klaeufer

:octocat:
View GitHub Profile
@klaeufer
klaeufer / ThreadSafety.scala
Last active March 13, 2026 21:39
Unsafe and safe concurrent threads for experimenting in the Scala REPL
var shared = 0
val lock = new java.lang.Object
def unsafe =
val local = shared
Thread.sleep(0)
shared = local + 1
def safe =
lock.synchronized:
@klaeufer
klaeufer / monoidExample.scala
Last active December 30, 2015 19:59
minimal scalaz monoid example
import scalaz._ // general scalaz support
import syntax.monoid._ // monoid syntax as used below
import std.anyVal._ // some instances to work with
import std.list._ // some more instances
import std.option._ // some more instances
import syntax.std.option._ // postfix methods for options
object monoidExample {
val res0 = ∅[Int] // or: mzero[Int]
val res1 = 3 |+| 4 // or: 3 `mappend` 4
@klaeufer
klaeufer / IntAbelianGroupScalaCheck.scala
Created December 9, 2013 22:10
minimal ScalaCheck example
import org.scalacheck.Properties
import org.scalacheck.Prop.forAll
object IntAbelianGroup extends Properties("Int") {
property("Associativity") = forAll { (a: Int, b: Int, c: Int) =>
(a + b) + c == a + (b + c)
}
}
// Closure
@klaeufer
klaeufer / .gitignore
Created November 12, 2014 18:47
.gitignore for F# development
*.suo
*.obj
*.pdb
*.user
*.vspscc
*.bak
*.cache
*.log
*.lib
[Bb]in
@klaeufer
klaeufer / .gitignore
Created November 12, 2014 18:47
.gitignore for Haskell development
a.out
*.swp
*.o
*.orig
*.hi
*.lkshs
*.iml
.idea/
dist/
out/
@klaeufer
klaeufer / createBintrayPackage.sh
Created November 22, 2014 16:52
Create Bintray package using curl and REST API
#!/bin/bash
# put in ~/.curlrc: user=BintrayUser:BintrayAPIKey
ORG=$1
PACKAGE=$2
curl -i -H "Content-Type: application/json" -d "{ \"name\":\"${PACKAGE}\",\"labels\":[\"android\",\"scala\",\"cse\",\"mobile\",\"oop\",\"designpatterns\"],\"website_url\":\"http://lucoodevcourse.github.io\",\"issue_tracker_url\":\"https://github.com/${ORG}/${PACKAGE}/issues\",\"github_repo\":\"${ORG}/${PACKAGE}\",\"github_release_notes_file\":\"README.md\",\"public_download_numbers\":true,\"public_stats\":true,\"vcs_url\":\"https://github.com/${ORG}/${PACAKGE}\",\"licenses\":[\"MIT\"] }" https://bintray.com/api/v1/packages/${ORG}/generic
def pi(n: Long): Double = {
var i = 0L
var c = 0L
while (i < n) {
val x = math.random
val y = math.random
if (x * x + y * y <= 1) c += 1
i += 1
}
4.0 * c / n
@klaeufer
klaeufer / install-android-sdk.sh
Last active August 29, 2015 14:16
Android SDK install script for Ubuntu on Codio
#!/bin/bash
# Download and run this script in a Codio terminal.
# (Workaround for Codio included at the bottom.)
#
# You should set these variables in /home/codio/.bashrc:
#
# export ANDROID_HOME=$HOME/android-sdk-linux
# export LD_LIBRARY_PATH=$HOME/lib32:$LD_LIBRARY_PATH
#
val lines = scala.io.Source.stdin.getLines
var count = 0
for (l <- lines) {
println(l)
count += 1
}
println("count = " + count)
@klaeufer
klaeufer / gist:867abb57dddc1b247b52
Last active August 29, 2015 14:17
Scala Iterator loop idiom (stateless): repeatedly apply an endofunction until a condition is met
scala> def f(i: Int) = i * i
f: (i: Int)Int
scala> Iterator.iterate(2)(f)
res11: Iterator[Int] = non-empty iterator
scala> res11 takeWhile { _ < 10 } foreach { println }
2
4