I'm looking to build a Reactive Web application and in Haskell. I've read through the Yesod book and some blog posts (http://www.yesodweb.com/blog/2012/10/yesod-fay-js - this one to be specific) and it seems as though (correct me if I'm wrong) the Yesod Framework in 2012 wasn't really suitable for that. Have there been changes to that end? In fact a more basic question (excuse my naievity) is would Yesod work with with reactive libraries like Reactive banana to provide the same functionality as the Reactive Stocks example from Play + Scala (https://www.typesafe.com/activator/template/reactive-stocks)?
What options to I have for client side interactivity in Yesod besides Angular and Fay as mentioned in the article?submitted by ChavXO
[link] [8 comments]
When I read John A De Goes post A Modern Architecture for FP I found it to be a bit too vague for me, but the topic was just interesting enough to finally push me to play a little with free monads. It’s not the first post I’ve read on the topic, there have been many before. None have quite managed to push me into actually doing something though!A file API
To make it concrete but still short enough to not bore readers I came up with a small API for working with files:data SimpleFileF a = LoadFile FilePath (String -> a) | SaveFile FilePath String a
The free monad wraps a functor, so here’s an implementation of thatinstance Functor SimpleFileF where fmap f (LoadFile fp f')= LoadFile fp (f . f') fmap f (SaveFile fp d a) = SaveFile fp d (f a)
Now for some convenient functions to work with the API typeloadFile :: FilePath -> Free SimpleFileF String loadFile fp = liftF $ LoadFile fp id saveFile :: FilePath -> String -> Free SimpleFileF () saveFile fp d = liftF $ SaveFile fp d ()
With this in place I can write a somewhat more complex onewithSimpleFile :: (String -> String) -> FilePath -> Free SimpleFileF () withSimpleFile f fp = do d <- loadFile fp let result = f d saveFile (fp ++ "_new") result
Now I need a way to run programs using the APIrunSimpleFile :: Free SimpleFileF a -> IO a runSimpleFile = foldFree f where f (LoadFile fp f') = liftM f' $ readFile fp f (SaveFile fp d r) = writeFile fp d >> return r
If this code was save in the file FreePlay.hs I can now convert it all to upper case by usingrunSimpleFile $ withSimpleFile toUpper "FreePlay.hs"
which of course will create the file FreePlay.hs_new.What have I bought so far?
Well, not much really.
So far it’s not much more than a rather roundabout way to limit what IO actions are available. In other words it’s not much more than what can be done by creating a limited IO, as in my earlier posts here and here.
Of course it would be possible to write another run function, e.g. one that doesn’t actually perform the file actions but just says what it would do. The nice thing though is, to use that same metaphor as John does, that I can add a layer to the onion. In other words, I can decorate each use of my SimpleFileF API with some other API. I think it was John’s description of this layering that pushed me to play with Free.