Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine these three methods into one

Tags:

methods

scala

I'm using parboiled to write a parser. I defined some methods as:

def InlineCharsBefore(sep: String) 
    = rule { zeroOrMore(!str(sep) ~ InlineChar) }
def InlineCharsBefore(sep1: String, sep2: String) 
    = rule { zeroOrMore((!str(sep1) | !str(sep2)) ~ InlineChar) }
def InlineCharsBefore(sep1: String, sep2: String, sep3: String) 
    = rule { zeroOrMore((!str(sep1) | !str(sep2) | !str(sep3)) ~ InlineChar) }

You can see they are very similar. I want to combine them into one, but I don't know how to do it. Maybe it should be:

def InlineCharsBefore(seps: String*) = rule { ??? }
like image 266
Freewind Avatar asked Jul 17 '11 16:07

Freewind


People also ask

How do you combine two methods in C#?

What you should do is parameterize the methods, by making parameter(s) of all difference between two equal functions. E.g. Use an if statement depending on the bool add. Btw, it would be better to use an enum instead of a boolean.


1 Answers

The vararg version can be implemented as:

 def InlineCharsBefore( seps: String* ) = {
   val sepMatch = seps.map( s => ! str(s) ).reduceLeft( _ | _ )
   rule { zeroOrMore( sepMatch ~ InlineChar) }
 }

However, I don't use parboiled so I cannot test it.

like image 137
paradigmatic Avatar answered Sep 27 '22 16:09

paradigmatic