Created
March 6, 2011 02:47
-
-
Save kencoba/856980 to your computer and use it in GitHub Desktop.
n-indexed clojure.contrib.seq-utils.indexed for n-dimension.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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