[#33640] [Ruby 1.9-Bug#4136][Open] Enumerable#reject should not inherit the receiver's instance variables — Hiro Asari <redmine@...>

Bug #4136: Enumerable#reject should not inherit the receiver's instance variables

10 messages 2010/12/08

[#33667] [Ruby 1.9-Bug#4149][Open] Documentation submission: syslog standard library — mathew murphy <redmine@...>

Bug #4149: Documentation submission: syslog standard library

11 messages 2010/12/10

[#33683] [feature:trunk] Enumerable#categorize — Tanaka Akira <akr@...>

Hi.

14 messages 2010/12/12
[#33684] Re: [feature:trunk] Enumerable#categorize — "Martin J. Dst" <duerst@...> 2010/12/12

[#33687] Towards a standardized AST for Ruby code — Magnus Holm <judofyr@...>

Hey folks,

23 messages 2010/12/12
[#33688] Re: Towards a standardized AST for Ruby code — Charles Oliver Nutter <headius@...> 2010/12/12

On Sun, Dec 12, 2010 at 9:55 AM, Magnus Holm <judofyr@gmail.com> wrote:

[#33689] Re: Towards a standardized AST for Ruby code — "Haase, Konstantin" <Konstantin.Haase@...> 2010/12/12

On Dec 12, 2010, at 17:46 , Charles Oliver Nutter wrote:

[#33763] [Ruby 1.9-Bug#4168][Open] WeakRef is unsafe to use in Ruby 1.9 — Brian Durand <redmine@...>

Bug #4168: WeakRef is unsafe to use in Ruby 1.9

43 messages 2010/12/17

[#33815] trunk warnflags build issue with curb 0.7.9? — Jon <jon.forums@...>

As this may turn out to be a 3rd party issue rather than a bug, I'd like some feedback.

11 messages 2010/12/22

[#33833] Ruby 1.9.2 is going to be released — "Yuki Sonoda (Yugui)" <yugui@...>

-----BEGIN PGP SIGNED MESSAGE-----

15 messages 2010/12/23

[#33846] [Ruby 1.9-Feature#4197][Open] Improvement of the benchmark library — Benoit Daloze <redmine@...>

Feature #4197: Improvement of the benchmark library

15 messages 2010/12/23

[#33910] [Ruby 1.9-Feature#4211][Open] Converting the Ruby and C API documentation to YARD syntax — Loren Segal <redmine@...>

Feature #4211: Converting the Ruby and C API documentation to YARD syntax

10 messages 2010/12/26

[#33923] [Ruby 1.9-Bug#4214][Open] Fiddle::WINDOWS == false on Windows — Jon Forums <redmine@...>

Bug #4214: Fiddle::WINDOWS == false on Windows

15 messages 2010/12/27

[ruby-core:33991] Re: [feature:trunk] Enumerable#categorize

From: Marc-Andre Lafortune <ruby-core-mailing-list@...>
Date: 2010-12-29 08:58:49 UTC
List: ruby-core #33991
Hi!

On Tue, Dec 28, 2010 at 1:45 PM, Tanaka Akira <akr@fsij.org> wrote:

I agree with most of what you wrote. Here are comments on some parts:

> I'm not sure that matz will satisfy the name 'associate'.

Or `mash`, `make_hash` or `build_hash`.

> * 'associate' doesn't create nested hash.
>
> associate' is simpler here.
>
>  think 'associate' can be extended naturally that the method creates
> ested hash when the block returns an array with 3 or more elements.

Indeed, that could be. I need to read again your implementation for details.

> * 'associate' assumes {|v| v } if the block is not given.
>
> his simplify some usages.
> owever this forbids Ruby 1.9 style enumerator creation
> hich returns an enumerator when block is not given.
> his means we cannot write enum.associate.with_index {|v, i| ... }.

Indeed, but you can write `enum.each_with_index.associate.{|v, i| ...
}` gives the same result, and `to_enum` is always available.

The no block form being {|v| v} can be changed, of course. I
originally wanted to propose this as `to_h`, and it thus had to be
idempotent on any Hash. If it is not called `to_h`, then the form
without a block can return an enumerator.

I find that returning an enumerator is super useful on iterators that
modify how the iteration is done (each_with_index, each_slice, ...),
but not so useful on iterators that create objects (map, all?, ...); I
tend to stick these at the end of the chain. Either way can work; it
doesn't change the important part of the proposal.

> * 'associate' treates non-array block value.
>
> his is more complex than 'categorize'.
>
>  feel it is bit distorted specification.
> specially "(first)" in "Otherwise the value is the result of the block
> nd corresponding key is the (first) yielded item."

For the return value, we should look into the possibility merging
categorize & associate.

For the "default index" when the return value doesn't provide one, I
agree it's not "pure". Again, this was because I wanted it to be idem
potent on hashes, but I feel it is much more useful anyways. I don't
recall ever creating a hash with arrays as keys.

I feel that Ruby favors the practical side of things over purity.

> owever symbol and symbol.to_proc will be different, though.

Yes, but I feel that's normal, like the fact that `inject(:+)` and
`inject(&:+)` are different.

> * 'associate' doesn't have a way to specify the seed.
>
> his is simpler specification than 'categorize'
> ut this makes some usages more complex.

I'm curious as to what usage you are thinking. I feel that at the
block level, you should assume that this is first time you associate a
key with a value, as conflicts are the responsibility of the "merge"
argument. If a seed is needed, it should go in the block.

Also, I feel that seeds can be easily misunderstood(e.g. are they
dupped like slice_before/chunk? just copied by reference like
Hash.new(seed)?) This adds a level of complexity which can be avoided
here.

> n your 'associate' examples for [ruby-talk:347364] and
> ruby-talk:327908], array and string concatenation is O(n**2).

Indeed. To make it O(n), replace `:+` by `:concat`.

> Actually I want one more method for counting.
> (I want 3 methods: grouped hash, normal hash, count hash)

A count hash could be useful, indeed. Again, I think `associate` is
much more needed.
Indeed, `inventory{|x| whatever}` can be computed with
`associate(:+){|x| [whatever, 1]}`.

>> I just feel that `associate` should
>> be added in priority over `categorize`.
>
> matz felt similar. ruby-dev:42643]

I'm glad to hear that :-) Has another specification been proposed and
how does it compare to `associate`?



> Your solution depends on 'a' has no duplicated elements.

Oh, right, I didn't read the original ruby-talk post. This doesn't
change the comparison of the first line which is what matters. So let
my second line use flat_map+fetch :-)

h = a.categorize.with_index {|e, i| [e,i] }
p dest == b.map.with_index {|e, j| h[e] ? h[e].map {|i| [i,j] } : []}.flatten(1)
# compared to
h = a.each_with_index.associate
p dest == b.flat_map.with_index {|e, j| h.fetch(e){[]}.map {|i| [i,j] }}

Thanks
--
Marc-Andr

In This Thread