FP: Capturing the characteristics of a process which blocks, causes side-effects, and may fail

I have a driver function modifyFile that interacts with many sources in the outside world (e.g. HTTP, filesystem). Let’s say the code is as such:

def downloadFile(from: String, to: String): Try[Unit]

def runUnixProcess(cmd: String): Boolean

def modifyFile = {
    downloadFile(from = "http://.../some/file.dat", to = "/tmp/file.dat") // 
    val status = runUnixProcess("/usr/bin/somebinaries /tmp/file.dat")
    if (status == Status.OK) deleteFile("/tmp/file.dat")
}

For the sake of the argument, let’s consider that these functions abide to the Single Responsibility Principle rule, and that modifyFile is only a driver that “integrates” and facilitates communication between separate entities such as OS, File system and web service.

While this function will most likely sit in the higher level of the application logic, some other application function might be built on top of it. Hence, I want to have some kind of proper abstraction (e.g. by wrapping it with a monadic construct) so that we can still compose an even higher abstraction over it.

Now my question: What is the best language construct to abstract this kind of computation? Namely a computation that:

  • calls to many entities in the external world
  • may fail, either partially or completely at some steps due to various conditions. To name a few, we might be dealing with filesystem permission issue, network connection issue, unix binaries missing issue, etc.
  • might need to be ran asynchronously, considering that there are several blocking calls in it.

Is Scalaz’s Task up for it?

Or should I simply wrap it as a Future[Try[T]], which basically captures most of the characteristics it exhibits?

I’m not sure how easy this would be to implement in Scala, but here’s how I’d look to structure it in Haskell:

To begin I’m going to review a couple of the monads which Haskell and F# provide — You’re probably familiar with them, but I’ll review them just to set the stage.

In Haskell there is the IO monad which is used for doing IO operations (e.g. web calls, file-system access) and which keeps side-effects out of pure code.

There are also the Maybe and Either monads; these monads will short-circuit a chain of monadic functions when there’s an error/failure in one of the functions, bypassing the remaining functions in the chain.

In F# there’s the Async monad (aka workflow/computation expression) — I’m sure there’s some equivalent in Haskell, but I’ve not worked with one yet — which lets you run asynchronous operations with code that looks like regular sequential calls. (Here’s an intro tutorial on async programming in F#, and there’s another tutorial on that site which goes more into the development of the workflow and how bind for it naturally arises from callbacks, but I can’t find it at the moment.)

Now, what you’re asking about is some way to use all of these at once. There is a way to do this, and it’s a little more complex, but it’s definitely a nice thing to have in your toolbox.

Beyond monads, there are monad transformers; these allow you to compose monads together, creating a kind of super monad that allows you to use the all capabilities of the monads composing it — think of it as kind of a monad stack where you can access the individual monads at different levels (using the lift function).

Some example (return) type-signatures:

IO (Either String a) -- Either & IO
IO (Async (Maybe a)) -- Maybe, Async, IO

If, for example, you have a bunch of IO operations you want to run sequentially, any of which might fail, then you’d probably create a transformer stack using the IO and Either monads.

I won’t go into the details of them here, but the paper Monad Transformers Step-by-Step is a pretty good introductory resource.

Monad transformers have, IMO, a steep learning curve, so if you’re in a hurry to get something implemented then this probably isn’t the way to go, but maybe it’ll give you some ideas. If you’re looking for a long-lasting general abstraction then you probably should consider monad transformers, despite the large up-front investment.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật