Skip to content

Instantly share code, notes, and snippets.

@djcsdy
djcsdy / Map.java
Created February 10, 2011 18:15
Example of functional-style ‘map’ in Java. Isn’t it revolting? :)
package net.noiseinstitute.lambda;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Map {
public static <T,U> List<U> map (Collection<T> collection, MapCallback<T,U> callback) {
List<U> list = new ArrayList<U>();
@djcsdy
djcsdy / make-thumbnail.as
Created February 12, 2011 17:38
Example of creating a bimap thumbnail of a sprite/movieclip in Flash/AS3
var matrix:Matrix = new Matrix();
matrix.scale(0.2);
var bitmapData:BitmapData = new BitmapData(width, height);
bitmapData.draw(movieClip, matrix);
@djcsdy
djcsdy / elastic-collision.as
Created March 7, 2011 01:17
Rough implementation of fully-elastic collision. Only modifies the position and velocity of sprite 1; the assumption is that this code will get run again with the sprites swapped round. The stuff in actionQueue.enqueue should get run after all collisions
var sprite2ToSprite1:FlxPoint = VectorMath.subtract(sprite1.centre, sprite2.centre);
var sprite2ToSprite1UnitVector:FlxPoint = VectorMath.normalize(sprite2ToSprite1);
var sprite2SpeedTowardsSprite1:Number = VectorMath.dotProduct(sprite2.velocity, sprite2ToSprite1UnitVector);
var sprite1ToSprite2UnitVector:FlxPoint = VectorMath.negate(sprite2ToSprite1UnitVector);
var sprite1SpeedTowardsSprite2:Number = VectorMath.dotProduct(sprite1.velocity, sprite1ToSprite2UnitVector);
var sumOfThoseSpeeds:Number = sprite1SpeedTowardsSprite2 + sprite2SpeedTowardsSprite1;
var sprite1VelocityChange:FlxPoint =
VectorMath.multiply(sprite2ToSprite1UnitVector, sumOfThoseSpeeds/2);
@djcsdy
djcsdy / svn-clean.ps1
Created March 17, 2011 16:03
Powershell script to delete files from an SVN working copy that are neither version-controlled nor ignored. Roughly equivalent to git clean -f. Requires the command-line svn to be located in your %PATH%.
svn status |
Select-String '^\?' |
ForEach-Object {
[Regex]::Match($_.Line, '^[^\s]*\s+(.*)$').Groups[1].Value
} |
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
@djcsdy
djcsdy / escape-selector.js
Created March 24, 2011 16:58
Escapes a string for use within a CSS selector as an identifier
function escapeSelector(selector) {
return "".replace.call(selector,
/(^[^_a-zA-Z\u00a0-\uffff]|[^-_a-zA-Z0-9\u00a0-\uffff])/g,
"\\$1");
}
// Use this to escape CSS identifiers that may contain reserved characters.
// For example, in JQuery, to select the element whose identifier is named by the variable 'id':
$('#' + escapeSelector(id));
@djcsdy
djcsdy / svn-clean-all.ps1
Created April 20, 2011 15:27
Powershell script to delete all unversioned files from an SVN working copy, including ignored files. Roughly equivalent to git clean -f -x. Requires the command-line svn to be located in your %PATH%.
svn status --no-ignore |
Select-String '^[?I]' |
ForEach-Object {
[Regex]::Match($_.Line, '^[^\s]*\s+(.*)$').Groups[1].Value
} |
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
@djcsdy
djcsdy / BrokenUriBuilder.cs
Created September 29, 2011 10:38
Microsoft are imbeciles
var uriBuilder = new UriBuilder();
uriBuilder.Query = "foo=bar";
Console.WriteLine(uriBuilder.Query); // prints "?foo=bar"
uriBuilder.Query = uriBuilder.Query;
Console.WriteLine(uriBuilder.Query); // prints "??foo=bar"
// Someone at Microsoft needs a swift kick in the balls.
@djcsdy
djcsdy / gist:1896016
Created February 24, 2012 00:15
Object closure allocation in AS3
class Foo {
function hatstand():void {}
function lampshade():void {
Tablecloth.onRemove(hatstand); // <- this allocates
}
}
class Foo {
var f:Function;
function Foo() {
@djcsdy
djcsdy / LICENSE.txt
Created June 5, 2012 15:04 — forked from p01/LICENSE.txt
Music SoftSynth
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Mathieu 'p01' Henri http://www.p01.org/releases/
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@djcsdy
djcsdy / debug-swf.sh
Created June 12, 2012 23:08
Launches a SWF in an fdb session so you can see traces and uncaught exceptions
#!/bin/sh
# This turned out to be easier than expected.
SWF="$1"
fdb "$SWF"