Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doing regex with turtle #341

Open
flip111 opened this issue Dec 24, 2018 · 3 comments
Open

Doing regex with turtle #341

flip111 opened this issue Dec 24, 2018 · 3 comments

Comments

@flip111
Copy link

flip111 commented Dec 24, 2018

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
Copy link
Author

flip111 commented Dec 24, 2018

@Gabriella439
Copy link
Owner

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

@flip111
Copy link
Author

flip111 commented Jan 1, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants