hello everyone,
I just can't see what is wrong here. It looks ok to me. And the filter function works fine if
I use it directly with filter, but I get a very short traceback using it in a query.
This is basically a category vector, I just want to filter based on if the vectors match
always matching for the length of the short one, which will always be in the specified list
not the other way around. I've written lots of filter functions but I think I must be going blind.
Maybe all I need is some fresh eyes to take a look.
In addition to the working code below, I also tried using "cats" as a sub-query
with (<-....) so that fcats looked like this:
(defn cats []
(<- [?id ?catv]
(cat-tap ?id ?lvl ?code)
(:sort ?lvl)
(mk-vec ?code :> ?catv)))
(defn fcats [catf]
(let [prods (cats)]
(??<- [?id ?catv]
(cats ?id ?catv)
;(catf ?catv)
)))
This has been a mixed bag. It makes me suspect my environment.
I've seen it work, and and I've seen unhandled exceptions, and also this error which is
better than nothing. Although I can't seem to get this error back.
Tails required in merge-tails. (not-empty tails)
At the moment, using a sub-query is not working at all for this example. I think it would
simplify the problem a bit if it did.
I can't say how many times I've written queries like that. What is it about this one?
I have noticed that when I use real code, I write the sub query usage like this: (cats :> ?id ?catv)
which works fine for my more complex production code that this example is modeled after.
The other thing I tried was turning my filter-fn into map fn so I could return the result
and see it in the output. That actually worked for a while. Then I restarted my REPL and it
stopped. Again, I'm suspicious something weird is going on.
Now, it results in the same crash as the filter does.
(defn catv-filter
"Returns a function that filters by none, or by a lists of category vectors.
Takes an empty list or a list of vectors of category numbers. The filter takes a
vector of category numbers. The vector will be shortened to match the comparison
category vectors as needed so that sub categories will match. "
[categories]
(filterfn [v]
(if (empty? categories)
true
(reduce #(or (= (vec (take (count %2) v)) %2) %1) false categories))))
(defmapfn catv-filter-res
"Returns a function that filters by none, or by a lists of category vectors.
Takes an empty list or a list of vectors of category numbers. The filter takes a
vector of category numbers. The vector will be shortened to match the comparison
category vectors as needed so that sub categories will match. "
[categories]
(fn [v]
(if (empty? categories)
true
(reduce #(or (= (vec (take (count %2) v)) %2) %1) false categories))))
(defbufferfn mk-vec [tuples]
[[(reduce #(conj %1 %2) [] (map first tuples))]])
(def cat-tap [["foo" 1 3]
["foo" 2 100]
["foo" 3 8]
["foo" 4 12]
["bar" 1 5]
["bar" 2 22]
["bar" 3 36]
["bar" 4 212]
["baz" 1 3]
["baz" 2 100]
["baz" 3 8]
["baz" 4 300]])
(def allcats (catv-filter nil))
(def catf (catv-filter '([3 100 8 12])))
(def catf2 (catv-filter '([3 100 8])))
(defn cats []
(??<- [?id ?catv]
(cat-tap ?id ?lvl ?code)
(:sort ?lvl)
(mk-vec ?code :> ?catv)))
(defn catsres [s]
(let [fres (catv-filter-res s)]
(??<- [?id ?catv ?res]
(cat-tap ?id ?lvl ?code)
(:sort ?lvl)
(mk-vec ?code :> ?catv)
(fres ?catv :> ?res))))
(defn fcats [catf]
(??<- [?id ?catv]
(cat-tap ?id ?lvl ?code)
(:sort ?lvl)
(mk-vec ?code :> ?catv)
(catf ?catv)))
(defn filter-test [f]
(filter #(f (second %)) (cats)))
test> (cats)
(["bar" [5 22 36 212]] ["baz" [3 100 8 300]] ["foo" [3 100 8 12]])
test> (filter-test allcats)
(["bar" [5 22 36 212]] ["baz" [3 100 8 300]] ["foo" [3 100 8 12]])
test> (filter-test catf)
(["foo" [3 100 8 12]])
test> (filter-test catf2)
(["baz" [3 100 8 300]] ["foo" [3 100 8 12]])
--test> (fcats allcats)
Unhandled cascading.flow.FlowException
local step failed
FlowStepJob.java: 219 cascading.flow.planner.FlowStepJob/blockOnJob
FlowStepJob.java: 149 cascading.flow.planner.FlowStepJob/start
FlowStepJob.java: 124 cascading.flow.planner.FlowStepJob/call
FlowStepJob.java: 43 cascading.flow.planner.FlowStepJob/call
FutureTask.java: 266 java.util.concurrent.FutureTask/run
ThreadPoolExecutor.java: 1142 java.util.concurrent.ThreadPoolExecutor/runWorker
ThreadPoolExecutor.java: 617 java.util.concurrent.ThreadPoolExecutor$Worker/run
Thread.java: 745 java.lang.Thread/run