This is a quick demonstration of counting the number of fucks in a text file using scalaz-stream (which I love). This work and the word fuck should not be associated with the project or its authors. They seem like mightily helpful chaps! :)
sbt run
| target |
| name := "logstash-buffer-stream" | |
| scalaVersion := "2.11.2" | |
| libraryDependencies ++= { | |
| val scalazVersion = "7.1.0" | |
| val scalazStreamVersion = "0.5a" | |
| val scalacheckVersion = "1.11.5" | |
| Seq( | |
| "org.scalaz" %% "scalaz-core" % scalazVersion, | |
| "org.scalaz" %% "scalaz-effect" % scalazVersion, | |
| "org.scalaz" %% "scalaz-concurrent" % scalazVersion, | |
| "org.scalaz.stream" %% "scalaz-stream" % scalazStreamVersion, | |
| "org.scalacheck" %% "scalacheck" % scalacheckVersion % "test", | |
| "org.scalaz" %% "scalaz-scalacheck-binding" % scalazVersion % "test" | |
| ) | |
| } | |
| resolvers += "Scalaz Bintray Repo" at "http://dl.bintray.com/scalaz/releases" | |
| scalacOptions += "-feature" | |
| scalacOptions += "-unchecked" | |
| initialCommands in console := "import scalaz._, Scalaz._, scalaz.stream._" |
| package fuck | |
| import scalaz.stream._ | |
| import scalaz.concurrent.Task | |
| /* | |
| * Please do not think I am associating aggressive use of the word "fuck" with the authors/contributors | |
| * of Scalaz-stream. I am simply enjoying writing Scalaz Stream code lately and this can serve as an | |
| * example I can teach my [male] coworkers. Maybe. Full cycle. | |
| * | |
| * What has the toxic culture in Technical Operations done to me? I miss software engineering. Take me | |
| * back. | |
| */ | |
| object Fuck extends App { | |
| val inputFilename = "howmanyfucks.txt" | |
| val fuckRegex = "(fuck|Fuck|FUCK)" | |
| val fuckCounter = | |
| io.linesR(inputFilename). | |
| map(wordCount(_)). | |
| pipe(process1.sum). | |
| runLast | |
| fuckCounter.run.foreach(println) | |
| private def wordCount(s: String): Int = findFucks(s).length | |
| private def findFucks(s: String) = fuckRegex.r.findAllIn(s) | |
| } | |
| // Outside of package you can run it: | |
| // fuck.Fuck.main(Array[String]()) | |
| // In REPL something like this: | |
| // scala> Fuck.main(Array[String]()) | |
| // 5 | |
| // Alternative on command line with SBT installed: | |
| // sbt run |
| fuck | |
| bla | |
| foo bar | |
| foo bar baz | |
| fuck bar baz | |
| baz | |
| Bar Fuck | |
| mindFUCK | |
| clusterfuck | |
| adding badly capitalized one: fUcK (should not be counted) |