Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active June 25, 2023 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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/bc631f6d1bbf2955bb728a01c8146fdc66999921
#!/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.8"
//> 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