How would you get every possible bination of 2 elements in an array?
For example:
[
1,
2,
3,
4
]
bees
[
[1, 2],
[1, 3],
[1, 4],
[2, 1],
[2, 3],
[2, 4],
[3, 1],
[3, 2],
[3, 4],
[4, 1],
[4, 2],
[4, 3]
]
This answer uses brute force but is there a functional way with Ramda and or currying? Derive every possible bination of elements in array
How would you get every possible bination of 2 elements in an array?
For example:
[
1,
2,
3,
4
]
bees
[
[1, 2],
[1, 3],
[1, 4],
[2, 1],
[2, 3],
[2, 4],
[3, 1],
[3, 2],
[3, 4],
[4, 1],
[4, 2],
[4, 3]
]
This answer uses brute force but is there a functional way with Ramda and or currying? Derive every possible bination of elements in array
Share Improve this question edited May 23, 2017 at 12:32 CommunityBot 11 silver badge asked Feb 28, 2016 at 20:37 sa555sa555 3101 gold badge4 silver badges12 bronze badges 05 Answers
Reset to default 8Here's an elegant solution:
// permutations :: Number -> [a] -> [[a]]
const permutations = R.pose(R.sequence(R.of), R.flip(R.repeat));
Usage examples:
permutations(2, [1, 2, 3, 4]);
// => [[1, 1], [1, 2], ..., [4, 3], [4, 4]]
permutations(3, [1, 2, 3, 4]);
// => [[1, 1, 1], [1, 1, 2], ..., [4, 4, 3], [4, 4, 4]]
Borrowing from Haskell:
as = [1, 2, 3]
f xs = do
a <- xs
b <- xs
return $ if a == b then [] else [a, b]
main = print $ filter (not . null) . f $ as
This is my Ramda version:
var as = [1, 2, 3, 4]
var f = xs =>
R.pipe(
R.chain(a => R.map(b => a == b ? [] : [a, b])(xs))
, R.filter(R.pipe(R.isEmpty, R.not))
)(xs)
console.log(f(as))
PS. LiveScript has a nice syntax for this: http://homam.github.io/try-livescript/#wele/lists
For choosing a subset of ant size: Ramda code
var g = (xs, n) =>
n == 0 ? [[]] :
R.isEmpty(xs) ? [] :
R.concat(
R.map(R.prepend(R.head(xs)), g(R.tail(xs), n - 1))
, g(R.tail(xs), n)
)
g(as, 3)
If you just want two elements, the answer from Oriol should do you fine. But if you want something that extends to any size subgroup, something like this might do:
const permutations = (n, tokens, subperms = [[]]) =>
n < 1 || n > tokens.length ?
subperms :
R.addIndex(R.chain)((token, idx) => permutations(
n - 1,
R.remove(idx, 1, tokens),
R.pose(R.map, R.append)(token)(subperms)
), tokens);
permutations(2, [1, 2, 3, 4]);
//=> [[1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4],
// [3, 1], [3, 2], [3, 4], [4, 1], [4, 2], [4, 3]]
permutations(3, [1, 2, 3, 4]);
//=> [[1, 2, 3], [1, 2, 4], [1, 3, 2], [1, 3, 4], [1, 4, 2], [1, 4, 3],
// [2, 1, 3], [2, 1, 4], [2, 3, 1], [2, 3, 4], [2, 4, 1], [2, 4, 3],
// [3, 1, 2], [3, 1, 4], [3, 2, 1], [3, 2, 4], [3, 4, 1], [3, 4, 2],
// [4, 1, 2], [4, 1, 3], [4, 2, 1], [4, 2, 3], [4, 3, 1], [4, 3, 2]]
This version was slightly adapted from one I presented in Ramda's Gitter room. There I suggested it was overwrought, but that was for full permutations. It seems appropriate for n-binations.
You can see it in action on the Ramda REPL.
You don't need any library, you can do it trivially in vanilla-js using a nested loop:
var arr = [1, 2, 3, 4],
result = [];
for(var i=0; i<arr.length; ++i)
for(var j=0; j<arr.length; ++j)
if(i !== j)
result.push([arr[i], arr[j]]);
This will work for any length of permutations just adjust it to cut off at 2.
function permutate(input, output) {
if (input.length === 0) {
document.body.innerHTML += "<div>" + output + "</div>";
}
for (var i = 0; i < input.length; i++) {
output.push(input[i]);
permutate(input.slice(0, i).concat(input.slice(i + 1)), output);
output.pop();
}
}
permutate([1, 2, 3, 4], []);