Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

could you share an example of a ioke program with a 5x ratio to ruby code? I can think of many things that could be way less verbose in ruby, but I wonder where from does a a fivefold improvement come.


I fear I am going to do a bad job of explaining this, but here goes:

You'll get the typical Lisp-like terseness and folding that'll make your complex code much smaller. This includes things like currying and passing multiple functions as parameters (I know both of these are possible in Ruby, but it really isn't idiomatic). This one, I think, is easier to understand especially if you've done some work in those types of languages.

The other benefit is more difficult to see at face value. What Ioke does is encourages you to write expressions and to build up your own language. This is a DSL and can be built in Ruby and Lisp and Javascript and many other languages. However, Ioke makes the DSL writing the "Pit of Success" that a developer encounters. It isn't a conscious choice, it is how you write your code. Simplistic example:

    square = lambda { |x| x * x }
    puts (1..10).inject(0) { |sum, x| sum + square.(x) }
In Ioke:

    square = method(x, x * x)
    (1..10) map(x, square(x)) sum println
This is obviously a trivial example that doesn't save any lines. They look very similar except that ruby has a bit more "noise". First, we're using the inject function. I love the inject function but it is a significant amount of noise in the ultimate goal of a readable line of code. Ioke has 4 words that are all significant: map square sum println. They are used once and only once. Ioke's space a function applicator cannot be overlooked, though. It is syntax, yes, but it really encourages "sentence writing" a la Smalltalk. And, finally, Ioke reads left-to-right unlike the C-inspired languages which read inside-out. In fact, if you were to describe this problem in English, you would probably write something similar to:

For the range 1 to 10, square each number and then sum the results.

Ioke maps very well to this.

            (1..10) map(x, square(x))        sum          println
The problem with this trivial example is that this expressiveness is just slightly off for Ruby. But when we grow the program to scale, in Ruby we lean on the classical inheritance-based Object Oriented model that is available in Ruby. In Ioke, we continue to build a DSL.

I hope that makes a little sense?

Edit: Let me also say that I'm madly in love with Ruby and think its a great language :) By no means does "Less Expressive than Ioke" mean "Not Expressive Enough" or "Bad Language"!


But you can do the same map square, then sum in ruby 1.9:

puts (1..10).map{|x| x*x}.inject(:+)

And if you don't like writing inject(:+) all the time, it's pretty easy to monkey patch in a sum method to either array or enumerable.


And Perl6 can also provide an expressive example:

  say [+] (1..10).map: {$_ * $_};


No need to wait for Perl 6:

    my $tot = 0; map { $tot += $_ } map { $_ ** 2 } 1 .. 10; say $tot;
EDIT: Or, regarding List::Util (http://search.cpan.org/~gbarr/Scalar-List-Utils-1.23/lib/Lis...) as part of Perl 5,

    say sum map { $_ ** 2 } 1 .. 10;
EDIT 2: (Golfing is fun!) Or an Applescript-y

    sub the { @_ } sub of { @_ }
    sub squares { map { $_ ** 2 } @_ }

    say the sum of the squares of 1 .. 10;


No need to wait for Perl 6

Absolutely :)

  use autobox::Core;  # or perl5i::2

  [1..10]->map(sub{ $_ ** 2 })->sum->say;
Or if you like a double helping of sugar in your tea!

  use Whatever;

  [1..10]->map( &_ ** 2 )->sum->say;


I thought `Whatever` was a joke, and had to go look it up on CPAN (http://search.cpan.org/~asg/Whatever-0.21/lib/Whatever.pm). Not to belabour a joke, but the examples there suggest that the map should be

    map( $* ** 2 )
not

    map( &_ ** 2 )


No it's quite a serious & well written module. Hopefully it will become part of perl5i - https://metacpan.org/module/perl5i

  &_ is an explicit $_   You use $* or &* when you need to check $_[0] first.
  
  So &_ is more correct with general map but $* (and &*) will work also in 
  this autobox example (in fact i had used it in my first edit but preferred 
  aesthetics of &_ especially with previous usage of $* only being deprecated 
  in 5.10).
NB. Above in code just for aesthetics!


> In Ioke:

    square = method(x, x * x)
    (1..10) map(x, square(x)) sum println
> Ioke has 4 words that are all significant: map square sum println. They are used once and only once.

Sorry to be that guy, but `square` is clearly used twice above. Why not `(1..10) map(x, x*x) sum println`? (I don't have Ioke on this computer, so I can't easily check if this works.) Or are you assuming that `square` would probably have been defined somewhere already?


Not surprising its nearly identical to how you would write it in Io (http://iolanguage.com):

    square := method(x, x * x)
    1 to(10) map(x, square(x)) sum println
and also the variation:

    1 to(10) map(x, x * x) sum println




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: