Skip to content

Instantly share code, notes, and snippets.

View chrisguitarguy's full-sized avatar

Christopher Davis chrisguitarguy

View GitHub Profile
<?php
interface MoneyFormatter
{
public function format($value, $decimals=0, $decimalPoint='.', $thousandsSep=',');
}
class FormatterExtension extends \Twig_Extension
{
private $formatter;
@chrisguitarguy
chrisguitarguy / list.php
Last active August 29, 2015 13:56
PHP list weirdness
<?php
$one = array('two', 'one');
list($two, $one) = $one;
var_dump($one, $two);
/* Output
string(3) "one"
string(1) "o"
*/
/*
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
"""
https://code.google.com/codejam/contest/32016/dashboard#s=p0
The fuck is a "scalar product"?
http://en.wikipedia.org/wiki/Dot_product#Algebraic_definition
"""
import sys
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
"""
https://code.google.com/codejam/contest/90101/dashboard#s=p0
"""
import re
import sys
<?php
require __DIR__ . '/vendor/autoload.php';
$s3 = Aws\S3\S3Client::factory($config);
$s3->registerStreamWrapper();
$url = 's3://{$bucket}/{$key}';
// Read CSV with fopen
@chrisguitarguy
chrisguitarguy / readline.clj
Created June 21, 2014 20:54
Read from an input stream until a CRLF.
(defn read-line-crlf [instream]
(loop [sb (StringBuilder.) has-cr false]
(let [byt (.read instream)]
(when (= -1 byt)
(throw (java.io.IOException. "Unexpected end of input")))
(let [chr (char byt)]
(if (and has-cr (= \newline chr))
(.substring sb 0 (- (.length sb) 1))
(recur
(.append sb chr)
@chrisguitarguy
chrisguitarguy / Foo.php
Created June 28, 2014 20:50
An example of a "default singleton"
<?php
class Foo
{
private $name;
private static $defaultInstance = null;
/**
* Public constructor: create as many instance as you want
*/
@chrisguitarguy
chrisguitarguy / example.py
Created July 18, 2014 14:42
Dev server example for a Flask + Client-Side App
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
We have a little all-api python application that will have a client-side front
end and needed a script to run a dev server. This is it.
The app itself is a flask app, and will be mounted at `/api` in production. This
is an example of how to reproduce a setup like that with Werkzeug and Static.
:copyright: (c) 2014 PMG <http://pmg.co>
@chrisguitarguy
chrisguitarguy / default_file.py
Created July 21, 2014 00:53
How to use a "default document" with static https://github.com/lukearno/static
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Sometimes you want all not found requests to go to a default file. A single page
JS app, for example, might use a client side router to show the appropriate view
from a single ``index.html``.
This is a simple example of accomplishing something like that with static:
https://github.com/lukearno/static
@chrisguitarguy
chrisguitarguy / curry.js
Created August 26, 2014 19:07
Function currying in JavaScript. Kind of.
function makeCurry(callable) {
return function () {
var args = [];
function inner() {
args = args.concat(Array.prototype.slice.call(arguments));
if (args.length >= callable.length) {
return callable.apply(callable, args);
}
return inner;