Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

There is one construct for repetition in Go. It's called "for".

    parser = ParserNil;
    for i := 0; i < 10; i++ {
      parser = ParserSeq(parser, ParserSeq(parseComma, parseInt))
    }
PS. I'm a complete Go newbie. Don't take this code as the "one true Go way".


So you're forced to duplicate the monadic combinators (e.g: inlined replicateM here).

Now let's consider:

  myParser = do
    logDate <- date
    char ':'
    logTime <- time
    let fullTime = fromDateTime logDate logTime
    msg <-
      if fullTime < newLogFmtEpoch
      then do
        str <- parseString
        return (toLogMsg str)
      else do
        idx <- parseLogIndex
        getLogParser idx
Translating this to bare-bones Go would require using explicit continuations everywhere. Any combinator you use from Control.Monad is going to be duplicated/inlined in your Go code, violating DRY repeatedly.


> forced to duplicate the monadic combinators (e.g: inlined replicateM here).

??? This is a bare bones for loop. What exactly do you consider "duplication" ???

Regrettably, I can't accept the challenge because I have no idea what the code you pasted is doing and why.


A bare bones for loop is the implementation of replicateM. There are more complex combinators than replicateM, e.g: filterM, zipWithM, and more... Which you would need more than duplicating a bare bones loop at every occurence.

The code I pasted above uses "do" notation to write a monadic parser. The parser parses the format started by a <date>:<time> and if those set a date larger than some point in the past, it parses the continuation of the text differently. Which part of the code are you having difficulty understanding?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: