Skip to content

Instantly share code, notes, and snippets.

@jtpaasch
jtpaasch / walk_directories.c
Last active August 29, 2015 13:56
Walk a directory up or down, looking for a file or folder.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
/*
* A NON-CONSTANT VARIABLE
@jtpaasch
jtpaasch / alphanumeric_regex
Last active August 29, 2015 13:56
Regex: Alphanumeric, with at least one number and one character
// Match alphanumeric strings, with at least one number and one character.
// -----------------------------------------------------------------------
( // Find a group of:
[0-9]+[a-z] // - one or more digits, followed by a character.
| // - OR
[a-z]+[0-9] // - one or more characters, followed by a digit.
)
[a-z0-9]* // Then, 0 or more digits or characters.
// Match hexadecimal strings, with at least one number and one character.
@jtpaasch
jtpaasch / find_properties.js
Created February 25, 2014 22:39
Get a list of properties from a list of objects.
var find = function(list, property) {
return list.map(function(x) { return x[property]; }).sort();
};
@jtpaasch
jtpaasch / find_dupes.js
Created February 25, 2014 22:41
Find the list of items that are in both of two lists.
find_dupes = function (list1, list2) {
return list1.filter(function(x) { return list2.indexOf(x) > -1; });
};
@jtpaasch
jtpaasch / Generator.js
Last active August 29, 2015 14:00
A simple tool for yielding items incrementally from a list.
/**
* A tool for yielding items incrementally from a list.
* Usage:
* var gen = Generator([1, 2, 3], 0);
* gen.next(); // Yields 1
* gen.next(); // Yields 2
* gen.previous(); // Yields 1
*
* @author JT Paasch
* @license MIT <http://opensource.org/licenses/MIT>
@jtpaasch
jtpaasch / clean_branch.sh
Created May 30, 2014 15:25
Recreate a git branch clean
#!/bin/bash
# We want to clear out the "rc" branch. To do that, we need
# to be on a different branch. We'll get onto "master".
# Does master exist? The first command's status code
# will be "0" if the branch exists.
git show-ref --verify --quiet refs/heads/master
MASTER_EXISTS=$?
@jtpaasch
jtpaasch / network.http.1.hs
Last active August 29, 2015 14:02
Make some simple web requests with Network.HTTP
-- For working with HTTP.
import Network.HTTP
-- The `getRequest x` function takes a string (the URL you want to fetch)
-- and builds a GET `Request` object. Let's remap that to a function
-- called `build_get_request`:
build_get_request url = getRequest url
-- The `simpleHTTP` function takes a `Request` object and performs
-- the request. It returns a `Response` object, wrapped in the IO Monad.
@jtpaasch
jtpaasch / aeson.1.hs
Last active August 29, 2015 14:02
Encode and decode some simple JSON with the Aeson library.
{-# LANGUAGE OverloadedStrings #-}
-- We need all of these.
import Data.Aeson
import Control.Applicative
import Control.Monad
import qualified Data.ByteString.Lazy.Char8 as BSL
-- Create a haskell data type called `Person`.
-- Persons have a name and an age.
@jtpaasch
jtpaasch / is_integer.php
Last active August 29, 2015 14:04
A PHP function to check if a string is an integer
/**
* This function takes a string and tells you
* if the string represents an integer.
* All characters of the string must be digits,
* and there cannot be any leading zeros.
*
* @param String $string The string to check.
* @return Boolean
*/
function is_this_string_an_integer($string) {
@jtpaasch
jtpaasch / argparse.rb
Created August 12, 2015 11:26
A simple Ruby argument parsing loop.
#!/usr/bin/env ruby
if ARGV.empty?
puts "No arguments provided."
end
until ARGV.empty? do
case ARGV[0]
when '-q'
ARGV.shift