ruby wish-list

R

Roger Pack

there's a multiples verb like .include? it would also come with
My interpretation of it is that "include" here is in the infinitive,
as in "Does x include y?", so the API as it is makes sense to me.
YMMV, of course.

Yeah both would be good.

From my side

if a.includes? b ...

works nicely.

-R
 
M

Mikael Høilund

Perhaps a bit controversial, but I'd like to see a keyword for the =20
=93current block=94 as a way to refer to the Proc instance created =
inside =20
that block. That would allow for recursive anonymous blocks like:

[[1,2,[3]],[[[5],6],7],8].map { |e|
Array =3D=3D=3D e ? e.map(&current_block) : e.to_s
}

without resorting to ugly syntax like

[[1,2,[3]],[[[5],6],7],8].map &(deep_to_s =3D proc { |e|
Array =3D=3D=3D e ? e.map(&deep_to_s) : e.to_s
})

--=20
instance_variable_set(%@\@%sample@%%@[email protected], Class.new(&proc{def =20
self.net;$;,
$/=3D'','/';%;.fqn-=20
cmtkhng;end}));Kernel.send:)"define_method",:method_missing){|
n,$_|$_<<"?kd!jhl";n=3Dsplit.map{|q|q.succ}*'';puts n.reverse.chomp.tr(*=20=

%w{" a})}
(e-mail address removed)
 
A

Adam Shelly

Perhaps a bit controversial, but I'd like to see a keyword for the "curre= nt
block" as a way to refer to the Proc instance created inside that block.
That would allow for recursive anonymous blocks like:

[[1,2,[3]],[[[5],6],7],8].map { |e|
Array =3D=3D=3D e ? e.map(&current_block) : e.to_s
}

It's not exactly the same, but couldn't you just define a
recursive map function?

class Array
def map_r &block
map{|e|Array=3D=3D=3De ? e.map_r(&block) : block[e]}
end
end
p [[1,2,[3]],[[[5],6],7],8].map_r{|e|e.to_s}

=3D> [["1", "2", ["3"]], [[["5"], "6"], "7"], "8"]
 
J

Joel VanderWerf

Mikael said:
Perhaps a bit controversial, but I'd like to see a keyword for the
“current block” as a way to refer to the Proc instance created inside
that block. That would allow for recursive anonymous blocks like:

[[1,2,[3]],[[[5],6],7],8].map { |e|
Array === e ? e.map(&current_block) : e.to_s
}

without resorting to ugly syntax like

[[1,2,[3]],[[[5],6],7],8].map &(deep_to_s = proc { |e|
Array === e ? e.map(&deep_to_s) : e.to_s
})

IIUC, this would also let us take this idiom:

pr = proc {|h,k| h[k] = Hash.new(&pr)}
h = Hash.new(&pr)
h[1][2][3] = 6
p h # ==> {1=>{2=>{3=>6}}}

and simplify to:

h = Hash.new {|h,k| h[k] = Hash.new(&current_block)}
 
M

Mikael Høilund

Perhaps a bit controversial, but I'd like to see a keyword for the =20=
"current
block" as a way to refer to the Proc instance created inside that =20
block.
That would allow for recursive anonymous blocks like:

[[1,2,[3]],[[[5],6],7],8].map { |e|
Array =3D=3D=3D e ? e.map(&current_block) : e.to_s
}

It's not exactly the same, but couldn't you just define a
recursive map function?

class Array
def map_r &block
map{|e|Array=3D=3D=3De ? e.map_r(&block) : block[e]}
end
end
p [[1,2,[3]],[[[5],6],7],8].map_r{|e|e.to_s}

=3D> [["1", "2", ["3"]], [[["5"], "6"], "7"], "8"]

That's not optimal if it's just for a single usage, especially not if =20=

it requires monkey-patching a built-in class. I could imagine this =20
being used pretty much any time recursive behavior is necessary for an =20=

iterator. And besides, what's more descriptive?

array.map { |e|
Array =3D=3D=3D e ? e.map(&current_block) : e.to_s
}

or

array.map_r { |e| e.to_s } # Please look somewhere in the source tree =20=

for the definition of Array#map_r

?

IIUC, this would also let us take this idiom:

pr =3D proc {|h,k| h[k] =3D Hash.new(&pr)}
h =3D Hash.new(&pr)
h[1][2][3] =3D 6
p h # =3D=3D> {1=3D>{2=3D>{3=3D>6}}}

It's dangerous to go alone. Until we have this sort of divine magic, =20
take this:
h =3D Hash.new { |h, k| h[k] =3D Hash.new(&h.default_proc) }
h[:a][:b][:c] =3D :eek:
h
=3D> {:b=3D>{:c=3D>:eek:}}

--=20
Mikael H=F8ilund
http://hoilund.org/
 
R

Roger Pack

A thought on current_block_yield and then the GC:
It's not exactly the same, but couldn't you just define a
recursive map function?

class Array
def map_r &block
map{|e|Array===e ? e.map_r(&block) : block[e]}
end
end
p [[1,2,[3]],[[[5],6],7],8].map_r{|e|e.to_s}

=> [["1", "2", ["3"]], [[["5"], "6"], "7"], "8"]

Though a little hackish, I wonder if you can receive a named block then
pass that block to itself, a la
def map_r &block
map{|e| block(e, &block) }
end
so that the block could itself call yield. But this would still be a
little harder than being able to say current_block, should it be doable.
Just thinking out loud.

A question on the GC.
So if you try and build a multi-threaded GC [one thread picks up on free
objects, hands them back to the parent thread for use], it turns out
that if you don't free memory, then my current GC does this:

1) you run out of freelist, so you call garbage_collect which spawns a
child thread, and adds to the heap so the parent can continue.
2) when the child thread terminates the heap is now in a larger state
than it was.
3) when it runs out of freelist again, it adds to the heap and spawns
the child thread. The child thread now takes longer to finish than it
did before. Meaning the parent will be adding more to heap while
waiting for it to finish.
4) repeat 3 over and over until you run out of memory.

