Skip to content

Instantly share code, notes, and snippets.

View dtao's full-sized avatar

Dan Tao dtao

View GitHub Profile
@dtao
dtao / frp.example.js
Created May 5, 2014 15:58
Lazy.js jQuery Event Stream
window.addEventListener('load', function() {
// This 'createWrapper' method is super weird and awkward and I'm not proud of
// it at all. But right now it's the only option for defining custom sequence
// types that wrap arbitrary sources. In a future version I will come up with
// something much better, swear to God.
var jqueryWrapper = Lazy.createWrapper(function(selector, eventName) {
var source = this;
$(selector).on(eventName, function(e) {
source.emit(e);
});
@dtao
dtao / createGeneratorIdea.js
Created January 11, 2014 21:24
An idea for how generators could be implemented pre-ES6
/**
* Using yield to create a generator -- only possible in ES6/Harmony.
*/
function fibonacci(limit) {
var fn1 = 1;
var fn2 = 1;
while (1) {
var current = fn2;
fn2 = fn1;
fn1 = fn1 + current;
@dtao
dtao / convert_to_markdown.rb
Last active December 20, 2015 06:59
A very basic script to translate a simple HTML file (consisting only of paragraphs with bold & italic text) to Markdown.
require 'nokogiri'
file = ARGV[0]
if !File.exist?(file)
puts "File #{file} does not exist."
exit
end
html = File.read(file)
@dtao
dtao / agent.js
Created May 21, 2013 16:55
How 'this' in JavaScript is like 'instance_eval' in Ruby
function Agent(name) {
this.name = name;
this.command = function(subordinate, description, instructions) {
console.log(this.name + " tells " + subordinate.name + ": '" + description + "'");
instructions.apply(subordinate);
};
this.getCoffee = function() {
var recipients = Array.prototype.slice.call(arguments);
@dtao
dtao / disable_xml_parameter_parsing.rb
Last active December 11, 2015 00:09
Rails initializer to disable XML parameter parsing to protect against newly discovered exploit
# Disable XML parameter parsing completely as a stopgap measure until upgrading to Rails 2.3.16
# to defend against arbitrary code execution vulnerability; see:
# https://groups.google.com/forum/#!topic/rubyonrails-security/61bkgvnSGTQ/discussion
ActionController::Base.param_parsers.delete(Mime::XML)
@dtao
dtao / FastString.cs
Created August 25, 2012 22:35
Proof of concept for an immutable string class optimized for concatenation with the + operator
using System;
using System.Text;
namespace FastStringConcatenation
{
public class FastString
{
const int DefaultCapacity = 32;
readonly StringBuilder buffer;
@dtao
dtao / EvilStringHelper.cs
Created August 16, 2012 17:01
Devious C# class that makes .NET strings mutable without requiring the /unsafe compiler switch
public static class EvilStringHelper
{
private static readonly Action<string, int, char> _setChar;
private static readonly Action<string, int> _setLength;
static EvilStringHelper()
{
if (Environment.Version.Major < 4)
{
MethodInfo setCharMethod = typeof(string).GetMethod(
@dtao
dtao / expand_window.js
Created August 1, 2012 22:49
Function to automatically zoom in any website to fill available screen width
(function() {
function expand($) {
function text($element) {
return $.trim($element.clone().children().remove().end().text());
}
function isFixedOrFloating($element) {
try {
if ($element.css("position") === "fixed") {
return true;
@dtao
dtao / increment_decrement.py
Created May 25, 2012 16:10
Sublime Text plugin to increment/decrement all selected numbers
import sublime
import sublime_plugin
class NumberCommand(sublime_plugin.TextCommand):
def run(self, edit):
selection = self.view.sel()
for region in selection:
try:
value = int(self.view.substr(region))
self.view.replace(edit, region, str(self.op(value)))
@dtao
dtao / example.sublime-keymap
Last active December 19, 2017 22:58
Sublime Text plugin to replace selected text with random letters/numbers
[
{ "keys": ["ctrl+alt+r"], "command": "randomize" }
]