Skip to content
Permalink
Browse files

Reorganize Converter

  • Loading branch information...
kubukoz committed Nov 18, 2018
1 parent 8339b13 commit 80ed4d1a6784764597b6cc2d5ddc3927ba75bb04
Showing with 24 additions and 15 deletions.
  1. +8 −5 README.md
  2. +8 −5 docs/ReadmeExample.md
  3. +8 −5 docs/src/ReadmeExample.md
@@ -32,22 +32,25 @@ import java.util.concurrent.Executors
import scala.concurrent.ExecutionContext
object Converter extends IOApp {
private val blockingExecutionContextResource =
private val blockingExecutionContext =
Resource.make(IO(ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(2))))(ec => IO(ec.shutdown()))
def run(args: List[String]): IO[ExitCode] = Stream.resource(blockingExecutionContextResource).flatMap { blockingExecutionContext =>
val converter: Stream[IO, Unit] = Stream.resource(blockingExecutionContext).flatMap { blockingEC =>
def fahrenheitToCelsius(f: Double): Double =
(f - 32.0) * (5.0/9.0)
io.file.readAll[IO](Paths.get("testdata/fahrenheit.txt"), blockingExecutionContext, 4096)
io.file.readAll[IO](Paths.get("testdata/fahrenheit.txt"), blockingEC, 4096)
.through(text.utf8Decode)
.through(text.lines)
.filter(s => !s.trim.isEmpty && !s.startsWith("//"))
.map(line => fahrenheitToCelsius(line.toDouble).toString)
.intersperse("\n")
.through(text.utf8Encode)
.through(io.file.writeAll(Paths.get("testdata/celsius.txt"), blockingExecutionContext))
}.compile.drain.as(ExitCode.Success)
.through(io.file.writeAll(Paths.get("testdata/celsius.txt"), blockingEC))
}
def run(args: List[String]): IO[ExitCode] =
converter.compile.drain.as(ExitCode.Success)
}
```

@@ -22,22 +22,25 @@ import scala.concurrent.ExecutionContext
// import scala.concurrent.ExecutionContext
object Converter extends IOApp {
private val blockingExecutionContextResource =
private val blockingExecutionContext =
Resource.make(IO(ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(2))))(ec => IO(ec.shutdown()))
def run(args: List[String]): IO[ExitCode] = Stream.resource(blockingExecutionContextResource).flatMap { blockingExecutionContext =>
val converter: Stream[IO, Unit] = Stream.resource(blockingExecutionContext).flatMap { blockingEC =>
def fahrenheitToCelsius(f: Double): Double =
(f - 32.0) * (5.0/9.0)
io.file.readAll[IO](Paths.get("testdata/fahrenheit.txt"), blockingExecutionContext, 4096)
io.file.readAll[IO](Paths.get("testdata/fahrenheit.txt"), blockingEC, 4096)
.through(text.utf8Decode)
.through(text.lines)
.filter(s => !s.trim.isEmpty && !s.startsWith("//"))
.map(line => fahrenheitToCelsius(line.toDouble).toString)
.intersperse("\n")
.through(text.utf8Encode)
.through(io.file.writeAll(Paths.get("testdata/celsius.txt"), blockingExecutionContext))
}.compile.drain.as(ExitCode.Success)
.through(io.file.writeAll(Paths.get("testdata/celsius.txt"), blockingEC))
}
def run(args: List[String]): IO[ExitCode] =
converter.compile.drain.as(ExitCode.Success)
}
// defined object Converter
```
@@ -11,22 +11,25 @@ import java.util.concurrent.Executors
import scala.concurrent.ExecutionContext
object Converter extends IOApp {
private val blockingExecutionContextResource =
private val blockingExecutionContext =
Resource.make(IO(ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(2))))(ec => IO(ec.shutdown()))
def run(args: List[String]): IO[ExitCode] = Stream.resource(blockingExecutionContextResource).flatMap { blockingExecutionContext =>
val converter: Stream[IO, Unit] = Stream.resource(blockingExecutionContext).flatMap { blockingEC =>
def fahrenheitToCelsius(f: Double): Double =
(f - 32.0) * (5.0/9.0)
io.file.readAll[IO](Paths.get("testdata/fahrenheit.txt"), blockingExecutionContext, 4096)
io.file.readAll[IO](Paths.get("testdata/fahrenheit.txt"), blockingEC, 4096)
.through(text.utf8Decode)
.through(text.lines)
.filter(s => !s.trim.isEmpty && !s.startsWith("//"))
.map(line => fahrenheitToCelsius(line.toDouble).toString)
.intersperse("\n")
.through(text.utf8Encode)
.through(io.file.writeAll(Paths.get("testdata/celsius.txt"), blockingExecutionContext))
}.compile.drain.as(ExitCode.Success)
.through(io.file.writeAll(Paths.get("testdata/celsius.txt"), blockingEC))
}
def run(args: List[String]): IO[ExitCode] =
converter.compile.drain.as(ExitCode.Success)
}
```

0 comments on commit 80ed4d1

Please sign in to comment.
You can’t perform that action at this time.