Last active
November 29, 2017 14:25
-
-
Save frgomes/18c88ccfa9b733b8a8f8d970641fc798 to your computer and use it in GitHub Desktop.
Scala - grep-like processing a list of inclusions and a list of exclusions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //------------------------------------------------------------------------------------------------- | |
| // This would be something equivalent to: | |
| // $ cat inputs.txt | fgrep -f includes.txt | fgrep -v -f excludes.txt | uniq | sort | |
| //------------------------------------------------------------------------------------------------- | |
| import scala.util.matching.Regex | |
| def grep(inputs: Seq[String], includes: Set[Regex], excludes: Set[Regex], sort: Boolean = false, uniq: Boolean = false): Iterable[String] = { | |
| def pass(s: String, set: Set[Regex]): Boolean = set.exists(r => r.findFirstIn(s).isDefined) | |
| val grep1: Iterable[String] = if(includes.size==0) inputs else for { c <- inputs if(pass(c, includes)) } yield { c } | |
| val grep2: Iterable[String] = if(excludes.size==0) grep1 else for { c <- grep1; e <- excludes if(!pass(c, excludes)) } yield { c } | |
| val unique: Iterable[String] = if(uniq) grep2.toSet else grep2 | |
| val sorted: Iterable[String] = if(sort) unique.toSeq.sorted else unique | |
| sorted | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of use: