Skip to content

Instantly share code, notes, and snippets.

@Vinai
Vinai / example.clj
Last active July 22, 2019 13:48
Example of property based tests for a simple algorithm in Clojure and in PHP.
(ns example.core
(:require [clojure.test :refer :all]
[clojure.test.check :as tc]
[clojure.test.check.generators :as gen]
[clojure.test.check.properties :as prop]))
;; -----------------------------------------------------------------------
;; Application code to test
(defn includes? [needle haystack]
@Vinai
Vinai / magento-composer-issues.md
Last active April 15, 2020 03:09
My beef with composer and Magento
#!/bin/bash
current=$(php --version | head -1 | cut -f2 -d' ')
current=$(echo "${current%.*}")
php_head=$(brew info php | head -1 | cut -f3 -d' ')
from=$([ "${current}" = "${php_head%.*}" ] && echo "php" || echo "php@${current}")
to=$([ "${1}" = "${php_head%.*}" ] && echo "php" || echo "php@${1}")
echo "Switching from $from to $to"
@Vinai
Vinai / mage-file-cache-clean.php
Last active January 8, 2019 09:25
Quick to invoke version of bin/magento cache:clean
#!/usr/bin/env php
<?php declare(strict_types=1);
use function array_reduce as reduce;
use function array_slice as slice;
use function array_filter as filter;
use function array_map as map;
$basedir = reduce(['.', '..', '../..', '../../..'], function ($acc, $basedir) {
return file_exists($basedir . '/vendor/autoload.php') ? $basedir : $acc;
@Vinai
Vinai / core.clj
Created September 22, 2017 09:03
Roman numerals kata in Clojure early while I was learning the language.
(ns roman-numerals.core)
(def roman-numerals-map (array-map "M" 1000, "CM" 900, "D" 500, "CD" 400, "C" 100, "XC" 90,
"L" 50, "XL" 40, "X" 10, "IX" 9, "V" 5, "IV" 4, "I" 1))
(defn arabic->roman
[n]
(if (= 0 n) ""
(let [[r v] (some (fn [[r v]] (if (<= v n) [r v])) roman-numerals-map)]
(apply str (concat (repeat (int (/ n v)) r) [(arabic->roman (rem n v))])))))
@Vinai
Vinai / core.clj
Created September 22, 2017 09:02
Print diamond kata in Clojure early while I was learning the language.
(ns print-diamond.core)
(defmacro abs
"Return the absolute value of the specified number.
This is a macro only for practice.
A regular function (max value (- value)) would work just as well"
[value]
`(if (pos? ~value) ~value (- ~value)))
(defn size
@Vinai
Vinai / core.clj
Created September 22, 2017 09:00
String calculator kata in Clojure done early while learning the language.
(ns string-calculator.core
(:require [clojure.string :refer [join]]))
(defn numbers-from-string
[string]
(map #(Integer. %) (re-seq #"-?\d+" string)))
(defn add
[string]
(if (empty? string) 0
@Vinai
Vinai / core.clj
Created September 22, 2017 08:58
Prime factors kata early in me learning Clojure
(ns prime-factors.core)
(defn generate-recursive
"Prime factor kata that does not use tail call optimization"
([n] (generate-recursive n [] 2))
([n primes candidate]
(if (= 1 n)
primes
(if (= 0 (mod n candidate))
(generate-recursive (/ n candidate) (conj primes candidate) candidate)
@Vinai
Vinai / 2016-48.md
Last active January 13, 2020 09:25
List of past code katas. The file names are the [year]-[week-of-year].md. I'll try to update the list each week.

The first kata is the classic BowlingGame Kata from Uncle Bob.

Write a class named Game that has two methods

  • roll(pins : int) is called each time the player rolls a ball. The argument is the number of pins knocked down.
  • score() : int is called only at the very end of the game. It returns the total score for that game.

Here is the original PowerPoint from Uncle Bob with the instructions including the solution steps. The PPT file also includes the rules for the scoring of a bowling game.

@Vinai
Vinai / prime-factors.sh
Created December 13, 2016 18:57
Language or testing frameworks or the lack thereof are no reason not to write tests. All that is needed is a function that calls the function to be checked.
#!/usr/bin/env bash
function assert_array_same {
if [ "${expected[*]}" != "${actual[*]}" ]; then
echo -e "\nFailed $1\nExpected: ${expected[*]}\nActual: ${actual[*]}" && exit 1
else
echo -en "."
fi
}