4 ms·
Quick little transliteration to ruby: 1. (1 to 10) map { _ * 2 } 1.upto(10).map { |x| x * 2 } 2. (1 to 1000).reduceLeft( _ + _ ) 1.upto(1000).in
by aphyr 15y ago
Quick little transliteration to ruby:
1. (1 to 10) map { _ * 2 }
1.upto(10).map { |x| x * 2 }
2. (1 to 1000).reduceLeft( _ + _ )
1.upto(1000).inject { |a, x| a + x }
3. val wordlist = List("scala", "akka", "play framework", "sbt", "typesafe")
wordlist = ["scala", "akka", "play framework", "sbt", "typesafe"]
val tweet = "This is an example tweet talking about scala and sbt."
tweet = "This is an example tweet talking about scala and sbt"
(words.foldLeft(false)( _ || tweet.contains(_) ))
wordlist.any? { |w| tweet[w] }
4. val FileText = io.Source.fromFile("data.txt").mkString
file_text = File.read 'data.txt'
val fileLines = io.Source.fromFile("data.txt").getLines.toList
file_lines = File.read('data.txt').lines
Or, more typically,
File.open('data.txt').each { |line| ... }
5. (1 to 4).map { i => "Happy Birthday " + (if (i == 3) "dear NAME" else "to You") }.foreach { println }
4.times { |i| puts "Happy Birthday #{i == 2 ? "dear NAME" : "to You"}" }
6. val (passed, failed) = List(49, 58, 76, 82, 88, 90) partition ( _ > 60 )
passed, failed = [49, 58, 76, 82, 88, 90].partition { |x| x > 60 }
7. val results = XML.load("http://search.twitter.com/search.atom?&q=scala")
results = Nokogiri::XML open 'http://search.twitter.com/search.atom?&q=scala'
8. List(14, 35, -7, 46, 98).reduceLeft ( _ min _ )
[14, 35, -7, 46, 98].min
[14, 35, -7, 46, 98].inject { |m, i| i > m ? i : m }
I'm impressed! Scala's lambda syntax is obviously bar none. The dual _ + _ was especially pleasing, and completely unexpected. The List() construct is also entirely warranted in place of array literals, given Scala's wide variety of collection types. On the other hand, what is this io.Source.fromFile mess? I don't understand the alternating capitalization, or the need for deep namespacing of an incredibly common type.
- msmith 15y agoIt's calling the fromFile method on the io.Source object. Package names are lower-case by convention, class and object names are capitalized camel-case, and methods are lower-case camel-case. Also, 1.upto(10) can also be written as (1..10) in ruby
- carey 15y agoBy the way, it is possible to have methods and values on the package itself, like math.sin(math.Pi), but that hasn't been done with scala.io. You can also import singleton methods, so if you write import io.Source._ then all the fromX methods are available directly.
- skimbrel 15y agoOkay, here it is in Perl, the granddaddy of them all when it comes to implicit variables: 1. map { $_ * 2 } (1..10) 2. reduce { $a + $b } (1..1000) 3. scalar grep { /(scala)|(akka)|(play framework)|(sbt)|(typesafe)/ } "This is an example tweet talking about scala and sbt." # Pow, easy regex syntax! 4. open(my $fh, '<', "data.txt"); $/ = undef; my $text = <$fh>; open(my $fh, '<', "data.txt"); my @lines = <$fh>; 5. map { $_ => "Happy Birthday " . (i == 3 ? "dear NAME" : "to you") } (1..4) 6. @grades = (49, 58, 76, 82, 88, 90); @passed = grep { $_ > 60 } @grades; @failed = grep { $_ < 60 } @grades; # Sort of gross, might also do this: my (@passed, @failed); map { $_ > 60 ? push @passed, $_ : push @failed, $_ } (49, 58, 76, 82, 88, 90); 7. # I'm not even going to try, because XML::Parser is disgusting. I'll just point out that you'd probably have the same amount of verbosity and disgustingness telling Scala (or Ruby) to parse $arbitrary_data_format, so XML being a one-liner really isn't a huge win. 8. min(14, 35, -7, 46, 98) max(14, 35, -7, 46, 98) Note: reduce(), min(), and max() are in List::Util, but that's in perl5 core, so they count.
- paulitex 15y ago> On the other hand, what is this io.Source.fromFile mess? I don't understand the alternating capitalization, or the need for deep namespacing of an incredibly common type Not at all necessary, just convenient for demos since it saves you an import. "io" : The package is scala.io (the scala part is already imported by default). Following java, package names are always lower case. "Source": There is a singleton object in the io package called 'Source'. Again, like java, classes and constants (including singleton objects) are capitalized. "fromFile": A method on the Source singleton. Methods are camelCase. In 'real' scala code the line would look like: import io.Source (...) val fileText = Source.fromFile("data.txt").mkString or, even more concisely import io.Source.fromFile (...) val fileText = fromFile("data.txt").mkString Since methods are just functions (a first class data type), there's no problem importing methods directly. (edit: I also note that you didn't include all your imports, such as nokogiri, so it's not entirely a level playing filed)
- ostso 15y ago"bar none" is a little excessive -- Haskell's syntax is probably even cleaner. Compare: 1. map (*2) [1..10] 2. sum [1..1000] -- sum = foldl (+) 0 3. any (`isInfixOf` tweet) wordlist 4. fileText <- readFile "data.txt" fileLines <- lines <$> readFile "data.txt" 5. mapM_ putStrLn . map (\i -> "Happy Birthday " ++ if i == 3 then "dear NAME" else "to You") $ [1..4] 6. (passed, failed) = partition (>60) [49,58,76,82,88,90] 7. -- I avoid XML so I don't know what library you'd use here. 8. minimum [14, 35, -7, 46, 98] -- minimum = foldl1 min
- draegtun 15y ago"bar none" is a little excessive -- Haskell's syntax is probably even cleaner. Compare And ditto for Io (http://www.iolanguage.com http://www.iolanguage.com): 1. 1 to(10) map(*2) 2. 1 to(1000) asList sum 3. tweet findSeqs( wordlist ) 4. fileText := File with("data.txt") open readLines join fileLines := File with("data.txt") open readLines 5. 4 repeat (i, writeln("Happy Birthday " .. if(i == 2, "dear NAME", "to you"))) 6. list(49, 58, 76, 82, 88, 90) partition(x, x > 60) 7. results := SGML URL with("http://search.twitter.com/search.atom?&q=scala") fetch asXML 8. list(14, 35, -7, 46, 98) min list(14, 35, -7, 46, 98) max Some notes: i) For 1 & 2 remember to have `Range` loaded first. ii). OK I cheated on 6 because there is no partition currently in the Io core lib. Here is a "simple" way I did it for the example: List partition := method ( returnThis := Object clone do ( passed := list() failed := list() ) argSlot := call message argAt(0) cond := call message argAt(1) self foreach (n, newSlot(argSlot asString, n) if (doMessage(cond), returnThis passed append(n), returnThis failed append(n)) ) returnThis )
- bobfunk 15y agoA different Ruby take on 2. that compares well to the _ + _: (1..10).inject(:+)
- aphyr 15y agoOh... oh my. I never realized that higher arity blocks worked with that syntax. Thanks for that one.
- ricardo_ 15y agolooks like I'm late to the party.. I rewrote most of them in CoffeeScript: http://ricardo.cc/2011/06/02/10-CoffeeScript-One-Liners-to-Impress-Your-Friends.html http://ricardo.cc/2011/06/02/10-CoffeeScript-One-Liners-to-I... I'm specially fond of "Happy Birthday #{if i is 3 then "dear Robert" else "to You"}" for i in [1..4]
- DanielRibeiro 15y agoThere this gem that brings the underscore syntax to Ruby: http://bit.ly/a6QmgL http://bit.ly/a6QmgL