String range in Scala

scala

Solution

For this example you could do (scala 2.10)

val atoz = 'a' to 'z'
for {c1 <- atoz if c1 <= 'b'; c2 <- atoz if (c1 == 'a' || (c1 == 'b' && c2 < 'c'))} yield s"$c1$c2"

Edited as per comment, thanks (but getting a bit ugly!)

Problem

In Ruby we can do this: ``` $ irb >> ("aa".."bb").map { |x| x } => ["aa", "ab", "ac", "ad", "ae", "af", "ag", "ah", "ai", "aj", "ak", "al", "am", "an", "ao", "ap", "aq", "ar", "as", "at", "au", "av", "aw", "ax", "ay", "az", "ba", "bb"] ``` In Scala if I try the same I get error: ``` $ scala Welcome to Scala version 2.9.1 (OpenJDK 64-Bit Server VM, Java 1.7.0_51). scala> ("aa" to "bb").map(x => x) <console>:8: error: value to is not a member of java.lang.String ("aa" to "bb").map(x => x) ^ ``` How do get a range of Strings in Scala ?

Original source