implementation icon indicating copy to clipboard operation
implementation copied to clipboard

Or method enhancements

Open metal3d opened this issue 11 years ago • 3 comments

Still working on implementatin, I see the whole implementation (excepting mine, but I'm wrong to implement this before speaking about with you) uses this kind of method:

or(value)
=> ...|value

I'm afraid that this one is not very usefull. It can be ok for simple test, but what about finding:

startofline, then "foo", then "bar", endofline OR startofline, then "bar", then "baz". endofline

My implementation does:

StartOfLine().Then("foo").Then("bar").EndOfLine().Or().StartOfLine().Then("bar").Then("baz").EndOfLine()

That returns:

(?m)(?:^(?:foo)(?:bar)$)|(?:^(?:bar)(?:baz)$)

I only record an array of "parts" to concatanate at compile time... My unittest work as expected.

That works with simple expressions, or multiple expressions...

Find("foo").Or().Find("bar")
(?m)(?:foo)|(?:bar)

StartOfLine().Then("foo").Then("bar").EndOfLine().
Or().StartOfLine().Then("bar").Then("baz").EndOfLine().
Or().StartOfLine().Find("AAA").EndOfLine().
(?m)(?:^(?:foo)(?:bar)$)|(?:^(?:bar)(?:baz)$)|(?:^(?:AAA)$)

metal3d avatar Aug 13 '13 14:08 metal3d

The right implementation of or is ambiguous.

Should it be capable of implementation like this?

(verbal-Expression-0 (or (Verbal-Expression-1) (Verbal-Expression-2) (Verbal-Expression-3) ) Verbal-Expression-4)

Or must it be implemented like this?

(Verbal-Expression-0) (or) (Verbal-Expression-1))

The first requires or to accept at least two arguments.

If or accepts no arguments then it has to be the second form. That's the way of the original implementation.

I went around this with the Racket implementation which currently is a macro that does the second but with further work could implement the first.

Ben

On Tue, Aug 13, 2013 at 7:12 AM, Patrice Ferlet [email protected]:

Still working on implementatin, I see the whole implementation (excepting mine, but I'm wrong to implement this before speaking about with you) uses this kind of method:

or(value) => ...|value

I'm afraid that this one is not very usefull. It can be ok for simple test, but what about finding:

startofline, then "foo", then "bar", endofline OR startofline, then "bar", then "baz". endofline

My implementation does:

StartOfLine().Then("foo").Then("bar").EndOfLine().Or().StartOfLine().Then("bar").Then("baz").EndOfLine()

That returns:

(?m)(?:^(?:foo)(?:bar)$)|(?: ^(?:bar)(?:baz)$)

I only records an array of "parts" to concatanate at compile time... My unittest work as expected.

— Reply to this email directly or view it on GitHubhttps://github.com/VerbalExpressions/implementation/issues/4 .

brudgers avatar Aug 13 '13 16:08 brudgers

Sorry for the late answer, I was out for 2 days. You're right, my implementation is not fully effective.

With my implementation, this tests:

A.(B+C)

should be call with distributed form:

(A+B) . (A+C)

"A" is evaluated 2 times if B is false

I like your idea but I dislike the fact to have "var args" (optionnal argument) (certainly because I develop with Go :))

I have got another idea that is (I guess) more effective. I think that Or() method should get a VerbalExpression object as argument. And with should add a method named "And()" that accept VerbalExpression object.

This is an example:

v1 = new VE().Find("B")
v2 = new VE().Find("C").Or(v1); // (B+C)
v = new VE().Find("A").And(v2) // A.(B+C)

This should return:

(?m)(?:A)(?:(?:B)|(?:C))

This way, we can imaginate very complexe expressions that are seperated in several objects... I can prepare a branch to validate if that works

metal3d avatar Aug 16 '13 08:08 metal3d

This is as I did in my branch:

https://github.com/metal3d/GoVerbalExpressions/blob/and-or-implementation/verbalexpressions.go#L329

See Or() and And() methods that accept VerbalExpression Objects as argument. My test: https://github.com/metal3d/GoVerbalExpressions/blob/and-or-implementation/verbalexpressions_test.go#L545 works like a charm.

Other Test for basic "Or": https://github.com/metal3d/GoVerbalExpressions/blob/and-or-implementation/verbalexpressions_test.go#L259 where I create new object as argument (Or(new...))

metal3d avatar Aug 16 '13 09:08 metal3d