Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Python vs Ruby holy war thread

Name: Christy McJesus !DcbLlAZi7U 2005-04-12 13:11

Last week I learned Python. It's not perfect but it's pretty damn cool.
This week I'm starting on Ruby. I've just encountered blocks and I would approve if someone would answer this: are blocks merely an ugly kludge because the author did not know about lambda expressions, or can they do something lambdas can't?

Name: Anonymous 2005-04-12 15:54

for this to answer truthfully, i first need to know what python lambdas can do ruby blocks can't.

Name: Anonymous 2005-04-12 16:50

>>2
then don't answer

Name: Christy McJesus !DcbLlAZi7U 2005-04-12 18:26

>>2

Lambda expressions implement Church's lambda calculus. As with any good feature they're ripped straight out of Lisp. From what I can tell blocks seem to produce a similar result only more ugly.

After I posted this blatant attempt to produce drama, I checked around on that ever useful resource: Google. Seems a bunch of people I never heard of agree with me.

Name: Anonymous 2005-04-12 19:25

I doubt that blocks can do more than lambda expressions. If they could do more, no turning machine could implement them.

Name: Anonymous 2005-04-13 4:23

I don't like the |name| notation for block arguments. It looks somewhat like an absolute value but has no place there. :(

Name: Christy McJesus !DcbLlAZi7U 2005-04-13 5:18

>>6
That's what got me. The whole idea of passing arguments into blocks seems semantically weird to me; the fact that it needs it's own special syntax should set off warning bells.

Actually this morning I thought of one way the two are different: lambda expressions may only be composed of expressions. In blocks you're allowed statements.

Not techically a limitation if you're coding in a clean, functional style, but I can see how it could be inconvenient. However if you do need to pass a more complicated function you can still do it the "normal" way by just giving it a name.

Next question: you use the "yield" statement to execute a block in ruby. Does this mean you can't have more than one block as a parameter to a ruby function? If so that's a limitation right there.

Name: Anonymous 2005-04-13 7:42

It looks somewhat like an absolute value but has no place there
the notation comes from smalltalk, where |variable| denotes a local variable afaik.

Does this mean you can't have more than one block as a parameter to a ruby function?
seems like it, though i haven't bothered much with research. i consider blocks syntactic sugar:

open "file" do |file_obj|
puts file_obj.read
end
instead of, say,
open("file", proc {|f| puts f.read})
or something. of course, there may be more to it but if you really want to find out, ask on the ruby-ml or search the archives: http://blade.nagaokaut.ac.jp/ruby/ruby-talk/index.shtml
ruby/tk objects for example evaluate blocks in the instance's context which i found fairly convenient.
lf = TkLabelFrame.new(top_frame) do text "labelframe" end

if you *do* want to pass more than that, you can still use lambdas. they're called "proc" (or it's synonym "lambda") in ruby-speak.
blafunc = proc {|x,y,z| puts x,y,z; puts "hello"} # { ... } can be replaced with do ... end
blafunc.call "x", "y", "z"
process_file(file, proc {|f| somecode()}, another_proc_obj)
essentially the same as in python/lisp i assume.

Name: Anonymous 2005-04-13 11:53

Python has 2000 attack 1000 defense, but ruby has 1000 attack 1200 defense and Ruby Beam Erementar Flush!  Combine this with Shining White Ultimate Combine and you will counter anything! 

Name: Christy McJesus !DcbLlAZi7U 2005-04-13 11:59

>>9
Gotcha, thanks.

Name: Anonymous 2005-04-17 1:39

new python version is getting rid of lambda in favour of nested functions.

... lol

Name: Christy McJesus !DcbLlAZi7U 2005-04-17 14:35

>>11

Yeah I noticed that. Guido is a fucking fucker.

I've also been finding Python's class system is really annoying, especially when compared to Ruby's. I'm starting to feel Ruby is gonna be the way to go for me, especially with all the commotion Rails is causing.>>11

Name: Anonymous 2005-04-17 16:32

Just try this:
Python: [ x**2 for x in xrange(100000) if x % 2 == 0]
Ruby: (1..100_000).select { |x| x % 2 == 0 }.map { |x| x**2 }

RUBY WINS! Python is ten times slower!

Name: Anonymous 2005-04-17 16:36

Seriously, you can store blocks in variables and give multiple blocks to a function. There is no real difference with lambda functions in functionnal programming, I guess the creator just wanted to be original or try a different syntax.

>>8
lambda, procs and Blocks are the same thing in Ruby!

Name: Anonymous 2005-04-18 1:26

lambda functions are anonymous functions that are restricted to one line. what the fuck is so great about that?

Name: Christy McJesus !DcbLlAZi7U 2005-04-18 8:49

>>15
Please learn Lisp.

>>14
I know this, it just seems weird, and not in a good way. It's like matz was too object obsessed to allow for functions, as opposed to methods, and invented the idea of blocks to serve the exact same purpose except with a weird syntax and they're not called functions. Wrapping a block in a lambda/proc lets you call it just like a function, so why not make it a function in the first place?

