elm-community / list-extra

Convenience functions for working with List.
http://package.elm-lang.org/packages/elm-community/list-extra/latest
MIT License
135 stars 58 forks source link

Add `pairings` function #118

Closed Janiczek closed 5 years ago

Janiczek commented 5 years ago
pairings [1,2,3,4]
--> [ (1,2), (1,3), (1,4), (2,3), (2,4), (3,4) ]

This is essentially doing the equivalent of the following Python loop:

def pairings(xs):
    return [(xs[i], xs[j]) for i in range(len(xs))
                           for j in range(i+1, len(xs))]

It's useful eg. for Advent of Code puzzles, or any occasion where you need to check elements of a list against each other, without duplicates and without checking against self ((x,x)).

Chadtech commented 5 years ago

Hey @Janiczek !

This looks good to me. A few points tho:

  1. Im guessing you were working on the advent of code challenge, would you mind linking to the problem and your solution that used this function?
  2. Would you mind adding a few tests to our test file?
  3. Maybe the name could be more specific? Im just worried that this function will get mixed up with other functions that involve lists and pairing and that it seem like "pairings" describes both equally well. Like "zip" is a kind of "pairings" it seems. Some alternative name ideas: pairOwnMembers, everyPairOf
prikhi commented 5 years ago

Maybe the name could be more specific? Im just worried that this function will get mixed up with other functions that involve lists and pairing and that it seem like "pairings" describes both equally well. Like "zip" is a kind of "pairings" it seems. Some alternative name ideas: pairOwnMembers, everyPairOf

It's called the Cartesian Product, but that might too mathy.

Maybe pairPermutations?

Edit: Nevermind, I would assumed the permutations would include (2,1), not just (1,2).

Janiczek commented 5 years ago

@Chadtech

  1. Yes, it was AoC 2018, problem 3. The pairings function allows me to simplify the code in this way.
  2. Of course, yes I'll add them. In a moment :)
  3. I guess the mathematical name is "combinations without repetition". The usual example is "handshakes". I'm partial to changing the name to combinations. What do you think?
Janiczek commented 5 years ago

OTOH, this is not the general function for combinations -- it only computes 2-combinations.

Chadtech commented 5 years ago

Yeah seems like combinations is more general. Im worried that even pairings is too general. How would you know from the name that it doesnt include (1,2) and (2,1) in the output list?

Seems like the most specific name would be handshakes. What do you think of that? Im worried about the name and the documentation being too mathematical and people not knowing what a "handshake" is (I didnt, I had to look it up, and while it was straight forward reading about it ideally looking up the term wouldnt be a necessary step).

Chadtech commented 5 years ago

pairsWithoutReptition? uniquePairs?

pzp1997 commented 5 years ago

FWIW I am a fan of handshakes.

Janiczek commented 5 years ago

@Chadtech your uniquePairs sound the best to me.

The bit about order not mattering ((a,b) being the same as (b,a)) would have to be deduced from the documentation and/or the examples, but I think there's only so much meaning you can put into a function name...

Chadtech commented 5 years ago

Okay. Lets do uniquePairs then? With that change, I'd be happy to merge.

Chadtech commented 5 years ago

Perfect! Thanks for your PR