Skip to content

Instantly share code, notes, and snippets.

Steve Miner miner

Block or report user

Report or block miner

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@miner
miner / pal.clj
Created Oct 2, 2018
find longest palindromic substring using Clojure
View pal.clj
(ns miner.pal
(:require [criterium.core :as crit]
[clojure.string :as str]))
;; An exercise from Apropos Clojure #18 video cast:
;; https://www.youtube.com/watch?v=elF9BPa0Np4
;;
;; Their solution is something like this...
@miner
miner / apropos3.clj
Created Mar 19, 2018
a stateful transducer inspired by @apropos_cast episode #3
View apropos3.clj
(defn digits [n]
{:pre [(int? n) (>= n 0)]}
(loop [digs () remainder n]
(if (< remainder 10)
(conj digs remainder)
(recur (conj digs (rem remainder 10)) (quot remainder 10)))))
(defn digits+rev [n]
(let [ds (digits n)]
(concat ds (reverse ds))))
@miner
miner / map-alt.clj
Created Mar 14, 2018
map-alt transducer
View map-alt.clj
;; map-alt is like the map xform but calls the funtions in an alternating order
;; (f i0) (g i1) (h i2) (f i3) ...
;;
;; In other words, map-alt spreads fn calls across elements, whereas (mapcat (juxt ...)) calls all
;; fns on each element.
(defn map-alt
([] (map identity))
([f] (map f))
@miner
miner / gdiff
Last active Nov 30, 2017
using FileMerge as git difftool on Mac OS X
View gdiff
#!/bin/bash
# my git difftool, calls FileMerge with project as -merge target
# better than using opendiff
#
# cd to your project dir and and run difftool like this:
# git difftool -d -x gdiff
# find top level of git project
dir=$PWD
until [ -e "$dir/.git" ]; do
@miner
miner / kein.sh
Last active Apr 29, 2017 — forked from cgrand/kein.sh
Launch a plain clojure repl according to project.clj without leiningen (most of the time)
View kein.sh
#!/bin/bash
# launch a clojure plain repl but with options and classpath matching project.clj
# Except when project.clj changes (and on first launch), lein is not called.
if [ ! -f "project.clj" ]; then
echo "No project.clj"
exit 1
fi
# stat (mostly) protects against staleness of copied project dir
@miner
miner / infix.clj
Last active Jun 26, 2016
An infix data-reader
View infix.clj
(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))
@miner
miner / bowling.clj
Last active May 19, 2016
Bowling Kata in Clojure
View bowling.clj
(ns miner.bowling)
;; http://codingdojo.org/cgi-bin/index.pl?KataBowling
;; https://www.javacodegeeks.com/2016/05/bowling-kata-clojure-f-scala.html
;; game is a string, encoding balls rolled
;; X for strike
;; / for spare
;; - for a miss or gutter ball
;; 1-9 for that many pins
View dijkstra_primes.clj
(ns miner.dijkstra-primes)
;; ----------------------------------------------------------------------
;; http://heinrichhartmann.com/2016/04/03/Dijkstra's-Prime-Number-Algorithm.html
;; https://github.com/HeinrichHartmann/DijkstraPrimes/blob/master/Primes.lua
;; Converted to Clojure by SEM. Note that there are lots of shadowing and recursive calls in
;; the Clojure code to avoid the mutation in the original code. The Clojure loops are a bit
;; ugly. Not sure if this is the best way to do things. However, the performance is pretty
;; good.
View 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
View submap.clj
;; 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))
You can’t perform that action at this time.