Skip to content

Instantly share code, notes, and snippets.

@TJC
TJC / gist:1946649
Created March 1, 2012 02:02
melb scala ranges challenge
def ranges(ints: Seq[Int]): Seq[(Int, Int)] = {
ints.sorted.foldLeft( Seq[(Int,Int)]() )(
(acc: Seq[(Int,Int)], n: Int) => acc match {
// case Nil => acc :+ (n,n)
case x :: (a,z) if (z == n) => acc
case x :: (a,z) if (z+1 == n) => x :+ (a, n)
case _ => acc :+ (n,n)
}
)
}
@TJC
TJC / gist:1946959
Created March 1, 2012 03:13
Another attempt at Scala ranges challenge
// This method works, but I'm trying to shrink it in the form further down..
def ranges(ints: Seq[Int]): Seq[(Int, Int)] = {
ints.sorted.foldLeft( Seq[(Int,Int)]() )(
(acc: Seq[(Int,Int)], n: Int) => acc match {
case acc if (acc.nonEmpty) => acc.last match {
case (a,z) if (z == n) => acc
case (a,z) if (z+1 == n) => acc.init :+ (a, n)
case _ => acc :+ (n,n)
}
@TJC
TJC / weighted_random.scala
Created March 14, 2012 02:53
Solution to scala-melb challenge, Mar 14 2012
// This is a fairly ugly solution; in a hurry and didn't spend much time on it.
import scala.util.Random
case class WeightedItem[T](item: T, weight: Double)
val items = Seq(WeightedItem("Red", 1d/6), WeightedItem("Blue", 2d/6),
WeightedItem("Green", 3d/6) )
def weightedSelection[T](
@TJC
TJC / explain.txt
Created April 25, 2012 06:18
Query plan that chews up gigabytes during EXPLAIN
This query plan results from a query that basically looks like
UPDATE line SET status = 'X'
WHERE line IN (
SELECT id FROM a_view_based_on_views
WHERE file_id = 1
)
AND file_id = 1;
@TJC
TJC / arm-none-eabi-gcc
Created May 23, 2013 08:23
Perl script to monkey-patch cortex-m4 math libraries into path for teensyduino (teensy 3.0)
#!/usr/bin/perl
use strict;
use warnings;
my @args = @ARGV;
my $real = $0 . ".real";
if (grep { /cortex.m/ } @args) {
my @new_args = grep { ! /^\-lm$/ } @args;
push @new_args, "-L/home/tobyc/git/arduino/lib", "-larm_cortexM4l_math", "-lm";
@TJC
TJC / M4Demo.ino
Last active February 15, 2021 23:33
Teensy 3.0 (teensyduino) example program using Cortex-M4 DSP FFT function.
#define ARM_MATH_CM4
#include "arm_math.h"
#define TEST_LENGTH_SAMPLES 1024
#include "arm_fft_bin_data.h"
// static float32_t testOutput[TEST_LENGTH_SAMPLES/2];
// NOTE: q15t is int16_t in arm_math.h
@TJC
TJC / kml_dedupe.scala
Created February 6, 2014 04:47
Dedupe placemarks in KML file
// Run with "scala convert.scala"
import scala.xml.XML
import scala.xml.Node
// Given a node, return a trimmed string containing its coordinates
def toCoords(node: Node): String = node match {
case node if ! (node \ "Point").isEmpty => (node \ "Point" \ "coordinates").text.trim
case node if ! (node \ "LineString").isEmpty => (node \ "LineString" \ "coordinates").text.trim
case node if ! (node \ "Polygon").isEmpty => (node \ "Polygon" \ "outerBoundaryIs" \ "LinearRing" \ "coordinates").text.trim
case _ => println(f"node unknown coords: %s", node \ "name"); "unknown"
@TJC
TJC / bfr_chevron.go
Last active August 29, 2015 14:03
Quick knock-up of scrolling chevron effect
package main
import "fmt"
import "time"
import "github.com/mgutz/ansi"
const ledsAcross = 12 // rows of display
const ledsDown = 16 // columns of display (was 8)
const ledTotal = 12*16
type Colour struct {
@TJC
TJC / keybase.md
Created August 4, 2014 01:16
keybase proof

Keybase proof

I hereby claim:

  • I am tjc on github.
  • I am tjc (https://keybase.io/tjc) on keybase.
  • I have a public key whose fingerprint is 928F E178 7459 A9A5 8D6E 853F 1C1E 2A07 021B 8378

To claim this, I am signing this object:

@TJC
TJC / bulk_convert_flac_to_mp3.pl
Created August 25, 2014 02:02
Bulk conversion script from FLAC albums to MP3 with ID3 tags added
#!/usr/bin/env perl
use 5.12.0;
use warnings;
use IPC::Run qw(run);
use autodie;
=head1 NAME
convert_flac_to_mp3.pl