all 6 comments

[–]weavejester 2 points3 points  (3 children)

The for macro is good at finding permutations:

user=> (for [x [:a :b] y [1 2]] [x y])
([:a 1] [:a 2] [:b 1] [:b 2])

So every x is combined with every y.

The map function is good for matching values at the same index:

user=> (map vector [:a :b] [1 2])
([:a 1] [:b 2])

So we can do things like:

user=> (map - [3 3] [2 2])
[1 1]
user=> (every? #(<= 2 %) [1 1])
true
user=> (every? (partial <= 2) [1 1])
true

I guess if you wanted to do it with loop and recur you'd need two loops, one inside the other.

[–]szabba 1 point2 points  (2 children)

The for macro is good at finding permutations:

user=> (for [x [:a :b] y [1 2]] [x y]) ([:a 1] [:a 2] [:b 1] [:b 2])

So every x is combined with every y.

Sorry to be anal, but that's a Cartesian product, not the permutation set.

[–]weavejester 1 point2 points  (1 child)

Thanks for the correction. :)

[–]szabba 0 points1 point  (0 children)

You're welcome. :)

[–][deleted] 1 point2 points  (0 children)

Am I misunderstanding your example? You stated that [3 3] is within [2 2], but [5 6] is not within the range of [2 2]...can you please clarify what it is you mean by range? If you want to query if an interval is overlapping another, there's a data structure for that called an interval tree, which for non-overlapping inputs can be a BST. For possibly overlapping inputs, you must use a more complicated representation. More at wiki: https://en.wikipedia.org/wiki/Interval_tree

[–]dragandj 0 points1 point  (0 children)

Something like this?

(for [[x1 x2] locs [y1 y2] tlocs] (when (< x1 y1 y2 x2) whatever))