2013-11-26 67 views
3

Scala的源解釋了這些運營商:的〜>和實施<〜解析器組合運營商

~> is a parser combinator for sequential composition which keeps only the right result.

<~ is a parser combinator for sequential composition which keeps only the left result

我寫了下面兩班。請注意,關於此主題的Daniel Spiewak出色的article非常幫助我開始理解Parser Combinators。

~>

class KeepRightParser[+A](left: =>Parser[A], 
         right: =>Parser[A]) extends Parser[A] { 
    def apply(s: Stream[Character]) = left(s) match { 
     case Success(_, rem) => right(rem) 
     case f: Failure => f 
    } 
} 

<~

class KeepLeftParser[+A](left: =>Parser[A], 
         right: =>Parser[A]) extends Parser[A] { 
    def apply(s: Stream[Character]) = left(s) match { 
     case Success(a, rem) => right(rem) match { 
      case Success(_, _) => Success(a, rem) 
      case f: Failure => f 
     } 
     case f: Failure => f 
    } 
} 

這裏的測試:

val s4 = Stream[Character]('f', 'o', 'o', 'b', 'a', 'r', 'b', 'u', 'z', 'z') 
val krp = new KeepRightParser("foo", "bar") 
println("~> test: " + krp(s4)) 

val klp = new KeepLeftParser("foo", "bar") 
println("<~ test: " + klp(s4)) 

與輸出:

~> test: Success(bar,Stream(b, ?)) 
<~ test: Success(foo,Stream(b, a, r, b, ?)) 

據我所知,第二個流顯示比更多,因爲需要讀取bar來解析序列的後半部分。

這是正確的嗎?

回答

1

是的你是對的。 StreamtoString不是不可變的,因爲您可以在REPL中驗證:

scala> val s = Stream(1,2,3,4,5,6,7,8) 
s: scala.collection.immutable.Stream[Int] = Stream(1, ?) 

scala> s(3) 
res5: Int = 4 

scala> s 
res6: scala.collection.immutable.Stream[Int] = Stream(1, 2, 3, 4, ?) 
+0

謝謝。路易吉,不是「不變的」,因爲它的結構**不能改變?但由於調用's.toString'不會總是輸出相同的結果,所以它不是'引用透明'? –

+0

只要你不認爲它的'toString'是核心功能,Stream本身就是不可變的,我不會這樣做。我認爲你在技術上是正確的,「參考透明度」是一個函數是否對給定的輸入返回相同的東西的正確術語,而「不變性」是數據結構/對象的一個​​屬性。 –