Skip to content

Instantly share code, notes, and snippets.

@kencoba
Created March 6, 2011 02:47
Show Gist options
  • Save kencoba/856980 to your computer and use it in GitHub Desktop.
Save kencoba/856980 to your computer and use it in GitHub Desktop.
n-indexed clojure.contrib.seq-utils.indexed for n-dimension.
(ns n-indexed
(:require [clojure.contrib.seq-utils :as s])
(:use [clojure.test]))
(defn n-indexed [s]
(cond (not (coll? s)) s
(not (coll? (first s))) (s/indexed s)
:else (reduce concat
(for [[i ls] (s/indexed (map n-indexed s))]
(map #(cons i %) ls)))))
(deftest test-n-indexed
(is (= 'a (n-indexed 'a)))
(is (= '((0 a)) (n-indexed '(a))))
(is (= '((0 a) (1 b)) (n-indexed '(a b))))
(is (= '((0 0 a) (0 1 b) (1 0 c) (1 1 d)) (n-indexed '((a b) (c d)))))
(is (= '((0 0 0 a) (0 0 1 b) (0 1 0 c) (0 1 1 d)
(1 0 0 e) (1 0 1 f) (1 1 0 g) (1 1 1 h))
(n-indexed '(((a b) (c d)) ((e f) (g h)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment