7 ms·
I wish more languages follow the sense of being fun to code like ruby. [1, 2, 3] - [1, 3] #Yields [2] The code is readable, intuitive and short yet many langu
by h1d 8y ago
I wish more languages follow the sense of being fun to code like ruby.
[1, 2, 3] - [1, 3]
#Yields [2]
The code is readable, intuitive and short yet many languages never have this simplicity.
JS example.
https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript https://stackoverflow.com/questions/1187518/how-to-get-the-d...
- rgovostes 8y agoThat doesn't seem super intuitive to me. I assume that [1, 2, 3] + [1] == [1, 2, 3, 1] but then if I subtract [1] again, does it take away the first 1, or the last 1? It turns out it removes both of them. So subtracting a list is not the inverse of adding a list. In Python, there is the distinction between lists and sets, and while both support the + operator, only sets support the - operator for this reason.
- ricardobeat 8y agoFirst one is called 'concatenation', second 'array difference'. It does require a one-time look at documentation, which doesn't take anything away from the fun or practical aspects. There is no expectation that they are inverse or commutative.
- chx 8y ago> There is no expectation that they are inverse or commutative Please. I am not a Ruby coder but I fully expect any language that a + b - b is just a and also a + b - a is just b.
- lifthrasiir 8y agoUh, floating point numbers don't.
- ricardobeat 8y agoThese are not mathematical operators (hence the “overloading”).
- lifthrasiir 8y agoThe takeaway is that you can't have the single best-defined array difference---it is badly adapted from the well-defined set difference. There are tons of possible values that `[1a, 1b, 1c, 1d, 2, 3] - [1e, 1f, 2]` can yield (where `1a` through `1f` all compare identically but have different object identities): [1b, 1c, 1d, 3] (remove the first occurrence only, no matter the RHS has many occurrences) [1a, 1b, 1c, 3] (remove the last occurrence only) any of [1a, 1b, 1c, 3], [1a, 1b, 1d, 3] etc. (remove any single unspecified occurrence) [1c, 1d, 3] (remove the first K occurrences where K is the multiplicity) [1a, 1b, 3] (remove the last K occurrences) any of [1a, 1b, 3], [1b, 1c, 3], [1c, 1d, 3] etc. (remove any K unspecified occurrences) [1a, 1b, 1c, 1d, 3] (remove a whole group of equal occurrences only when multiplicites match) [1a, 3] (flatten both the LHS and the RHS and calculate the set difference) [1d, 3] (same as above, but the set flattening prefers later occurrences) any of [1a, 3], [1b, 3], [1c, 3] or [1d, 3] (same as above, but the set flattening is unspecified) any of [1a, 3], ..., [1d, 3], [1e, 3] or [1f, 3] (same as above, but without any guarantee of the identical element) runtime error, as there are multiple same elements in the LHS and/or the RHS [0, 0, -1, NaN, NaN, NaN] (okay, I'll stop being clever and calculate actual numeric differences) See? The "array difference" is not enough. Something like the "array difference with multiple occurrences removed in the order of appearance" would work, but this refutes the OP's claim of the "array difference" being "readable" and "intuitive".
- ricardobeat 8y agoI don’t see what’s complex about it: make a copy of the first array excluding any elements that exist in the second. Only one outcome possible.
- lifthrasiir 8y agoThe multiplicity is important if you see an array as a bag, i.e. a multiset. Some may reasonably expect the pairwise difference, which is indeed a default mode for vectors and matrixes (even C++ has one, `std::valarray`). There are also tons of implementation concerns I haven't ever dabbled.
- anonytrary 8y agoIf you're talking about sets, then yes. Doing that on lists would be awful. Lists don't have the same guarantees as sets, so the operator would have plenty of behavioral ambiguity. You'd have to learn the convention of the operator for edge cases where the operation is not well defined.