зеркало из https://github.com/github/ruby.git
[DOC] Tweaks for Enum#tally
This commit is contained in:
Родитель
53e3795379
Коммит
76ccd1df37
54
enum.c
54
enum.c
|
@ -1235,29 +1235,47 @@ tally_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* tally -> new_hash
|
* tally(hash = {}) -> hash
|
||||||
* tally(hash) -> hash
|
|
||||||
*
|
*
|
||||||
* Returns a hash containing the counts of equal elements:
|
* When argument +hash+ is not given,
|
||||||
*
|
* returns a new hash whose keys are the distinct elements in +self+;
|
||||||
* - Each key is an element of +self+.
|
* each integer value is the count of occurrences of each element:
|
||||||
* - Each value is the number elements equal to that key.
|
|
||||||
*
|
|
||||||
* With no argument:
|
|
||||||
*
|
*
|
||||||
* %w[a b c b c a c b].tally # => {"a"=>2, "b"=>3, "c"=>3}
|
* %w[a b c b c a c b].tally # => {"a"=>2, "b"=>3, "c"=>3}
|
||||||
*
|
*
|
||||||
* With a hash argument, that hash is used for the tally (instead of a new hash),
|
* When argument +hash+ is given,
|
||||||
* and is returned;
|
* returns +hash+, possibly augmented; for each element +ele+ in +self+:
|
||||||
* this may be useful for accumulating tallies across multiple enumerables:
|
|
||||||
*
|
*
|
||||||
* hash = {}
|
* - Adds it as a key with a zero value if that key does not already exist:
|
||||||
* hash = %w[a c d b c a].tally(hash)
|
*
|
||||||
* hash # => {"a"=>2, "c"=>2, "d"=>1, "b"=>1}
|
* hash[ele] = 0 unless hash.include?(ele)
|
||||||
* hash = %w[b a z].tally(hash)
|
*
|
||||||
* hash # => {"a"=>3, "c"=>2, "d"=>1, "b"=>2, "z"=>1}
|
* - Increments the value of key +ele+:
|
||||||
* hash = %w[b a m].tally(hash)
|
*
|
||||||
* hash # => {"a"=>4, "c"=>2, "d"=>1, "b"=>3, "z"=>1, "m"=> 1}
|
* hash[ele] += 1
|
||||||
|
*
|
||||||
|
* This is useful for accumulating tallies across multiple enumerables:
|
||||||
|
*
|
||||||
|
* h = {} # => {}
|
||||||
|
* %w[a c d b c a].tally(h) # => {"a"=>2, "c"=>2, "d"=>1, "b"=>1}
|
||||||
|
* %w[b a z].tally(h) # => {"a"=>3, "c"=>2, "d"=>1, "b"=>2, "z"=>1}
|
||||||
|
* %w[b a m].tally(h) # => {"a"=>4, "c"=>2, "d"=>1, "b"=>3, "z"=>1, "m"=>1}
|
||||||
|
*
|
||||||
|
* The key to be added or found for an element depends on the class of +self+;
|
||||||
|
* see {Enumerable in Ruby Classes}[rdoc-ref:Enumerable@Enumerable+in+Ruby+Classes].
|
||||||
|
*
|
||||||
|
* Examples:
|
||||||
|
*
|
||||||
|
* - Array (and certain array-like classes):
|
||||||
|
* the key is the element (as above).
|
||||||
|
* - Hash (and certain hash-like classes):
|
||||||
|
* the key is the 2-element array formed from the key-value pair:
|
||||||
|
*
|
||||||
|
* h = {} # => {}
|
||||||
|
* {foo: 'a', bar: 'b'}.tally(h) # => {[:foo, "a"]=>1, [:bar, "b"]=>1}
|
||||||
|
* {foo: 'c', bar: 'd'}.tally(h) # => {[:foo, "a"]=>1, [:bar, "b"]=>1, [:foo, "c"]=>1, [:bar, "d"]=>1}
|
||||||
|
* {foo: 'a', bar: 'b'}.tally(h) # => {[:foo, "a"]=>2, [:bar, "b"]=>2, [:foo, "c"]=>1, [:bar, "d"]=>1}
|
||||||
|
* {foo: 'c', bar: 'd'}.tally(h) # => {[:foo, "a"]=>2, [:bar, "b"]=>2, [:foo, "c"]=>2, [:bar, "d"]=>2}
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче