Skip to content

Instantly share code, notes, and snippets.

@mkaz
Last active February 15, 2024 10:56
Show Gist options
  • Save mkaz/d11f8f08719d6d27bab5 to your computer and use it in GitHub Desktop.
Save mkaz/d11f8f08719d6d27bab5 to your computer and use it in GitHub Desktop.

10 Scala One Liners to Impress Your Friends

Here are 10 one-liners which show the power of scala programming, 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.

Updated: June 17, 2011 - I'm amazed at the popularity of this post, glad everyone enjoyed it and to see it duplicated across so many languages. I've included some of the suggestions to shorten up some of my scala examples. Some I intentionally left longer as a way for explaining / understanding what the functions were doing, not necessarily to produce the shortest possible code; so I'll include both.

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 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 create our list of numbers and reduceLeft iterates and sum together returning a single value. Added simpler example using built-in sum function.

(1 to 1000).reduceLeft( _ + _ )
(1 to 1000).sum

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.

val wordList = List("scala", "akka", "play framework", "sbt", "typesafe")
val tweet = "This is an example tweet talking about scala and sbt."

(wordList.foldLeft(false)( _ || tweet.contains(_) ))
wordList.exists(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.

val fileText = io.Source.fromFile("data.txt").mkString

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 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.

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.

val results = XML.load("http://search.twitter.com/search.atom?&q=scala")

8. Find minimum (or maximum) in a List

Another couple of examples using reduceLeft to iterate through a list and apply a function. Added simpler examples of the method min/max on the list.

List(14, 35, -7, 46, 98).reduceLeft ( _ min _ )
List(14, 35, -7, 46, 98).min

List(14, 35, -7, 46, 98).reduceLeft ( _ max _ )
List(14, 35, -7, 46, 98).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.

val result = dataList.par.map( line => 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.

(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.

@GabeChurch
Copy link

I like it! Thanks for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment