Skip to content

Instantly share code, notes, and snippets.

@davidallsopp
davidallsopp / Progress bar
Created September 13, 2017 21:16
Progress bar
|*****-----|----------|
0% 50% 100%
@davidallsopp
davidallsopp / value.md
Last active March 10, 2018 20:34
Value - What, When, Where?
import Data.List
let numAndPlace xs ys = length $ (zip xs [0..]) `intersect` (zip ys [0..])
let numNotPlace xs ys = (length $ xs `intersect` ys) - (numAndPlace xs ys)
let test xs ys = (numAndPlace xs ys, numNotPlace xs ys)
let match xs = map (test xs) [[6,8,2],[6,1,4],[2,0,6],[7,3,8],[8,7,0]] == [(1,0),(0,1),(0,2),(0,0),(0,1)]
filter match [[x,y,z] | x<-[0..9], y<-[0..9], z<-[0..9]]
@davidallsopp
davidallsopp / MapDataFrame.scala
Created November 14, 2016 08:53
Mapping over a Spark DataFrame, via RDD, back to DataFrame so we can use the databricks API to write to an Avro file.
//import sqlContext.implicits._
import com.databricks.spark.avro._
import org.apache.spark.sql._
import org.apache.spark.sql.types._
//val inschema = StructType(List(StructField("name", StringType, true), StructField("age", IntegerType, true)))
val outschema = StructType(List(StructField("summary", StringType, true)))
val input = sc.parallelize(List(Row("fred", 34), Row("wilma", 33)))
@davidallsopp
davidallsopp / find_files.py
Created January 12, 2016 21:28
Recursively find files from a starting directory, as a 1-line dictionary comprehension (or you could use a generator comprehension). Can easily add a filter using an "if" clause in the comprehension
>>> import os
>>> from os.path import join
>>> [join(dr,f) for dr, dirs, files in os.walk('test') for f in files if f.endswith('html')]
['test/t1.html', 'test/t2.html', 'test/subtest/t3', 'test/subtest/subsubtest/t6.html']
@davidallsopp
davidallsopp / folds.hs
Last active October 30, 2015 11:56
Illustrating folds - from "Haskell Programming" (http://haskellbook.com/), who in turn borrowed the idea from from Cale Gibbard from the haskell Freenode IRC channel and on the Haskell.org wiki https://wiki.haskell.org/Fold#Examples
Prelude> let xs = map show [1..5]
Prelude> foldr (\x y -> concat ["(",x,"+",y,")"]) "0" xs
"(1+(2+(3+(4+(5+0)))))"
Prelude> let f = (\x y -> concat ["(",x,"+",y,")"])
Prelude> foldl f "0" (map show [1..5])
"(((((0+1)+2)+3)+4)+5)"
@davidallsopp
davidallsopp / colouredtabs.sh
Last active October 2, 2015 14:28
Colouring a text file to highlight spaces, tabs, etc. See also cat -A
#!/bin/bash
RED=`echo -e '\033[41m\033[37m'`
BLUE=`echo -e '\033[44m\033[37m'`
NORMAL=`echo -e '\033[0m'`
cat "$1" | sed -e s/\ /${BLUE}\ ${NORMAL}/g | sed -e s/$'\t'/${RED}\ \ \ \ ${NORMAL}/g
@davidallsopp
davidallsopp / tcpclient.py
Last active January 26, 2018 02:20
Example of reading lines from a socket using Tornado IOStream (via the TCPClient helper)
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.tcpclient import TCPClient
stream = None
def out(data):
print(data)
stream.read_until(b"\n", callback=out)
@davidallsopp
davidallsopp / TimerLoop.hs
Last active August 29, 2015 14:24
Game loop - looping with a fixed delay in Haskell until a condition is met
module TimerLoop where
import Control.Monad.Loops (iterateUntilM)
import Control.Concurrent (threadDelay)
-- Adapted from https://stackoverflow.com/questions/19285691/how-do-i-write-a-game-loop-in-haskell
--
-- NB must compile with -threaded option or the threadDelay has no apparent effect
-- even, then 9 and 10 are printed out simultaneously on my Windows 7 machine (GHCi 7.8.3)
@davidallsopp
davidallsopp / build_tree.py
Last active September 6, 2021 19:39
Build a tree from a list of pairs of (parent, child) identifiers.
from collections import defaultdict
ROOT = -1
def create_tree(pairs):
""" Given an iterable of (parent, child) identifiers, build a tree
where each node is a dict whose key is its identifier, and whose
value is a list of children (which may be empty for leaf nodes).
Multiple root nodes are supported; all roots are placed under a
synthetic ROOT node.