The functional path in tasks related to IO

Most beginners program tasks related to IO without fail. Are there any general suggestions to help beginners move to a more functional way of completing IO-related tasks?

As a specific example, consider the task of moving directories and processing files along this path. For me, the imperative way is to walk through the directory tree and process each file, as in the C style languages. Then I discovered the Conduit library , which for me is a functional way: Create the source list, then use the list.

Can I say that β€œchannel path” is the preferred functional way of programming in IO related tasks?

+4
source share
2 answers

The approach to the channel is not to collect the entire list at once and destroy it; The point of the pipeline (and pipes, another current popular solution to this problem) is that you only consume one element at a time. This is similar to lazy IO, except in a more fundamental way, because lazy IO really annoys reasoning and becomes correct.

The first step is to try to pull as much of your logic into pure functions as possible; instead of writing parseFile :: FileName -> Result, write parseContents :: String -> Resultand use readFile (well, probably you should also use Text or ByteString instead of String, but this does not apply).

: , , , . , , , conduit, - Source IO FilePath, , Conduit FilePath IO Text, , Sink Text Result, . , (source $= conduit) $$ sink - , IO Result.

, , btu. , .

+2

: IO IO monad. IO . , IO monad ( ) . IO monad , - .

, . RxJava, Scala . - . , -, , . Your Mouse - , . , , , , .

+1

All Articles