all this latest and greatest...seems like old hat to me

No need to worry about going "off topic" here
Forum rules
This forum is for general chit-chat, not necessarily APL-related. However, it's not for spam or for offensive or illegal comments.

all this latest and greatest...seems like old hat to me

Postby tclviiidyalog on Thu Jun 02, 2011 1:54 pm

from Hacker News pointing to ... r-friends/

10 Scala One Liners to Impress Your Friends
Posted on May 31, 2011

Here are 10 one-liners which show the power of scala, impress your friends and woo women; ok, maybe not. However, these one liners are a good set of examples using functional programming and scala syntax you may not be familiar with. I feel there is no better way to learn than to see real examples.
1. Multiple Each Item in a List by 2

The map function takes each element in the list and applies it to the corresponding function. In this example, we take each element and multiply it by 2. This will return a list of equivalent size, compare to other examples which use reduceLeft and foldLeft those functions return only a single value not a list.
1 (1 to 10) map { _ * 2 }

2. Sum a List of Numbers

The most common example using reduceLeft is summing a list of numbers. This example sums the numbers 1 to 1000 using the range function to to create our lit of numbers and reduceLeft iterates and sum together returning a single value.
1 (1 to 1000).reduceLeft( _ + _ )

3. Verify if Exists in a String

This example returns a boolean if a word in a list exists in a string. I used this example for checking if a tweet contains a word I’m interested in. I suppose technically it is three lines, but the first two are just setting variables.
1 val wordlist = List("scala", "akka", "play framework", "sbt", "typesafe")
2 val tweet = "This is an example tweet talking about scala and sbt."
4 (words.foldLeft(false)( _ || tweet.contains(_) ))

4. Read in a File

This one-liner might only be impressive if you are coming from a Java background, it is pretty common now to be able to read a file in with one line of code. Here are two examples of reading in a file, one reads entire file in to a string, the other reads in each line as an entry in a List.
1 val fileText = io.Source.fromFile("data.txt").mkString
3 val fileLines = io.Source.fromFile("data.txt").getLines.toList

5. Happy Birthday to You!

A common one-liner which prints out the Happy Birthday song. This illustrates scala’s ternary operator as well as combining map and foreach.
1 (1 to 4).map { i => "Happy Birthday " + (if (i == 3) "dear NAME" else "to You") }.foreach { println }

6. Filter list of numbers

Filter a list of numbers into two categories based on a criteria using partition.This example creates two lists of students based on their test scores.
1 val (passed, failed) = List(49, 58, 76, 82, 88, 90) partition ( _ > 60 )

7. Fetch and Parse an XML web service

Since XML is a native structure to scala, parsing an XML feed comes with no effort. Here’s an example fetching the Twitter search feed.
1 val results = XML.load("")

8. Find minimum (or maximum) in a List

Another couple of examples using reduceLeft to iterate through a list and apply a function.
1 List(14, 35, -7, 46, 98).reduceLeft ( _ min _ )
2 List(14, 35, -7, 46, 98).reduceLeft ( _ max _ )

9. Parallel Processing

Scala 2.9 introduced a new collection type called “parallel collections” which utilize multi-core processors when performing bulk operations such as foreach, map, filter, etc… Here’s a video of Aleksandar Prokopec explaining parallel collections at Scala Days 2010.

This example is not quite a copy-and-paste into the REPL, but it illustrates how to use parallel collections. Imagine you had a set of data defined in a list dataList and a function processItem which was very cpu intense. The following one-liner would give you parallel processing over the list.
1 val result = => processItem(line))

10. Sieve of Eratosthenes

Ok, this one isn’t quite practical and technically is not a one-liner since it relies on a operator being previously defined, but it is still pretty darn cool, even if it is unreadable. Daniel Sobral created the Sieve of Eratosthenes which is a algorithm used to determine if a number is prime.
1 (n: Int) => (2 to n) |> (r => r.foldLeft(r.toSet)((ps, x) => if (ps(x)) ps -- (x * x to n by x) else ps))

Requires definition of |> operator, a syntax borrowed from F#. See Steve Gilham’s blog for an example.

So, your turn. Do you have any impressive scala one-liners? Share in the comments.
This entry was posted in scala and tagged examples. Bookmark the permalink.
Posts: 17
Joined: Tue Apr 26, 2011 1:03 pm

Return to Chat

Who is online

Users browsing this forum: No registered users and 1 guest