Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

who says perl is not readable?

Name: Anonymous 2015-11-06 14:25

my $rxParsePath = qr
{(?x) # Use extended regular expression syntax to
# allow comments and white space
^ # Anchor pattern to beginning of string
(?=.) # Zero-width look ahead assertion to ensure
# that there must be at least one character
# for the match to succeed
(.*/)? # A memory grouping (1st) for path, greedy
# match of any characters up to and including
# the rightmost slash (the path part) with a
# quantifier of '?' (0 or 1), i.e. there
# may or may not be a directory part
( # Open memory grouping (2nd) for file name
(.*?) # A memory grouping (3rd) for file name stub
# of a non-greedy match of any character
# without a quantifier since, if there is a
# file name part, at least some of it will
# form a stub otherwise it would be a dot-file
( # A memory grouping (4th) for file name
# extension
(?<=[^/]) # zero width look behind assertion such
# that following pattern will only succeed
# if preceded by any caracter other than
# a slash '/'
\.[^.]+ # a literal dot '.' followed by one or more
# non-dots
)? # Close memory grouping (4th) with a quantifier
# of '?' (0 or 1), i.e. there may or may not
# be a file name extension part
)? # Close memory grouping (2nd) with a quantifier
# of '?' (0 or 1), i.e. there may or may not
# be a file name part
$ # Anchor pattern to end of string
};

Name: Anonymous 2015-11-06 14:53

Comments that are nothing more than verbose English rehashes of the code suck.

Name: Anonymous 2015-11-06 18:20

Writing monadic parsers is much easier and you almost never have to write a book to document them.

Name: Perlmind 2015-11-06 20:34

Name: Anonymous 2015-11-07 3:05

anyChar = lpeg.P(1)
dotChar = lpeg.P"."
slashChar = lpeg.P"/"
optionalDotChar = dotChar^-1
anyCharButSlash = anyChar - slashChar
anyCharButDot = anyChar - dotChar
oneOrMoreNonDotChars = anyCharButDot ^ 1

followedByAtLeastOneChar = #anyChar
endOfString = lpeg.P(-1)

everythingUpToAndIncludingASlash = anyCharButSlash^0 * slashChar
everythingUpToAndIncludingTheLastSlash = everythingUpToAndIncludingASlash^0
path = everythingUpToAndIncludingTheLastSlash
capturePath = lpeg.C(path)

fileNameExtension = dotChar * oneOrMoreNonDotChars * endOfString
optionalFileNameExtension = fileNameExtension^-1
captureOptionalFileNameExtension = lpeg.C(optionalFileNameExtension)

anyCharNotStartOfFileExtension = anyChar - fileNameExtension
fileNameStub = optionalDotChar * anyCharNotStartOfFileExtension^0
captureFileNameStub = lpeg.C(fileNameStub)

fileName = captureFileNameStub * captureOptionalFileNameExtension
captureFileName = lpeg.C(fileName)

pathPattern = followedByAtLeastOneChar * capturePath * captureFileName

capturePathPattern = lpeg.C(pathPattern)

Name: Anonymous 2015-11-07 14:49

>>5
see this proves that haskell parser combinators is much more readable than perl, even with zero comments!

Name: Anonymous 2015-11-07 18:08

import Control.Arrow
import Data.Foldable (fold)
import Data.List (init)
import Data.List.Split

data Result = R { dirname :: String
, basename :: String
, filename :: String
, extension :: String
} deriving (Eq, Show)

type Error = String

parse :: String -> Either Error Result
parse str =
case flatten $ p str of
("", "", "") -> Left "No path"
(d, f, e) -> Right $ R d (f ++ e) f e

p = splitPath
& (init & fold
&&&
(last
& splitExt
& (init & fold
&&&
last)))

splitPath = split $ keepDelimsR $ onSublist "/"
splitExt = split $ keepDelimsL $ onSublist "."

infixr 4 &
(&) = flip (.)
flatten (x, (y, z)) = (x, y, z)

Name: Anonymous 2015-11-07 18:12

>>7
get rid of flatten

Name: Anonymous 2015-11-07 18:19

>>8
--- a.hs 2015-11-07 18:09:04.578542585 +0000
+++ b.hs 2015-11-07 18:14:04.119451873 +0000
@@ -15,9 +15,10 @@

parse :: String -> Either Error Result
parse str =
- case flatten $ p str of
- ("", "", "") -> Left "No path"
- (d, f, e) -> Right $ R d (f ++ e) f e
+ case p str of
+ ("", ("", "")) -> Left "No path"
+ (d, (f, e)) -> Right $ R d (f ++ e) f e

f = splitPath
& (init & fold
@@ -33,4 +34,3 @@

infixr 4 &
(&) = flip (.)
-flatten (x, (y, z)) = (x, y, z)

Name: Anonymous 2015-11-07 19:05

>>9
How on earth did that take you 5 minutes?

Name: Anonymous 2015-11-08 11:05

>>7
some test cases:
"foo/bar/baz.boo.bee" should return ".bee" as the extension and "baz.boo" as the basename
"foo/.bar" should return ".bar" as the basename and no extension.
"foo/bar." should return "bar." as the basename and no extension

Name: Anonymous 2015-11-08 14:10

>>11
Why? Why is it the job of the regex to determine ".bar" is actually the whole filename? Dealing with such a platform-specific special case would logically be the job of the enclosing program.

Name: Anonymous 2015-11-08 14:18

^(?=.)
Just like my Lens!

Name: Anonymous 2015-11-08 17:47

>>12
Because your code should be correct.

Name: Anonymous 2015-11-08 19:25

>>14
And the specification...?

Name: Anonymous 2015-11-08 20:45

>>15
It's in >>1, try to keep up.

Name: Anonymous 2015-11-09 19:37

>>16
The ellipsis was a prompt for you to finish the sentence as a continuation of the statement in >>14.
Acceptable responses include ``need not be correct, obviously'' and ``is unrelated to the problem domain''.

Name: Anonymous 2015-11-09 20:10

>>17
What are you yammering about?

Name: Anonymous 2015-11-09 20:38

>>18
Let's go through the conversation I would have liked to have had with you.

A. Why? Why is it the job of the regex to determine ".bar" is actually the whole filename? Dealing with such a platform-specific special case would logically be the job of the enclosing program.
B. Because your code should be correct.
A. And the specification...?
B. The specification need not be correct, obviously. The specification is unrelated to the problem domain.

Now can you please stop ruining my fantasies?

Name: Anonymous 2015-11-09 23:47

>>17,19
The ellipsis adds what is known as a `pregnant' or `meaningful' pause, and signifies that you'd like the listener to carefully consider the meaning of the preceding sentence, but does not usually prompt a direct continuation by your opponent.

The pause is correct, but you would have to additionally roll your hands in front of you in the way that in this part of the world is recognized as meaning `go on' or `continue'.

Name: Anonymous 2015-11-10 19:28

>>20
I did gesture for you to continue, you just weren't there to see it.

Name: Anonymous 2015-11-10 20:49

>>21
I hereby register a gesture for you to check these duplicated digits.

Name: Anonymous 2015-11-11 1:40

This thread is very readable. Except for the Perl parts.

Name: Anonymous 2015-11-11 8:27

This thread is very checkable. Except for the non-dubs parts.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List