Seriously it's just a little niggle, but you notice such things more when the rest of the language is so well designed.

Name: Anonymous 2005-04-19 10:06

>>16
Blocks were copied from Smalltalk. Ruby is pretty much a mix of Smalltalk and Perl.

Name: Christy McJesus !DcbLlAZi7U 2005-04-19 17:42

>>17
o rly.
I knew the object system is lifted straight from smalltalk, and I'm pretty impressed with it to be honest.

Name: Anonymous 2005-05-10 8:07

Please learn Lisp.

You can't compare Python's lambda to Lisp's becasue there are no let blocks. So assignments are pretty much impossible, which restricts lambda blocks a lot.

Name: Christy McJesus !DcbLlAZi7U 2005-05-10 12:00

>>19
Not forgetting that Python relies on statements instead of making everything an expression, which is hardly a difficult feat. Yes you're right, Python's lambdas are pretty much crippled. But the lambda calculus, when implemented properly in a real language, is very powerful. Which is why I suggested that the good sir >>15 should learn lisp :-)

Name: Anonymous 2005-05-14 6:10

apparently python wants blocks now?
http://www.python.org/peps/pep-0340.html

Name: Christy McJesus !DcbLlAZi7U 2005-05-14 11:24

aw nuts

Name: EYERYS 2005-05-27 10:36

>>11
Guido is a fucker in this respect, there'll be a lot of outcry if Guido actually removes lambdas. So I don't think he'll do it. Lambdas were a compromise in the first place and Guido doesn't like them so he wants 'em gone.

>>19
lambda is all you need!

This does "assignment":

rsplit = lambda S, I=' ', C=-1: (lambda L: len(L) == 1 and L or L.extend(map(lambda L: L.pop(), (L,)*(len(L)-1))) or L)((lambda L: L.insert(0, I.join(L.pop(0))) or L)((lambda L: L[0].reverse() or L)((lambda L: L.insert(0, L.pop(0).split(I)) or L)((lambda L: L.insert(0, L.pop()) or L)(I.join((lambda L: L.reverse() or L)(list(S.split(I)))).split(I, C))))))

See http://en.wikipedia.org/wiki/Lambda_calculus

>>21
notice that this pep was rejected

Name: Christy McJesus !DcbLlAZi7U 2005-05-29 14:15

So, anyone know how to subclass Integer in Ruby?

Name: Anonymous 2005-06-04 11:03

So, anyone know how to write maintainable code? :P

Name: Christy McJesus !DcbLlAZi7U 2005-06-04 12:11

>>25
Only in Perl.

lol.

Name: Anonymous 2005-06-04 18:05

why sublass it :O?

Name: Anonymous 2005-06-05 10:59

>>27

Because it's kewl and witty

Name: Christy McJesus !DcbLlAZi7U 2005-06-05 11:08

Because I should be able to. I'm told ZOMG EVERYTHING IS AN OBJECT YOU CAN SUBCLASS EVERYTHING EVEN BUILT IN TYPES but when I try to subclass Integer it doen't work :(

Name: Anonymous 2005-06-06 6:20

You need to make Integer WANT to be subclassed.

Name: Anonymous 2005-06-06 19:19

>>30 Dressed like that? It's pretty clear what Integer WANTS. ;)

Name: Anonymous 2005-06-08 21:34

It should be a bug then

Integers are not treated the same way as other objects. A normal object is reference to a structure or something, but integers are not.

Name: Anonymous 2005-06-09 4:27

>>32
I don't disagree if that's what the language specification says, but... Why on holy Earth do you want to make an integer a reference to a structure!?!?

No wonder why crapware is that slow.

Name: Christy McJesus !DcbLlAZi7U 2005-06-09 7:51

>>32
That's true, but the language semantics are supposed to hide the fact that numbers are done at machine level.

Name: Anonymous 2005-06-09 12:04

>>34
They could make a special option, --with-real-integers, which would wrap all basic types so you can subclass them, at the cost of 15% slower programs. I personally wouldn't use it.

Name: Christy McJesus !DcbLlAZi7U 2005-06-09 14:32

>>35

What do you mean? We're not talking about Java btw. Ruby Integers are objects, with methods. Which is why I want subclassage :(

Name: Anonymous 2005-06-09 14:49

I think there are 2 types of integers. Smallints and Bigints or something. Don't remember the names. You could probably subclass bigints.

Name: Christy McJesus !DcbLlAZi7U 2005-06-09 15:46

There's Fixnum and Bignum, and Integer switches between them as necessary, for great justice.

Name: Anonymous 2005-06-09 20:43

>>36
Oh, I see. I knew we were talking about Ruby, which I don't know, but you said that so I assumed it had to be like Java.

Name: Anonymous 2005-06-09 23:58

>>38
Are you sure that there is an Integer class at all?

I don't have Ruby with me now, but I don't think you can wrap up Fixnum and Bignum in one Integer class.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List