Skip to content

Instantly share code, notes, and snippets.

View miner's full-sized avatar

Steve Miner miner

View GitHub Profile
;; I posted an earlier version on my blog:
;; http://fnclojure.wordpress.com/2012/08/06/roman-numerals-in-clojure/
;; This version is faster and more readable.
(defn range-down
"Returns a seq of integers from HIGH (exclusive) down to LOW (inclusive).
LOW defaults to 0. STEP is a positve decrement, defaults to 1. Like
`(reverse (range low high step))' but a bit faster."
([high] (range (dec high) -1 -1))
@miner
miner / ycombinator.clj
Last active January 4, 2021 09:38 — forked from z5h/ycombinator.clj
;;; 01/14/14 16:18 by miner -- Steve Miner revised this code to be more idiomatic Clojure.
;;;
; Short sidebar: Clojure has a special form for the efficient compilation of tail recursion.
; Something like this would work as a factorial function:
(defn fact2 [n]
(loop [n n acc 1]
(if (zero? n) acc (recur (dec n) (* n acc)))))
; We're not going to discuss `recur` any further as we're imagining a language that doesn't
@miner
miner / infix.clj
Last active June 26, 2016 13:27
An infix data-reader
(ns miner.infix)
;; Inspired by the excellent book "The Joy of Clojure".
;; http://fogus.me/fun/unfix/infix-src.html
;;
;; Converted to be used as a data-reader by Steve Miner. The main change is to preserve
;; the prefix forms rather than calculating the result. Also, the reader gets symbols, not
;; the actual functions.
(def && #(and % %2))
;; inspired by https://twitter.com/puredanger/status/241282082268143619
;; reduce-kv is faster than destructuring with maps
;; use (get m k) instead of (k m) to be safe with non-keyword keys
(defn sub=
"Like = for most things, but maps compare recursively by only the keys in a, so it returns true
if a is a 'submap' of b."
[a b]
(if (and (map? a) (map? b))
@miner
miner / roman.clj
Created August 6, 2012 14:16
Roman Numerals
(ns miner.roman
(:require [clojure.test :refer :all]))
;; inspired by
;; http://www.jayway.com/2012/08/04/a-decimal-to-roman-numeral-converter-in-just-a-few-lines/
(def roman-map {1000 "M" 900 "CM" 500 "D" 400 "CD"
100 "C" 90 "XC" 50 "L" 40 "XL"
10 "X" 9 "IX" 5 "V" 4 "IV" 1 "I"})
@miner
miner / gist:3240618
Created August 2, 2012 21:03 — forked from swannodette/gist:3217582
sudoku_compact.clj
(ns sudoku
(:refer-clojure :exclude [==])
(:use clojure.core.logic))
(defn get-square [rows x y]
(for [x (range x (+ x 3))
y (range y (+ y 3))]
(get-in rows [x y])))
(defn init [vars hints]
@miner
miner / fourbit.clj
Created June 18, 2012 16:31
Miner's four-bit adder for Rosetta Code
;; http://rosettacode.org/wiki/Four_bit_adder
(ns miner.fourbit)
;; a bit is represented as a boolean (true/false)
;; a word is a big-endian vector of bits [true false true true] = 11
;; maybe little endian is more convenient???
(defn bvec