Last active
November 16, 2020 08:51
-
-
Save dorsev/b806512d0a8b7e9e63d63b32f6f27318 to your computer and use it in GitHub Desktop.
ZStream word count example
This file contains 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
import scala.util.Try | |
scalaVersion in ThisBuild := "2.12.11" | |
val zio = "dev.zio" % "zio" % "1.0.0" | |
resolvers += Resolver.sonatypeRepo("snapshots") | |
lazy val root = Project("hello-world", file(".")) | |
.settings( | |
Seq( | |
organization := "io.bigpanda", | |
name := "example", | |
libraryDependencies += "dev.zio" %% "zio" % "1.0.1", | |
libraryDependencies += "dev.zio" %% "zio-test" % "1.0.1" % "test", | |
libraryDependencies += "dev.zio" %% "zio-streams" % "1.0.3", | |
libraryDependencies += "dev.zio" %% "zio-test-sbt" % "1.0.1" % "test", | |
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework") | |
)) |
This file contains 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
package wordCount | |
import java.io._ | |
import zio._ | |
import zio.console._ | |
import zio.stream.{ZStream, ZTransducer} | |
object wordCount extends zio.App { | |
def run(args: List[String]) = | |
myAppLogic.exitCode | |
val myAppLogic = | |
for { | |
_ <- putStrLn( | |
"Hello! What path do you want to word count? please enter full path") | |
fullPath <- getStrLn | |
count <- ZStream | |
.fromInputStream(new FileInputStream(fullPath)) | |
.aggregate(ZTransducer.utf8Decode) | |
.aggregate(ZTransducer.splitLines) | |
.mapM(countWords) | |
.aggregate(ZTransducer.collectAllN(10)) | |
.fold(0)((agg, elem) => agg + elem.sum) | |
_ <- zio.console.putStrLn(s"found $count words") | |
} yield count | |
def countWords(str: String): UIO[Int] = UIO(str.split(" ").length) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment