Last active
April 11, 2023 16:47
-
-
Save dacr/26132a03a758fcc56a3833895887c251 to your computer and use it in GitHub Desktop.
zio-streams ZStream collectZIO stucked forever #7301 ? the ok case replacing collectZIO with mapZIO / published by https://github.com/dacr/code-examples-manager #3d78d1a8-0114-473e-abe4-b923b5af6341/560a2d60e635b39adfb78df2181a3ad112453d8a
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
// summary : zio-streams ZStream collectZIO stucked forever #7301 ? the ok case replacing collectZIO with mapZIO | |
// keywords : scala, ziostream, issue | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 3d78d1a8-0114-473e-abe4-b923b5af6341 | |
// created-on : 2022-09-03T09:32:10+02:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// attached-files | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.2.2" | |
//> using dep "dev.zio::zio-streams:2.0.10" | |
// --------------------- | |
// https://github.com/zio/zio/issues/7301 | |
import zio.* | |
import zio.stream.* | |
import scala.util.matching.Regex | |
import java.time.LocalDateTime | |
import java.time.format.DateTimeFormatter | |
object Photos extends ZIOAppDefault { | |
def parseTimestamp(timestamp: String): ZIO[Any, Throwable, LocalDateTime] = ZIO.succeed(LocalDateTime.now()) | |
def run = for { | |
_ <- ZIO.log(s"scanning...") | |
mask <- ZIO.attempt(""".*/(\d{8}-\d{6})-\d{2}-(.*)-evt.jpg""".r) | |
entries <- | |
ZStream | |
.fromIterable(List("./20220902-162930-00-raspcam1-evt.jpg", "./20220901-195134-00-raspcam1-evt.jpg", "./20220901-204013-00-raspcam1-evt.jpg")) | |
.filterNot(_.contains("Trash")) | |
.mapZIO { | |
case file@mask(timestamp, group) => parseTimestamp(timestamp).map(ts => Some(ts -> file)) | |
case _ => ZIO.succeed(None) | |
} | |
.collect { case Some(photo) => photo } | |
.tap(f => ZIO.log(s"found $f")) | |
.runCollect | |
_ <- ZIO.log(s"found ${entries.size} entries") | |
} yield () | |
} | |
Photos.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment