Extract tests are failing because the indexes aren't as expected

I *think* the code is right an the tests are wrong, but I'm too unwell/tired to verify that just now.
This commit is contained in:
Simon Brooke 2019-06-26 18:38:18 +01:00
parent 958413d2e3
commit 551fd00292
4 changed files with 177 additions and 6 deletions

View file

@ -213,25 +213,34 @@
1)
0))
(defn dense-array?
"Basically, any vector can be considered as a dense array of one dimension.
If we're seeking a dense array of more than one dimension, the number of
dimensions should be specified as `d`."
([x]
(vector? x))
([x d]
(and (vector? x) (< d (dense-dimensions x)))))
(defn dense-to-sparse
"Return a sparse array representing the content of the dense array `x`,
assuming these `coordinates` if specified. *NOTE THAT* if insufficient
values of `coordinates` are specified, the resulting sparse array will
assuming these `axes` if specified. *NOTE THAT* if insufficient
values of `axes` are specified, the resulting sparse array will
be malformed."
([x]
(dense-to-sparse x (map #(keyword (str "i" %)) (range))))
([x coordinates]
([x axes]
(let
[dimensions (dense-dimensions x)]
(reduce
merge
(apply make-sparse-array (take dimensions coordinates))
(apply make-sparse-array (take dimensions axes))
(map
(fn [i v] (if (nil? v) nil (hash-map i v)))
(range)
(if
(> dimensions 1)
(map #(dense-to-sparse % (rest coordinates)) x)
(map #(dense-to-sparse % (rest axes)) x)
x))))))
(defn arity

View file

@ -0,0 +1,68 @@
(ns sparse-array.extract
(:require [sparse-array.core :refer :all]))
;;; The whole point of working with sparse arrays is to work with interesting
;;; subsets of arrays most of which are uninteresting. To extract an
;;; interesting subset from an array, we're going to need an extract function.
(defn- extract-from-sparse
"Return a subset of this sparse `array` comprising all those cells for which
this `function` returns a 'truthy' value."
[array function]
(reduce
merge
(apply
make-sparse-array
(cons
(:coord array)
(if (coll? (:content array)) (:content array))))
(map
#(if
(= :data (:content array))
(if (function (array %)) {% (array %)})
(let [v (extract-from-sparse (array %) function)]
(if
(empty?
(filter integer? (keys v)))
nil
{% v})))
(filter integer? (keys array)))))
(defn extract-from-dense
"Return a subset of this dense `array` comprising all those cells for which
this `function` returns a 'truthy' value. Use these `axes` if provided."
([array function]
(extract-from-dense array function (map #(keyword (str "i" %)) (range))))
([array function axes]
(let
[dimensions (dense-dimensions array)]
(reduce
merge
(apply make-sparse-array (take dimensions axes))
(if
(= dimensions 1)
(map
(fn [i v] (if (function v) (hash-map i v)))
(range)
array)
(map
(fn [i v] (if (empty? (filter integer? (keys v))) nil (hash-map i v)))
(range)
(map #(extract-from-dense % function (rest axes)) array)))))))
(defn extract
"Return a sparse subset of this `array` - which may be either sparse or
dense - comprising all those cells for which this `function` returns a
'truthy' value."
[array function]
(cond
(sparse-array? array)
(extract-from-sparse array function)
(dense-array? array)
(extract-from-dense array function)
*safe-sparse-operations*
(throw
(ex-info
"Argument passed as `array` is neither sparse nor dense."
{:array array}))))