Skip to content

Instantly share code, notes, and snippets.

View chrisguitarguy's full-sized avatar

Christopher Davis chrisguitarguy

View GitHub Profile
(require '[clojure.string :as string])
(defn- make-rows [height]
(loop [out [] h height]
(if (< h 1)
(map #(apply str %) out)
(recur
(conj out (concat (repeat (- height h) \space) (repeat h \#)))
(dec h)))))
(defn perceptron [inputs weights bias]
{:pre [(= (count inputs) (count weights))]}
(let [combined (map * inputs weights)]
(if (< (+ (apply + combined) bias) 1) 0 1)))
(defn nand [one two]
(perceptron [one two] [-2 -2] 3))
(defn adder [x y]
"Returns a vector of [carry-bit sum-bit]"
<?php
function foo(bool $hello=true)
{
// ...
}
@chrisguitarguy
chrisguitarguy / example.php
Created March 7, 2016 14:29
TIL you cannot give PHP's scalar typehints any default value other than `null`. Completely with a misleading error message.
<?php
function foo(boolean $hello=true)
{
// ...
}
/*
local> php -l example.php
PHP Fatal error: Default value for parameters with a class type hint can only be NULL in t.php on line 3
@chrisguitarguy
chrisguitarguy / KeepValueListener.php
Created February 26, 2016 14:29
A way to keep original values around when a Symfony form is submitted with an empty value. Useful for password fields especially
<?php
/**
* Copyright (c) 2016 PMG <https://www.pmg.com>
*
* License: MIT
*/
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Form\FormEvents;
<?php
$query = new \WP_Query(array(
// ...
));
foreach ($query->chunk(4) as $chunk): ?>
<div class="row">
<?php while ($chunk->have_posts()): $chunk->the_post(); ?>
@chrisguitarguy
chrisguitarguy / KeepPasswordListener.php
Created February 22, 2016 21:30
Keep your Symfony form's password value around if an empty value is submitted. Optionally supply a "Clear Password" field.
<?php
/**
* Copyright (c) 2016 PMG <https://www.pmg.com>
*
* License: MIT
*/
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Form\FormEvents;
@chrisguitarguy
chrisguitarguy / example.sh
Created February 14, 2016 17:06
Update calls to `createForm` in Symfony controllers to use class names instead of instances
find path/to/Bundle/Controller -type f -name '*.php' -exec sed -i '' 's/createForm(new \(.*\)()/createForm(\1::class/' {} \;
@chrisguitarguy
chrisguitarguy / ajax.js
Last active November 27, 2015 02:37
Turns out it's pretty simple to write a little promised-based ajax wrapper
var assign = require('object-assign');
var Promise = require('es6-promise').Promise;
var EventEmitter = require('eventemitter2').EventEmitter2;
function buildQueryString(object, prefix) {
var out = [];
for (var prop in object) {
if (!object.hasOwnProperty(prop)) {
continue;
}
#!/usr/bin/env boot
(require '[clojure.string :as string])
(defn- str->int [s]
(Integer/parseInt s))
(defn- positive? [n]
(> n 0))