5 ms·
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 wher
by riffraff 15y ago
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.
- hammerdr 15y agoI 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"!
- rauljara 15y agoBut 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.
- draegtun 15y agoAnd Perl6 can also provide an expressive example: say [+] (1..10).map: {$_ * $_};
- JadeNB 15y agoNo 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/List/Util.pm 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;
- draegtun 15y agoNo 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;
- JadeNB 15y agoI 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 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 )