Skip to content

Instantly share code, notes, and snippets.

Vinai / example.clj
Last active Jul 22, 2019
Example of property based tests for a simple algorithm in Clojure and in PHP.
View example.clj
(ns example.core
(:require [clojure.test :refer :all]
[clojure.test.check :as tc]
[clojure.test.check.generators :as gen]
[ :as prop]))
;; -----------------------------------------------------------------------
;; Application code to test
(defn includes? [needle haystack]
Vinai /
Last active Apr 15, 2020
My beef with composer and Magento
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 / mage-file-cache-clean.php
Last active Jan 8, 2019
Quick to invoke version of bin/magento cache:clean
View mage-file-cache-clean.php
#!/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 / core.clj
Created Sep 22, 2017
Roman numerals kata in Clojure early while I was learning the language.
View core.clj
(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
(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 / core.clj
Created Sep 22, 2017
Print diamond kata in Clojure early while I was learning the language.
View core.clj
(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"
`(if (pos? ~value) ~value (- ~value)))
(defn size
Vinai / core.clj
Created Sep 22, 2017
String calculator kata in Clojure done early while learning the language.
View core.clj
(ns string-calculator.core
(:require [clojure.string :refer [join]]))
(defn numbers-from-string
(map #(Integer. %) (re-seq #"-?\d+" string)))
(defn add
(if (empty? string) 0
Vinai / core.clj
Created Sep 22, 2017
Prime factors kata early in me learning Clojure
View core.clj
(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)
(if (= 0 (mod n candidate))
(generate-recursive (/ n candidate) (conj primes candidate) candidate)
Vinai /
Last active Jan 13, 2020
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 /
Created Dec 13, 2016
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
echo -en "."