Skip to content

Instantly share code, notes, and snippets.

Avatar

Christopher Davis chrisguitarguy

View GitHub Profile
View solution.php
<?php
interface MoneyFormatter
{
public function format($value, $decimals=0, $decimalPoint='.', $thousandsSep=',');
}
class FormatterExtension extends \Twig_Extension
{
private $formatter;
@chrisguitarguy
chrisguitarguy / list.php
Last active Aug 29, 2015
PHP list weirdness
View list.php
<?php
$one = array('two', 'one');
list($two, $one) = $one;
var_dump($one, $two);
/* Output
string(3) "one"
string(1) "o"
*/
/*
View codejam.py
#! /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
View aliens.py
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
"""
https://code.google.com/codejam/contest/90101/dashboard#s=p0
"""
import re
import sys
View aws-csv-streamwrapper.php
<?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 Jun 21, 2014
Read from an input stream until a CRLF.
View readline.clj
(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 Jun 28, 2014
An example of a "default singleton"
View Foo.php
<?php
class Foo
{
private $name;
private static $defaultInstance = null;
/**
* Public constructor: create as many instance as you want
*/
@chrisguitarguy
chrisguitarguy / example.py
Created Jul 18, 2014
Dev server example for a Flask + Client-Side App
View example.py
#!/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>
View default_file.py
#! /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 Aug 26, 2014
Function currying in JavaScript. Kind of.
View curry.js
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;