So currently it doesn't free any memory ever [commented it out]. I'm
hoping that adding that capability will do the trick. If that doesn't
work, though, it seems possible for these two threads to become engaged
in a cycle, if you ever get above a certain threshold then basically it
will just eat up all the memory in the system as it takes longer and
longer to collect. So it exacerbates the problem.

Potential ways to beat this: never add to the heap during a garbage
collect?

Thoughts?
Thanks!
-R
 
Y

Yossef Mendelssohn

Rubbing the lamp....
I wish...

a =3D [1,2,3,4]

a[0..1, 4]

or

a[1,2,3]

worked.

Two things:

1) In my mind, this general thread is somewhat ridiculous. It's been
going on for -- what is it? -- ten months now? It seems to be just a
laundry list for things you want Ruby to do, whether they're sensible
or not, without a lot of thought into them, and at least sometimes
with reasoning like "I'm lazy and don't want to type some brackets".

2) "I wish X worked" is not a very useful way to describe what should
happen. I'm not saying you have to write tests or specs, but do
describe the behavior you expect. Maybe looking at the RubySpec
project will give you an idea of everything Ruby presently "should do"
and what the other implementations are working towards. If that
doesn't get you going, think about it as contributing to Rubinius in
some way, and the sooner that comes out the sooner you can easily
twist even more of Ruby to your desires.

When you're working on describing the behavior, consider this (from an
irb session in Ruby 1.8.6):
arr =3D (1..10).to_a =3D> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
arr[3,4]
=3D> [4, 5, 6, 7]

You seem to want arr[3,4] to return [4,5], and guess what? There's a
way to do that now:
=3D> [4, 5]

Let's say you don't care for Array#values_at and Array#[] is changed
to work as you expect. What will happen when someone who expects the
old behavior tries to use it?
 
R

Roger Pack

Oh, I wasn't understanding your idea, which does seem to make sense now.
...
I'll probably hack it up sometime, first version as a straight patch to
1.8.6

Turns out that just increasing the malloc limit to 80MB or so gives a
nice decrease in GC time[1]--about 80% for me. So that's an easy stop
gap till I get this going. It does tend to use a lot more RAM, though,
unfortunately.

Question about typical VPN's: I assume that typically the bottleneck is
typically RAM and not CPU [ex: on 256MB slice], and they have access to
multiple cores where available, is that right?
Thanks!
-=R
[1] http://github.com/skaes/railsbench/tree/v0.8.3/GCPATCH
 
R

Roger Pack

It throws warnings as deprecated, for some reason.

I don't think so:

shadowfax:~/Documents/terralien/dltsolutions/enrollnow rick$ ruby1.9 -v
ruby 1.9.0 (2007-10-25 patchlevel 0) [i686-darwin8.10.2]
shadowfax:~/Documents/terralien/dltsolutions/enrollnow rick$ ruby1.9
-we '(1..3).to_a'

Ahh I get it. Odd(irb):19: warning: default `to_a' will be obsolete

I was confusing that for
which works as expected. Thanks for helping me figure that out. I have
no idea why 1..6.to_a doesn't raise an exception. Oh well.
Thanks.
-=R
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top