Skip to content

Instantly share code, notes, and snippets.

View hrj's full-sized avatar
🔭
observing

hrj hrj

🔭
observing
View GitHub Profile
@stepancheg
stepancheg / gist:167566
Created August 14, 2009 01:15
How to convert Scala XML to JDK DOM
object XmlHelpers {
val docBuilder =
javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder()
}
class NodeExtras(n: Node) {
def toJdkNode(doc: org.w3c.dom.Document): org.w3c.dom.Node =
n match {
case Elem(prefix, label, attributes, scope, children @ _*) =>
// XXX: ns
@asmallteapot
asmallteapot / brainerd.py
Created January 24, 2011 15:42
Simple Markdown server in Python. Assumes you have Markdown files in ~/Text. Access ~/Text/Hello.mdown at http://localhost:5000/w/Hello.
#!/usr/bin/env python
# -*- coding: utf8 -*-
from flask import Flask, redirect, url_for
from markdown import markdown
import os
import re
# create the app
# TODO: load config/template from files, with fallbacks
@akihiro4chawon
akihiro4chawon / parasort.scala
Created May 19, 2011 16:09
practical implementation of parallel sorting algorithm in Scala
import java.util.Arrays
abstract class Sorter {
def sorted(a: Array[Int]): Array[Int]
}
object SimpleSorter extends Sorter {
def sorted(a: Array[Int]) = {
Arrays.sort(a)
a
@borismus
borismus / gist:1032746
Created June 18, 2011 02:46
Convert a base64 string into a binary Uint8 Array
var BASE64_MARKER = ';base64,';
function convertDataURIToBinary(dataURI) {
var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
var base64 = dataURI.substring(base64Index);
var raw = window.atob(base64);
var rawLength = raw.length;
var array = new Uint8Array(new ArrayBuffer(rawLength));
for(i = 0; i < rawLength; i++) {
@viktorklang
viktorklang / ScalaEnum.scala
Created June 30, 2011 23:12
DIY Scala Enums (with optional exhaustiveness checking)
trait Enum { //DIY enum type
import java.util.concurrent.atomic.AtomicReference //Concurrency paranoia
type EnumVal <: Value //This is a type that needs to be found in the implementing class
private val _values = new AtomicReference(Vector[EnumVal]()) //Stores our enum values
//Adds an EnumVal to our storage, uses CCAS to make sure it's thread safe, returns the ordinal
private final def addEnumVal(newVal: EnumVal): Int = { import _values.{get, compareAndSet => CAS}
val oldVec = get
@timaschew
timaschew / InteractiveGnuPlot.java
Created July 12, 2011 17:30
Using library JavaPlot for interactive rotating a Gnuplot in a JPanel
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import com.panayotis.gnuplot.JavaPlot;
import com.panayotis.gnuplot.swing.JPlot;
// include JavaPlot.jar from this site to your classpath
// http://gnujavaplot.sourceforge.net/JavaPlot/About.html
@viktorklang
viktorklang / minscalaactors.scala
Last active March 25, 2024 19:01
Minimalist Scala Actors
/*
Copyright 2012-2021 Viktor Klang
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
@pathikrit
pathikrit / SudokuSolver.scala
Last active April 12, 2024 15:00
Sudoku Solver in Scala
val n = 9
val s = Math.sqrt(n).toInt
type Board = IndexedSeq[IndexedSeq[Int]]
def solve(board: Board, cell: Int = 0): Option[Board] = (cell%n, cell/n) match {
case (r, `n`) => Some(board)
case (r, c) if board(r)(c) > 0 => solve(board, cell + 1)
case (r, c) =>
def guess(x: Int) = solve(board.updated(r, board(r).updated(c, x)), cell + 1)
val used = board.indices.flatMap(i => Seq(board(r)(i), board(i)(c), board(s*(r/s) + i/s)(s*(c/s) + i%s)))
@maiatoday
maiatoday / getdb
Last active December 7, 2015 10:10
getdb - little shell script to pull the db for an app on an android emulator or device.
#!/bin/bash
display_usage() {
echo -e "\nUsage:\n`basename $0` -p [package] -n [dbname] \n"
}
# if less than two arguments supplied, display usage
if [ $# -le 2 ]
then
display_usage
@hrj
hrj / alias.md
Last active November 29, 2023 09:50
pwd alias hell

TL;DR

When you are in a shell in Linux, you may be led to believe you are in directory xyz but might actually be in directory pqr.


Demonstration

In a terminal,