Skip to content

Instantly share code, notes, and snippets.

View justinhj's full-sized avatar

Justin Heyes-Jones justinhj

View GitHub Profile
@justinhj
justinhj / git-add-dired.el
Created July 7, 2013 21:28
Allows you to use dired to add multiple files to a git repository. This is useful since staging files in magit is sluggish and takes a long time when you have a lot of files.
(defun git-add-files(files)
"Run git add with the input file"
(interactive)
(shell-command (format "git add %s" files)))
(defun dired-git-add-marked-files()
"For each marked file in a dired buffer add it to the index"
(interactive)
(if (eq major-mode 'dired-mode)
(let ((filenames (dired-get-marked-files))
@justinhj
justinhj / clojure-heap.clj
Last active September 30, 2019 03:02
A binary queue and heap sort in Clojure
;;; An implementation of a binary heap in Clojure
;;; This is an algorithm used for efficiently taking the
;;; largest value from a collection. Commonly used to implement
;;; a priority queue
;;; The heap itself is stored as a vector. For convenience with
;;; array indexing the first element is set to nil and not used
;;; I wrote this as a learning experience not to be used to land
;;; jet aircraft or manage a nuclear reactor.
;;; Should be reasonably efficient though.
@justinhj
justinhj / gist:eb2d354d06631076566f
Created May 12, 2014 02:01
Sending a notification from emacs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Terminal notifier
;; requires 'sudo gem install terminal-notifier'
;; stolen from erc-notifier
(defvar terminal-notifier-command (executable-find "terminal-notifier") "The path to terminal-notifier.")
; (terminal-notifier-notify "Emacs notification" "Something amusing happened")
(defun terminal-notifier-notify (title message)
@justinhj
justinhj / efind
Created June 29, 2014 16:42
A bash script to easily search files for a regular expression
#!/bin/bash
# A simple script to recursively search files
if [ "$#" -ne 3 ]; then
echo "efind (easy find) usage: [path] [file pattern] [regex]"
echo 'efind ../c "*.c" "string"'
exit 1
fi
find "$1" -name "$2" -type f -print0 | xargs -0 egrep -iHn "$3"
// Determine if a value is the product of any two numbers in a vector
// Converts the array to a stream so it can be consumed lazily
// the helper function is recursive but subject to tail call optimization
def prod(in: Array[Int], v: Int): Boolean = {
def helper(in: Stream[Int], v: Int, found: Map[Int, Int]): Boolean = in match {
case x #:: xs => {
val r = v / x
if(r * x != v) helper(xs, v, found)
else if(found.contains(x)) true
@justinhj
justinhj / FileExt.scala
Last active August 29, 2015 14:20
Using unapply to match strings that are valid filenames with an extension
object FileExt {
def unapply(s: String): Option[String] = {
val parts: Array[String] = s.split('.')
if(parts.length < 2) None
else Some(parts.last)
}
@justinhj
justinhj / backupchangeslist.py
Last active August 29, 2015 14:22
backup your p$ change list files to a dated folder
# Copies a changelist (any files open for edit or add) to a directory you specify
# (C)2011 Justin Heyes-Jones
# TODO
# Allow user to pass in PORT setting
# Or at least check that it is set before doing perforce interactions
# P4 set P4PORT=servername:port
import subprocess, sys, marshal, string, os, time, datetime
@justinhj
justinhj / help.md
Created September 22, 2015 18:03
Water jug help for A*

State representation

In the 8Puzzle the state of each step in the solution is an array of tiles. In the water jug it can be an array of jug levels [0,0]

You can replace tiles with jugs TILE tiles[ BOARD_WIDTH*BOARD_HEIGHT ]; int jug_levels[ 2 ];

State transitions

@justinhj
justinhj / futureDemo.scala
Last active October 12, 2016 21:25
Demo of concurrency handling in for comprehension using Futures
// Demonstrating futures running concurrently and in sequence
import scala.concurrent.{ExecutionContext, Future}
import scala.language.postfixOps
// A simple execution context for us to use that will not shut down the JVM until the threads we start have all finished
// It does that by making the threads non-Daemon
// http://stackoverflow.com/questions/25236143/why-future-example-do-not-work
// http://stackoverflow.com/questions/2213340/what-is-daemon-thread-in-java
@justinhj
justinhj / translate.org
Created November 1, 2016 17:18
Translate tables of English words to Mandarin using python translate and org babel

-*- mode: org; org-confirm-babel-evaluate: nil; -*-

from translate import Translator
translator=Translator(to_lang="zh")
translated=translator.translate(translatetext)