15 Matching Annotations
- Jun 2021
-
stackoverflow.com stackoverflow.com
-
I don't think this warrants adding to the Array class, since it's not generalizable to all the types that Arrays can contain.
You could say the same thing about
Array#sort
. It can cause an error if elements of the array aren't all of the same type/shape. Just make sure it's safe to use first, and thenArray#sort
,Array#sum
,Array#average
, ... are all quite handy and useful to have on Array class. -
instance_eval { reduce(:+) / size.to_f }
-
- Jul 2020
-
bugs.ruby-lang.org bugs.ruby-lang.org
-
bugs.ruby-lang.org bugs.ruby-lang.org
-
stackoverflow.com stackoverflow.com
-
require 'set' class Array def uniq_elements(&prc) prc ||= ->(e) { e } uniques, dups = {}, Set.new each do |e| k = prc[e] ((uniques.key?(k)) ? (dups << k; uniques.delete(k)) : uniques[k] = e) unless dups.include?(k) end uniques.values end end
-
-
twitter.com twitter.com
-
To me the difference between [1,1,2,2,3,3] and [1,2,3] is not []
-
It’s even worse that there’s no alternative method that does the unsurprising thing IMO.
-
-
github.com github.com
-
One may expect Array#- to behave like mathematical subtraction or difference when it doesn't. One could be forgiven to expect the following behavior: [1,1,2,2,3,3,4,4] - [1,2,3,4] => [1,2,3,4]
-
I'll freely admit I was surprised by this behavior myself since I needed to obtain an Array with only one instance of each item in the argument array removed.
-
-
bugs.ruby-lang.org bugs.ruby-lang.org
-
Arrays are not sets. Trying to treat them as if they are is an error, and will create subtle problems. What should be the result of the following operations? [1, 1] | [1] [1] | [1, 1] Of course, there are more interesting examples. These two are to get you started. I don't care what the results currently are. I don't care what you think they should be. I can present extremely strong arguments for various answers. For this reason, I believe that #| is an ill-defined concept. Generalizing an ill-defined concept is a world of pain. If you insist on treating objects of one class as if they were members of a different class, there should be bumps in the road to at least warn you that maybe this is a bad idea. I'm not going to argue that we should remove or deprecate #|. I don't think of myself as a fanatic. But encouraging this sort of abuse of the type system just creates problems.
-
-
github.com github.com
-
Tags
Annotators
URL
-