简体   繁体   English

ZIO,入门,纯功能下载器

[英]ZIO, Getting started, pure functional downloader

I have a simple program that is more or less a downloader.我有一个简单的程序,它或多或少是一个下载器。 It collect one or many configurable resources (via a custom protocol), aggregate and write them in on place.它收集一个或多个可配置资源(通过自定义协议),将它们聚合并就地写入。

This program seems simple but the code is still complex and mix functional and procedural code (from my Java background).这个程序看起来很简单,但代码仍然很复杂,并且混合了功能和过程代码(来自我的 Java 背景)。 I would like to simplify it and move to an fully functional one.我想简化它并转向一个功能齐全的。 I am reading about ZIO and the whole picture start to build in my head.我正在阅读有关ZIO 的内容,整个画面开始在我脑海中建立。

However, after having read the getting started and the overview I still have some questions.但是,在阅读了入门指南和概述之后,我仍然有一些问题。

The main flow may be like :主要流程可能是这样的:

for {
  config    <- Configuration.parse(args)
  resources <- Downloaders.download(config)
  _         <- Writer.write(resources)
} yield ()
  1. I guess the configuration that contains static values but also one http client will be put into ZIO Environment .我想包含静态值和一个 http 客户端的配置将被放入 ZIO Environment

  2. The downloaders may be effects built from a future and may use the ZIO bracket to close the connection.下载器可能是未来构建的效果器,可能会使用 ZIO bracket关闭连接。 I guess that I can aggregate the results with fold .我想我可以用fold聚合结果。 But I don't know how to create and execute many downloaders.但我不知道如何创建和执行许多下载器。
    Edited a) Since Future are note pure, how can I model a effect that will run asynchronously (I plan to use Play Standalone WS Client) ?编辑 a)由于Future是纯音符,我如何建模将异步运行的效果(我计划使用 Play Standalone WS Client)?
    Edited b) I should create one downloader per target, the request will be assigned to them.编辑 b)我应该为每个目标创建一个下载器,请求将分配给它们。 But how should I implement that in my code ?但是我应该如何在我的代码中实现它? Should I create a ZIO[Configuration, Throwable, List[Downloader]] and then, later, execute all of them via collectAllPar ?我应该创建一个ZIO[Configuration, Throwable, List[Downloader]] ,然后通过collectAllPar执行所有这些吗?

  3. I have no idea on how to write the aggregation on disk.我不知道如何在磁盘上写入聚合。

I would like to use this project to promote ZIO internaly.我想用这个项目在内部推广 ZIO。 So I would like to use the best options and cleanest approach.所以我想使用最好的选择和最干净的方法。 Can someone give me some tips on how to implement those effects and structure my code ?有人能给我一些关于如何实现这些效果和构建我的代码的提示吗? Edited: Should I build my system on the ZIO components or build a set of functions and wrap them into the ZIO effects ?编辑:我应该在 ZIO 组件上构建我的系统还是构建一组函数并将它们包装到 ZIO 效果中?

Thanks a lot非常感谢

Your question is not very specific which means that unfortunately it won't attract high-quality answers.您的问题不是很具体,这意味着不幸的是它不会吸引高质量的答案。

I will still try to answer some of the points.我仍然会尝试回答一些观点。

  • Future s are not pure. Future不是纯的。 If possible, avoid them.如果可能,请避免它们。
  • Your donwloader will be a function Request => ZIO[R, E, Response] .您的下载器将是一个函数Request => ZIO[R, E, Response] To make them run in parallel, use ZIO.collectAllPar or ZIO.zip .要使它们并行运行,请使用ZIO.collectAllParZIO.zip The result is a ZIO[R, E, List[A]] or a ZIP[R, E, TupleX[...]] , which you can store in resources in your for-comprehension.结果是ZIO[R, E, List[A]]ZIP[R, E, TupleX[...]] ,您可以将它们存储在理解中的resources中。
  • Writing data to a file can be done with Scala's new PrintWriter(new File("hello.txt")) wrapped into, you guessed it, a ZIO.bracket .将数据写入文件可以通过 Scala 的new PrintWriter(new File("hello.txt")) ,你猜对了,一个ZIO.bracket

For more insights into program design with ZIO I would suggest reading Beautiful, Simple, Testable Functional Effects for Scala from De Goes himself.为了更深入地了解 ZIO 的程序设计,我建议阅读 De Goes 本人的Scala 的美丽、简单、可测试的功能效果

Also, of course, refer to the documentation found on ZIO's website .此外,当然,请参阅ZIO 网站上的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM