How should I derive values ​​from a list as they are calculated?

I have a lengthy calculation that outputs a list. I want to derive the values ​​from this list as they are calculated. What would be a neat way to do this?

I am currently using mapM_ print to print each value in STDOUT. This is good enough for the simple case of printing values ​​on the command line, but seems a bit hacky and hard to work with.

Also, at some point, I want to turn the output of my command line into interactive visualization. How could I turn my list into something like an FRP event stream? The ability to connect it to an existing graphical interface as an event source would be great.

Rewriting a function to use anything other than a list is an option, although a solution that allows me to take an as-is list would be ideal.

+6
source share
1 answer

This task is for iterations and iterations like libraries.

Using the proxy library.

 import Control.Proxy runProxy $ fromListS [1..10] >-> <processing> >-> printD >-> <more> processing> 

Where <processing> are the addition calculations you have to do.

Related questions: lazy version of mapM , Is Haskell mapM not lazy?

For instance:

 > labeledPrint label x = putStrLn $ label ++ show x > runProxy $ fromListS [1..4] >-> printD >-> mapD (*2) >-> useD (labeledPrint "Second printer: ") 1 Second printer: 2 2 Second printer: 4 3 Second printer: 6 4 Second printer: 8 

If you cancel the order of use of the application and use <-< instead of >-> , it will look like a normal application.

 runProxy $ useD (labeledPrint "Second printer: ") <-< mapD (*2) <-< printD <-< fromListS [1..4] 
+6
source

All Articles