Ruby like syntactic sugar

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • F Jamitzky

    Ruby like syntactic sugar

    It is rather easy to define functions in python that mimic the special
    ruby syntactic sugar like:

    5.times { print "Hello World!" }

    or

    [toast, cheese, wine].each { |food| eat food }

    In python these fragments can be written as:

    5 *times(lambda: printf("Hello World!") )

    or

    [toast, cheese, wine] *each (lambda food: eat(food) )

    by defining a Times class like that:

    class Times:
    def __rmul__(self,n ):
    for i in range(n):
    self.func()
    def __call__(self,f unc):
    self.func=func
    return self
    times=Times()

    Was this intended as a language feature or is this style an
    "unpythonic "
    missuse of the syntax ? I like the way of writing loops and list
    comprehensions that way, but I think there will be an outcry of some
    people. What do you guys think about extensions like that ?

    cheers
    Ferdinand
  • Riccardo Galli

    #2
    Re: Ruby like syntactic sugar

    On Tue, 02 Mar 2004 13:26:17 -0800, F Jamitzky wrote:

    [snip][color=blue]
    > In python these fragments can be written as:
    >
    > 5 *times(lambda: printf("Hello World!") )[/color]
    [...]

    printf ? a bit c-style, isn't it?
    may be did you want to write sys.stdout.writ e instead?

    Ciao,
    Riccardo


    --
    Riccardo Galli
    Sideralis Programs

    Comment

    • A.M. Kuchling

      #3
      Re: Ruby like syntactic sugar

      On 2 Mar 2004 13:26:17 -0800,
      F Jamitzky <fxj@mpe.mpg.de > wrote:[color=blue]
      > 5 *times(lambda: printf("Hello World!") )
      > or
      > [toast, cheese, wine] *each (lambda food: eat(food) )[/color]

      These formulations are no improvement over the obvious code:

      for i in range(5):
      print 'Hello world!'

      or

      for food in [toast, cheese, wine]:
      eat(food)

      It's certainly possible to use Python language features to get Ruby-like
      code, but this code isn't as clear, so why bother?

      --amk

      Comment

      • Wayne Folta

        #4
        Re: Ruby like syntactic sugar

        It seems your solution could be the basis for rubyesque Python. (Do you
        have it backwards regarding Ruby, though? Isn't 5.times the way a loop
        is done and the for-loop-looking construct is the syntactic sugar?)

        But on a personal note, I'd add that the
        [color=blue]
        > 5.times { print "Hello World!" }[/color]

        syntax is the idiom that persuaded me Ruby is not for me. In my
        opinion, this construct is either too clever by half or it's too
        Smalltalkish (everything must be classed). It makes some sense when
        spoken, but I fail to see how a looping method belongs to the class of
        numbers, so it troubles me too much to use or to enjoy a language where
        this is natural.


        Comment

        • David MacQuigg

          #5
          Re: Ruby like syntactic sugar

          On 2 Mar 2004 13:26:17 -0800, fxj@mpe.mpg.de (F Jamitzky) wrote:
          [color=blue]
          >It is rather easy to define functions in python that mimic the special
          >ruby syntactic sugar like:
          >
          >5.times { print "Hello World!" }
          >
          >or
          >
          >[toast, cheese, wine].each { |food| eat food }
          >
          >In python these fragments can be written as:
          >
          >5 *times(lambda: printf("Hello World!") )[/color]

          What is wrong with:
          for n in range(5): print("Hello World!")
          It doesn't require definition of a new class, and it uses a syntax
          everyone past Chapter 1 in Python will find familiar.

          There are a few things in Ruby I would like to see in Python. See the
          Working with Strings example at
          http://userlinux.com/cgi-bin/wiki.pl?RubyPython but in general, I'm
          finding the "syntactic sugar" of Ruby is almost all personal
          preference ( -1942.abs vs abs(-1942) ). I have yet to see an example
          of Ruby code blocks that is fundamentally better than the equivalent
          in Python.

          -- Dave
          [color=blue]
          >or
          >
          >[toast, cheese, wine] *each (lambda food: eat(food) )
          >
          >by defining a Times class like that:
          >
          >class Times:
          > def __rmul__(self,n ):
          > for i in range(n):
          > self.func()
          > def __call__(self,f unc):
          > self.func=func
          > return self
          >times=Times( )
          >
          >Was this intended as a language feature or is this style an
          >"unpythonic "
          >missuse of the syntax ? I like the way of writing loops and list
          >comprehensio ns that way, but I think there will be an outcry of some
          >people. What do you guys think about extensions like that ?
          >
          >cheers
          >Ferdinand[/color]

          Comment

          • Terry Reedy

            #6
            Re: Ruby like syntactic sugar


            "F Jamitzky" <fxj@mpe.mpg.de > wrote in message
            news:5349af3f.0 403021326.ed19d 8c@posting.goog le.com...[color=blue]
            > Was this intended as a language feature or is this style an
            > "unpythonic "[/color]

            It is intentional that Python give you the flexibility to do things the
            developers never imagined and that most users think a bit wierd or 'over
            the top'. Playing with things like this is one way to learn, and probably
            more fun than some.

            Terry J. Reedy




            Comment

            Working...