ary / 3 #
Sometimes I wish Array.partition
could split into more than halves. Something like partition_by or commonality. Like an Array./
method!
class Array def / len inject([]) do |ary, x| ary << [] if [*ary.last].nitems % len == 0 ary.last << x ary end end end >> products = %w[cycles vents hoops willies moogs rifles pools fawns tridents] >> products / 3 => [["cycles", "vents", "hoops"], ["willies", "moogs", "rifles"], ["pools", "fawns", "tridents"]] >> products / 2 => [["cycles", "vents"], ["hoops", "willies"], ["moogs", "rifles"], ["pools", "fawns"], ["tridents"]]
The other alternative being a hopscotch with a flip-flop.
mfp
This should be faster (didn’t try it):
Actually it’s not
Array.partition
butEnumerable#partition
, so you could define it there (given a better name).yxhuvud
class Array def / len each_index{|i| self[(i + 1)...(i + len)] = self[i+len] } end end
scili
Is this the same as each_slice ?
Granted, the source isn’t quite as compact.
each_slice(step=nil, &yld)
Iterate through slices. If slicing step is not given, the the arity if the block is used.why
yxhuvud: Super sly, but that method needs a bang.
scili: Ohhh, using the arity of the block is a wise idea! Geez, Facets is an entire schoolhouse of fledgling RCRs.
Danno
each_slice is writen in a peculairly inefficient way, unless I’m mistaken, also… am I crazy or does the first example need to drop the parameter 2 to demonstrate the behavior when step isn’t supplied?
My fixed(?) version:Danno
Uh, scratch that, not inefficient, just very unDRY.
praetorian
ruby nube, but wouldn’t
be more keeping with the idea of division?
cowgod
Aye, the functionality described in the post is useful, but it’s not what I’d expect from what looks like a division operator. I’d expect
products / 2
to return
[["cycles", "vents", "hoops", "willies", "moogs"], ["rifles", "pools", "fawns", "tridents"]]
Danno
Maybe it should be called slice_by then?
choonkeat
whichever the algo, or name. just thought that it should be noted that its important to be keeping those nils around. e.g. size() instead of nitems()
[1,2,nil,3,nil,nil,4,5,6] / 4 => [[1, 2, nil, 3], [nil, nil, 4, 5], [6]] => [[1, 2, 3, 4], [5, 6]]
choonkeat
oops. the examples was,
[1,2,nil,3,nil,nil,4,5,6] / 4 => [[1, 2, nil, 3], [nil, nil, 4, 5], [6]] <br><br> [1,2,nil,3,nil,nil,4,5,6].compact / 4 => [[1, 2, 3, 4], [5, 6]]
why
Okay, so maybe mine should use the modulo then.
jes5199
i wrote some related but different code to cut up Ranges in to partitions of a known size: I called it Range.%
I think must have been half-asleep when I wrote the code, though, I looks sort of ugly:
jes5199
Actually, Why, I would call your method % and have / divide your array into (n) parts of equal size
[1,2,3,4,5,6] / 2 => [[1,2,3], [4,5,6]]
[1,2,3,4,5,6] % 2 => [[1,2], [3,4], [5,6]]
jes5199
nevermind that last comment, you’re already agreeing with me.
mfp
fansipans
mfp: mmmmhm! hallelujah!(sic)
really really noobish
I’ve been lurking in the Ruby/Rails commuity for a while, but I haven’t seen many references to ‘Facets’. Why? Is seems a suprememly useful library, but no one mentions it.
For example; I don’t find it pre-gemed (real noob here) on many sites that offer Ruby/Rails hosting.
Will I run into trouble if I install then depend on Facets?
Sorry for the distraction.
jakdak
n00bi3 – u could either ask them to add it for u (possibly offer them a nice chocolate cake as a reward) – they are usually quite friendly
or u could copy and paste the function(s) u need from the source code of the gem into ur project :-)
ntk
My two cents: a = (1..10).to_a; maxsubarraysize = 3-1; result = []; while a.size > 0; result << a.slice!(0..maxsubarraysize); end
praetorian
”.oO(Why are they all rewriting what enumerator already does and more efficiently at that)”
shrug
Because we are ignorant.
cheers, prat
Austin
noobish: You don’t see many references to Facets because Facets is … far too large for most people to want to use. There are sometimes other reasons, too.
I personally wouldn’t use any library that depended on Facets, because I vehemently disagree with most of the “design” decisions made in making the library.
Ezra Zygmuntowicz
Really Really Noobish
Thanks JakDak, I know how to use gems. :-). I’m more interested in Austins answer. What design decisions did they make that turn you off?
ntk
Ezra, this is the all-in-one solution! Nice!
lundo
Why not “if offset+x > size – 1 THEN ret.last << nil; else ret.last … end”? Would be a bit easier to read, imho!
Comments are closed for this entry.