View isqrt.go
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
// odds computes the square root by adding odd numbers together until n is | |
// reached. | |
// Cost: 4 commands per loop; O(n) loops |
View reactHandlerUtils.js
/* Returns a function `(value, merge) => {...}` that calls | |
`component.setState(...)` to update the component's state with the new value | |
for `key`. | |
A simple example: | |
const func = updateKey(component, "counter"); | |
func(23); | |
which is equivalent to: |
View range_sensor.py
#!/usr/bin/python | |
# Measure distance to water level in sump pit using | |
# HC-SR04 ultrasonic ranging module | |
# Sensor info: | |
# 2cm - 4m. range with ~3mm. accuracy, 15 degree measuring angle | |
# 5VDC, 15mA working current | |
# Suggestion in spec to connect ground before 5VDC line when hot plugging | |
# Measure surface area no less than 0.5 m^2 (really?) |
View variableHash.js
var crypto = require("crypto"); | |
/* variableHash - Generate a variable-length hash of `data`. | |
Similar to the answer here: http://crypto.stackexchange.com/a/3559/4829 | |
If you want a b-bit hash of the message m, then use the first b bits of AES-CTR(SHA256(m)). | |
Rather than using the suggested algorithm in the stackexchange answer above, I developed | |
my own. | |
I decided to derive AES256 initialization vector and key from the output of SHA256(data). |
View changeTypeAttr.js
/* x is the <input/> element | |
type is the type you want to change it to. | |
jQuery is required and assumed to be the "$" variable */ | |
function changeType(x, type) { | |
if(x.prop('type') == type) | |
return x; //That was easy. | |
try { | |
return x.prop('type', type); //Stupid IE security will not allow this | |
} catch(e) { | |
//Try re-creating the element (yep... this sucks) |
View exportDatabaseToCSV.php
<?php | |
/* exportDatabaseToCSV | |
Author: Blake C. Miner | |
$db - must be a PDO connection | |
$database - string, the name of the schema to be backed up | |
$filename - string, the name of the *.tar file to be generated | |
*/ | |
function exportDatabaseToCSV($db, $database, $filename) { | |
//Delete files | |
if(file_exists($filename) ) |
View ebs-backup.php
#! /usr/bin/env php | |
<?php | |
//Don't forget to setup credentials for the AWS API. | |
require_once('/usr/lib/aws-php-sdk-1.3.1/sdk.class.php'); | |
$ec2 = new AmazonEC2(); | |
$volumes = array( | |
'vol-xxxxxxxx' => '-- Your snapshot description here --', | |
'vol-xxxxxxxx' => '-- Your snapshot description here --', | |
'vol-xxxxxxxx' => '-- Your snapshot description here --' |
View express_sessions_socket.io.js
var express = require('express'); | |
var RedisStore = require('connect-redis')(express); | |
express.socketio = require('socket.io'); | |
var app = express.createServer(); | |
app.configure(function() { | |
app.use(express.cookieParser() ); | |
app.sessionStore = new express.session.MemoryStore(); //Not recommended for production use | |
app.use(express.session({ |