2014-03-24 2 views
4

com.github.seratch.scalikesolr이 조금 불만족스러워하는 목록 [String]이 있습니다 ... List에 요소가 하나있는 경우 이 :목록에서 첫 번째와 마지막 부분을 바꾸는 스칼라 식의 방법

[value] 

(앞에 괄호가 붙어 있습니다.) 있는 경우 괄호를 제거하는 청소 찾고 스칼라 틱 코드를 ​​찾기 위해 고군분투

[value1 
value2] 

: 목록 1 개 이상의 요소가있는 경우처럼

는 요소 본다. 무엇을 권하고 싶습니다. 미리 감사드립니다.

P. 이 "필터"를 적용 할 여러 목록이 있습니다. 재사용 가능한 코드가 더 나은 방법입니다.

+0

이 결과'목록 ("[값 1의 값 2가"]) '또는 'List ("[value1", "value2]")? – flavian

+0

죄송합니다. 목록은 다음과 같습니다 : List ("[value1", "value2]") –

+0

'value' 그 자체에 브래킷이 포함될 수 있습니까? 그렇지 않다면 가장 간단한 해결책은 괄호를 빈 문자열로 바꾸는 것입니다. –

답변

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

사용 예제 :

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

출력 :

List(value1, value2) 
List(value1) 
+0

우승자는 간결함으로 인해 –

-1

목록 ("[값 1", "값 2]").지도 {_가. "[" ". 대체 ("() "] 대체", "")}이 약

1

어떻게?

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

목록 크기가 0이면 모든 조치를 취하고 빈 목록을 반환하는 이유는 무엇입니까? –

0

어때요?

$ 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") 
관련 문제