What is a scala-like way to change the first and last in a list

I have a List [String] that com.github.seratch.scalikesolr fills a bit inconveniently ... If the List has one element, it looks like this:

[value]

(It has pre-copied and attached brackets).

If the List has more than 1 item, the items look like this:

[value1
value2]

Strive to find the clean look of scala -ish code to remove parentheses, if any. What do you suggest. thanks in advance.

PS I have several lists to apply this "filter" to ... so reusable code is the best approach.

+4
source share
5 answers
def deBracketize(list: List[String]): List[String] = list.map(_.stripPrefix("[").stripSuffix("]"))

Usage example:

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

Outputs:

List(value1, value2)
List(value1)
+4
source

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("]")
}
+1

?

$ 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)
0

.

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)
  }

:

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")
0

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

-1
source

Source: https://habr.com/ru/post/1533213/


All Articles