Skip to content

Instantly share code, notes, and snippets.

@mbbx6spp
Last active July 15, 2016 22:49
Show Gist options
  • Save mbbx6spp/0d9f18eebe4ddfa6d8b5 to your computer and use it in GitHub Desktop.
Save mbbx6spp/0d9f18eebe4ddfa6d8b5 to your computer and use it in GitHub Desktop.
Scalaz-stream code snippet inspired by 'Gender and Language: Fuck as a metaphor for male sexual aggression' (by Pamela Hobbs) - http://home.earthlink.net/~p37954/sitebuildercontent/sitebuilderfiles/Fuck.pdf

Fuck

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! :)

Running

sbt run
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment