-
Notifications
You must be signed in to change notification settings - Fork 90
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
Comments
I saw that https://hackage.haskell.org/package/turtle-1.5.13/docs/src/Turtle.Prelude.html#inhandle uses https://hackage.haskell.org/package/text-1.2.3.1/docs/Data-Text-IO.html#v:hGetLine for ByteString https://hackage.haskell.org/package/streaming-bytestring-0.1.6/docs/Data-ByteString-Streaming.html#g:17 would be needed. While Text has some nice properties .. to be able to use ByteString is good too |
@flip111: The idiomatic way to translate your PHP example to a {-# 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. |
I have this PHP script i'm trying to convert to haskell to see how scripting works.
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 thenText --> 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.
The text was updated successfully, but these errors were encountered: