How to turn this into a flatMap implementation for understanding

I tried to turn this into a flatMap / map implementation for understanding, but am struggling to figure out how:

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

      

+3


source to share


2 answers


You can use the command line option -Xprint:parser

for scalac

to see the revoked version:

Source

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

      

Compiler command and output

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

      



After some formatting, the desugared version looks like this:

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

      

You can view the list of available compilation phases with this command:

$ 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

      

+4


source


As a rule, everyone <-

becomes flatMap

except for the last one (in your case n2 < -Parser.natural

), which becomes map

. But you can "cheat" and force the compiler to do it for you by compiling with:

 scalac -Xprint:parser YourClass.sca

      



It will show the de-sugar compiler to understand.

+1


source







All Articles