Something less imperative than this:
def subs(s: String) = for {start <- 0 to s.length; end <- i to s.length} yield s.substring(start, end)
scala> "asdf".inits.flatMap(_.tails).toList
res2: List[String] = List(asdf, sdf, df, f, "", asd, sd, d, "", as, s, "", a, "", "")
If you want to remove empty strings:
scala> "asdf".inits.flatMap(_.tails.toList.init).toList
res3: List[String] = List(asdf, sdf, df, f, asd, sd, d, as, s, a)
But note that this incantation is not so obvious to the reader, as @Randall Schulz points out in comments. Your original version, on the other hand, is instantly clear.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With