Combine the sequence with yourself recursively, without duplicates

It is hard to explain, but easy to show. Not sure why I myself did not understand this, so I have to skip something in clojure, which is obvious.

I need to combine the vector with me, into a vector, and I need to combine the first element with all the other elements, and then the second element with all the other elements (3rd and after).

As a brief example: [1 2 3 4 5]

I need a function: [[1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5]]

If this is like getting half the pairs for a large matrix, you're right. I just want to solve half the matrix minus the average diagonal. This is the only part in which I need to be consistent (so I only solve half), and the rest I want to use a reducer library to parallelize heavier maths in the background.

Thanks in advance!

0
source share
3 answers

What you want is built into clojure / math.combinatorics: https://github.com/clojure/math.combinatorics

The main example you are looking for is in readme, but for completeness of this answer I will repeat it here:

(ns example.core (:require [clojure.math.combinatorics :as combo])) (combo/combinations [1 2 3] 2) ;;=> ((1 2) (1 3) (2 3)) 
+3
source

I would just use for :

 (for [ i (range 1 6) j (range 6) :when (< ij)] [ij]) ; => ([1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5]) 
+3
source
 (def v [1 2 3 4 5]) (for [i (range (count v)) :let [vv (drop iv)] r (rest vv)] [(first vv) r]) => ([1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5]) 
0
source

All Articles