Hacker News new | ask | show | jobs
by ansin 6281 days ago
"I don't see where Ruby syntax is particularly more concise than C# 3's."

1.upto(10).each{|x| print x}

I'm curious - how would you print 1 through 10 in C# 3 by comparison?

5 comments

C# 3:

  (1).UpTo(10).ForEach(x => Console.WriteLine(x));
Of course, "UpTo" requires first extending int, which is trivial:

  public static class IntExtensions {
      public static List<int> UpTo(this int start, int end) {
          var range = new List<int>();
          for(int i = start; i < end; i++) {
              range.Add(i);
          }
          return range;
      }
  }
That's cool and all, but I think being able to do this natively in Ruby wins:

  p *1..10
Edit: you may need to do a "require 'pp'" first.
One way to do this in Scala:

  def print[T] (value:T) = System.out.println(value)
  1.until(10).foreach(x => print x)
This is a fairly uninteresting example, however. Something more interesting would be, for instance, the use of structural types to implement type-checked duck typing.

Scala example:

  // Declare a structural type for any class implementing close()
  type Closable = {def close(): Any}
  
  // Executes the provided function with the given
  // closable generator, creating a new instance which
  // will then be closed on completion. The provided
  // function's value will be returned.
  def withClosable[T, C <: Closable] (c: => C) (f: (C) => T) = {
    val closable = c
    try {
      f(closable)
    } finally {
      closable.close
    }
  }
  
  // Example usage
  def usage = {
    val updated = withClosable(db.openConnection) { conn =>
      conn.update("INSERT INTO example VALUES (...")
    }
    System.out.println("Rows updated: " + updated)
  }
This could be compared with Python's new 'with' syntax.
Since there's plenty of shitting on Scala going on, I'd like to point out that the Scala equivalent of that is:

    1 to 10 foreach println
Which seems more concise than the equivalent Ruby to me. Feh.
in Python:

    for i in range(1,11): print i
Python FTW? ;)
I still find this clearer:

  for x in range(1, 10): print x
I'm nitpicking, but your range should be range(1,11).
Every nit picked is valuable :)
Not so different in Ruby:

for x in 1..10; print x end

Yes, but we all know that Ruby is the haven of Devils!!
Ad hominem.
Ad...lingua?