Alan Jurgensen
Alan Jurgensen

Reputation: 853

Whats a scala-like way to alter first and last in a List

I have a List[String] that com.github.seratch.scalikesolr is populating a little inconveniantly... If List has one element, it looks like this:

[value]

(It has prepended and appended brackets).

If List has more than 1 element, the elements look like:

[value1
value2]

Struggling to find clean looking scala-ish code to remove the brackets if present. what do you suggest. thanks in advance.

P.S. I have several Lists to apply this "filter" to... so reusable code is the better approach.

Upvotes: 4

Views: 255

Answers (5)

Sudheer Aedama
Sudheer Aedama

Reputation: 2144

How about this ?

$ scala
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_65).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :paste
// Entering paste mode (ctrl-D to finish)

def stripBrackets(str: String) = str.replaceAll("\\[", "").replaceAll("\\]", "")

def stripBrackets(ls: List[String]): List[String] =
  ls match {
    case Nil => ls
    case List(head) => List(stripBrackets(head))
    case List(head, last) => List(stripBrackets(head), stripBrackets(last))
    case _ => stripBrackets(ls.head) :: ls.take(ls.size - 1).drop(1) ::: (stripBrackets(ls.last) :: Nil)
  }

val singleElemList = List("[value1]")
val twoElemList = List("[value1", "value2]")
val multiElemList = List("[value1", "value2", "value3", "value4]")

// Exiting paste mode, now interpreting.

stripBrackets: (str: String)String <and> (ls: List[String])List[String]
stripBrackets: (str: String)String <and> (ls: List[String])List[String]
singleElemList: List[String] = List([value1])
twoElemList: List[String] = List([value1, value2])
multiElemList: List[String] = List([value1, value2, value3, value4])

scala> stripBrackets(singleElemList)
res0: List[String] = List(value1)

scala> stripBrackets(twoElemList)
res1: List[String] = List(value1, value2)

scala> stripBrackets(multiElemList)
res2: List[String] = List(value1, value2, value3, value4)

Upvotes: 0

Dave Swartz
Dave Swartz

Reputation: 910

def deBracketize(list: List[String]): List[String] = list.map(_.stripPrefix("[").stripSuffix("]"))

Example usage:

println(deBracketize(List("[value1", "value2]")))
println(deBracketize(List("[value1]")))

Outputs:

List(value1, value2)
List(value1)

Upvotes: 4

Mika&#235;l Mayer
Mika&#235;l Mayer

Reputation: 10681

Here in a functional style using an optional parameter.

def removeBracket(list: List[String], first: Int=1): List[String] =
  list match {
    case Nil => Nil
    case a::Nil => List(a.substring(first,a.length-1))
    case a::q => a.substring(first,a.length)::removeBracket(q,0)
  }

Test:

scala> removeBracket(Nil)
res1: List[String] = Nil
scala> removeBracket(List("[test]"))
res1: List[String] = List("test")
scala> removeBracket(List("[test","tist]"))
res1: List[String] = List("test","tist")
scala> removeBracket(List("[test","toast","tist]"))
res1: List[String] = List("test","toast","tist")

Upvotes: 0

Pasi
Pasi

Reputation: 2654

How about this?

def removeBrackets(list: List[String]): List[String] = list.size match {
  case 0|1 => list.map(_.stripPrefix("[").stripSuffix("]"))
  case _ => (list.head.stripPrefix("[") :: list.drop(1).dropRight(1)) :+ list.last.stripSuffix("]")
}

Upvotes: 1

egprentice
egprentice

Reputation: 824

List("[value1", "value2]").map{_.replace("[","").replace("]","")}

Upvotes: -1

Related Questions