Skip to content

Instantly share code, notes, and snippets.

@ninthdrug
ninthdrug / gist:5bbeeb78a6f14a96aa85bcc2d6b48791
Last active February 9, 2017 22:34
gcd and lcm in haskell
gcd a b =
let r = a `mod` b
in if r == 0 then b else gcd b r
lcm a b = a * b / (gcd a b)
Handler mHandler;
public void useHandler() {
mHandler = new Handler();
mHandler.postDelayed(mRunnable, 1000);
}
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
@ninthdrug
ninthdrug / gist:1130f64f27c0df90c2fd
Created July 13, 2015 10:37
android status bar height
DisplayMetrics displayMetrics = new DisplayMetrics();
((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displayMetrics);
int statusBarHeight;
switch (displayMetrics.densityDpi) {
case DisplayMetrics.DENSITY_HIGH:
statusBarHeight = HIGH_DPI_STATUS_BAR_HEIGHT;
break;
case DisplayMetrics.DENSITY_MEDIUM:
@ninthdrug
ninthdrug / patch_classpath.scala
Created November 15, 2012 11:59
Script to patch the classpath in a Weblogic Server config.xml file
// script to patch the classpath in a Welogic Server config.xml file
import scala.xml._
import java.io._
def patchClassPath(configFile: String, edit: (String) => String) {
// inner function to apply the passed in edit function to the classpath
def patch(node: Node): Node = {
def patchElem(elem: Elem): Elem =
elem.copy(child = elem.child.map(patch(_)))
@ninthdrug
ninthdrug / rock_paper_scissors.scala
Created November 13, 2012 22:00
Rock Paper Scissors in Scala
// rock paper scissors in scala
import scala.util.Random
def rockPaperScissors(): String = {
Random.nextDouble match {
case d if d < 1.0 / 3 => "rock"
case d if d < 2.0 / 3 => "paper"
case _ => "scissors"
}
@ninthdrug
ninthdrug / parse_properties.scala
Last active October 18, 2019 06:35
Parsing java properties files with Scala
import scala.io.Source.fromFile
def parseProperties(filename: String): Map[String,String] = {
val lines = fromFile(filename).getLines.toSeq
val cleanLines = lines.map(_.trim).filter(!_.startsWith("#")).filter(_.contains("="))
cleanLines.map(line => { val Array(a,b) = line.split("=",2); (a.trim, b.trim)}).toMap
}