Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 25, 2024 08:38
Show Gist options
  • Save dacr/3174c908a996fc9b6e21810a6cf8f5a7 to your computer and use it in GitHub Desktop.
Save dacr/3174c908a996fc9b6e21810a6cf8f5a7 to your computer and use it in GitHub Desktop.
dump capitalized string coming from stdin using streams / published by https://github.com/dacr/code-examples-manager #3c60fbc5-e40e-4554-9051-eceeb628616a/ee5c4fd168660db458f9d6d79bdba0c2f4b3ad07
#!/usr/bin/env amm
// summary : dump capitalized string coming from stdin using streams
// keywords : scala, actors, akka, cat, streams
// publish : gist
// authors : Ewan Keith, David Crosson
// license : EwanKeith BUT Machine Learning models training is not allowed by the author
// id : 3c60fbc5-e40e-4554-9051-eceeb628616a
// created-on : 2021-02-08T07:17:52Z
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "2.13.14"
//> using dep "com.typesafe.akka::akka-http:10.2.7"
//> using dep "com.typesafe.akka::akka-actor-typed:2.6.18"
//> using dep "com.typesafe.akka::akka-stream-typed:2.6.18"
//> using dep "org.slf4j:slf4j-nop:1.7.32"
// ---------------------
import scala.concurrent.{Await, Future}
import akka.actor.ActorSystem
import akka.stream.IOResult
import akka.stream.scaladsl.{Keep, Sink, Source, StreamConverters}
import akka.util.ByteString
import scala.concurrent.duration.Duration
/*
Inspired from
https://medium.com/@ewan.keith100/using-akka-streams-with-standard-input-and-standard-output-38868a2020bd
*/
object Workaround {
implicit val sys: ActorSystem = ActorSystem("CapitaliseAndPrint")
val stdinSource: Source[ByteString, Future[IOResult]] = StreamConverters.fromInputStream(() => System.in)
val stdoutSink: Sink[ByteString, Future[IOResult]] = StreamConverters.fromOutputStream(() => System.out)
def capitaliseByteString(byteString: ByteString): ByteString = ByteString(byteString.utf8String.toUpperCase)
val (_, closed) = {
stdinSource
.map(capitaliseByteString)
.toMat(stdoutSink)(Keep.both)
.run()
}
}
Await.result(Workaround.closed, Duration.Inf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment