Skip to content

Instantly share code, notes, and snippets.

@ShahOdin
Last active September 17, 2020 12:57
Show Gist options
  • Save ShahOdin/4d1b6b3f63f037940cfce738de594003 to your computer and use it in GitHub Desktop.
Save ShahOdin/4d1b6b3f63f037940cfce738de594003 to your computer and use it in GitHub Desktop.
simple utility tool that reads two files and outputs their difference
import java.io.PrintWriter
import cats.effect.{ExitCode, IO, IOApp, Resource, Sync}
import cats.syntax.traverse._
import cats.syntax.functor._
import scala.io.Source
object IdDiff extends IOApp {
def readFromFile[F[_]: Sync](name: String): F[List[String]] = Resource
.fromAutoCloseable(
Sync[F].delay(Source.fromFile(name))
)
.use(source => Sync[F].delay(source.getLines.toList))
def writeToFile[F[_]: Sync](name: String, lines: List[String]): F[Unit] = {
Resource.fromAutoCloseable(
Sync[F].delay(new PrintWriter(name))
)
.use(pw =>
lines.traverse(line => Sync[F].delay(pw.println(line)))
).void
}
override def run(args: List[String]): IO[ExitCode] = for {
lines1 <- readFromFile[IO](name = "???")
lines2 <- readFromFile[IO](name = "???")
diff = lines1.diff(lines2)
_ = println(s"first file has:${lines1.size} and second one has:${lines2.size}. The two had a diff of ${diff.size} lines.")
_ <- writeToFile[IO](name = "???", diff)
} yield ExitCode.Success
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment