gpt4 book ai didi

scala - 如何将此理解转化为 flatMap 实现

转载 作者:行者123 更新时间:2023-12-01 11:36:21 25 4
gpt4 key购买 nike

我试图将此理解转化为 flatMap/map 实现,但我正在努力弄清楚如何:

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 命令行选项来查看脱糖版本:

源代码

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

关于scala - 如何将此理解转化为 flatMap 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26778946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com