Gabriella439 / turtle

Shell programming, Haskell style

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Doing regex with turtle

flip111 opened this issue · comments

I have this PHP script i'm trying to convert to haskell to see how scripting works.

<?php

$lines = file('input.txt');

$total = 0;
foreach ($lines as $l) {
  if (preg_match('/(\d+):(\d+)/u', $l, $r)) {
    $total += 60 * $r[1] + $r[2];
  }
}

printf("%d hours\n", $total / 3600);

which regex library goes well with turtle? I have type Shell Line as input and i need two regex groups converted to int. Since i'm already used to PCRE coming from PHP this https://hackage.haskell.org/package/regex-pcre-builtin seems the obvious candidate. But this library works on ByteString, which makes sense because that's what my file is. Using turtle i would get this conversion: ByteString --> Text implicit by turtle and then Text --> ByteString i have to do myself to put it into the regex library. But i would prefer to put lines directly into the regex engine. So what to do now?

For little scripts i use regex all the time, so some advise on this would be very appreciated.

By the way i saw Turtle.Patters but i prefer not to use parser combinators and use regex instead.

@flip111: The idiomatic way to translate your PHP example to a turtle script would be:

{-# LANGUAGE OverloadedStrings #-}

import Control.Applicative (empty)
import Turtle (Pattern, Shell, d, (%))

import qualified Control.Foldl
import qualified Turtle
import qualified Turtle.Line

main :: IO ()
main = do
    let minutes :: Pattern Integer
        minutes = do
            x <- Turtle.decimal
            ":"
            y <- Turtle.decimal
            return (60 * x + y)

    let numbers :: Shell Integer
        numbers = do
            line <- Turtle.input "input.txt"

            let text = Turtle.Line.lineToText line

            case Turtle.match minutes text of
                n:_ -> return n
                _   -> empty

    total <- Turtle.fold numbers Control.Foldl.sum

    Turtle.printf (d%" hours\n") (total `div` 3600)

Thank you for providing the haskell translation. I have to be honest and say that it's too long and too verbose. The time to write it, the time to read back the code .. When it's like this it's not a good alternative to replace the small PHP scripts. Perhaps the haskell code could be written a bit more compact but still the 1 line of regex is just really powerful.