Scala 如何将此理解转化为平面图实现
我试图将此理解转化为平面图/地图实现,但我正在努力解决如何:Scala 如何将此理解转化为平面图实现,scala,Scala,我试图将此理解转化为平面图/地图实现,但我正在努力解决如何: def operationParser: Parser[(Operation, Int, Int)] = { for { n1 <- Parser.natural _ <- Parser.list(Parser.space) op <- Parser.operation _ <- Parser.list(Parser.space) n2 <
def operationParser: Parser[(Operation, Int, Int)] = {
for {
n1 <- Parser.natural
_ <- Parser.list(Parser.space)
op <- Parser.operation
_ <- Parser.list(Parser.space)
n2 <- Parser.natural
} yield (op, n1, n2)
}
def-operationParser:Parser[(操作,Int,Int)]={
为了{
n1根据经验,所有您都可以使用scalac
的-Xprint:parser
命令行选项查看已删除的版本:
源代码
您可以使用以下命令查看可用编译阶段的列表:
$ scalac -Xshow-phases
phase name id description
---------- -- -----------
parser 1 parse source into ASTs, perform simple desugaring
namer 2 resolve names, attach symbols to named trees
packageobjects 3 load package objects
typer 4 the meat and potatoes: type the trees
patmat 5 translate match expressions
superaccessors 6 add super accessors in traits and nested classes
extmethods 7 add extension methods for inline classes
pickler 8 serialize symbol tables
refchecks 9 reference/override checking, translate nested objects
uncurry 10 uncurry, translate function values to anonymous classes
tailcalls 11 replace tail calls by jumps
specialize 12 @specialized-driven class and method specialization
explicitouter 13 this refs to outer pointers
erasure 14 erase types, add interfaces for traits
posterasure 15 clean up erased inline classes
lazyvals 16 allocate bitmaps, translate lazy vals into lazified defs
lambdalift 17 move nested functions to top level
constructors 18 move field definitions into constructors
flatten 19 eliminate inner classes
mixin 20 mixin composition
cleanup 21 platform-specific cleanups, generate reflective calls
delambdafy 22 remove lambdas
icode 23 generate portable intermediate code
jvm 24 generate JVM bytecode
terminal 25 the last phase during a compilation run
object Main {
trait Operation
trait Parser[A] {
def map[B](fn: A => B): Parser[B] = ???
def flatMap[B](fn: A => Parser[B]): Parser[B] = ???
}
object Parser {
def natural: Parser[Int] = ???
def space: Parser[String] = ???
def list[A](p: Parser[A]): Parser[A] = ???
def operation: Parser[Operation] = ???
}
def operationParser: Parser[(Operation, Int, Int)] = {
for {
n1 <- Parser.natural
_ <- Parser.list(Parser.space)
op <- Parser.operation
_ <- Parser.list(Parser.space)
n2 <- Parser.natural
} yield (op, n1, n2)
}
}
$ scalac -Xprint:parser Main.scala
[[syntax trees at end of parser]] // Main.scala
package <empty> {
object Main extends scala.AnyRef {
def <init>() = {
super.<init>();
()
};
abstract trait Operation extends scala.AnyRef;
abstract trait Parser[A] extends scala.AnyRef {
def $init$() = {
()
};
def map[B](fn: _root_.scala.Function1[A, B]): Parser[B] = $qmark$qmark$qmark;
def flatMap[B](fn: _root_.scala.Function1[A, Parser[B]]): Parser[B] = $qmark$qmark$qmark
};
object Parser extends scala.AnyRef {
def <init>() = {
super.<init>();
()
};
def natural: Parser[Int] = $qmark$qmark$qmark;
def space: Parser[String] = $qmark$qmark$qmark;
def list[A](p: Parser[A]): Parser[A] = $qmark$qmark$qmark;
def operation: Parser[Operation] = $qmark$qmark$qmark
};
def operationParser: Parser[scala.Tuple3[Operation, Int, Int]] = Parser.natural.flatMap(((n1) => Parser.list(Parser.space).flatMap(((_) => Parser.operation.flatMap(((op) => Parser.list(Parser.space).flatMap(((_) => Parser.natural.map(((n2) => scala.Tuple3(op, n1, n2)))))))))))
}
}
def operationParser: Parser[scala.Tuple3[Operation, Int, Int]] =
Parser.natural.flatMap(((n1) =>
Parser.list(Parser.space).flatMap(((_) =>
Parser.operation.flatMap(((op) =>
Parser.list(Parser.space).flatMap(((_) =>
Parser.natural.map(((n2) =>
scala.Tuple3(op, n1, n2)))))))))))
$ scalac -Xshow-phases
phase name id description
---------- -- -----------
parser 1 parse source into ASTs, perform simple desugaring
namer 2 resolve names, attach symbols to named trees
packageobjects 3 load package objects
typer 4 the meat and potatoes: type the trees
patmat 5 translate match expressions
superaccessors 6 add super accessors in traits and nested classes
extmethods 7 add extension methods for inline classes
pickler 8 serialize symbol tables
refchecks 9 reference/override checking, translate nested objects
uncurry 10 uncurry, translate function values to anonymous classes
tailcalls 11 replace tail calls by jumps
specialize 12 @specialized-driven class and method specialization
explicitouter 13 this refs to outer pointers
erasure 14 erase types, add interfaces for traits
posterasure 15 clean up erased inline classes
lazyvals 16 allocate bitmaps, translate lazy vals into lazified defs
lambdalift 17 move nested functions to top level
constructors 18 move field definitions into constructors
flatten 19 eliminate inner classes
mixin 20 mixin composition
cleanup 21 platform-specific cleanups, generate reflective calls
delambdafy 22 remove lambdas
icode 23 generate portable intermediate code
jvm 24 generate JVM bytecode
terminal 25 the last phase during a compilation run