[Haskell-beginners] calling inpure functions from pure code
Sean Perry
shaleh at speakeasy.net
Fri Oct 12 20:58:20 CEST 2012
On Oct 12, 2012, at 7:19 AM, Emmanuel Touzery wrote:
>>
>> Overall, splitting your algorithm into simple steps — steps that would
>> do just a part of work and return incomplete objects — is the way to go.
>>
>
> You have a point, about splitting code for smaller functions. I would just rather have getDetails called from getProgramme rather than a parent calling both separately. And the parent must do the connection by doing the IO if I want both pieces to be pure. That is what is bothering me mostly.
>
Think about this from a testing perspective. How do you verify that your code which identifies links is working? If the link finding is mixed in with the link retrieving you end up having to dummy out the IO. Think of this as the code becomes more complicated and like Alexander suggests you want to later retrieve images too. Now you need to mock out the image retrieval as well.
Perhaps you should think of this as creating a matching DOM like structure. First you tree starts out empty. Then you parse the top level and return a new tree with data and dangling nodes that are links needing to be followed. You check "have I gone as deep as I would like?". If not, pass in the new partial tree to the retrieval routine and start filling it in. Now you are back to the depth check. When the retrieval has reached its goal the tree is returned and it is as populated as it can be. Now the rest of your code can use the tree for whatever it needs.
Remember to always ask "how do I test this?". One of the key reasons to keep purity is it makes the testing so much easier. Every small piece can be verified.
More information about the Beginners
mailing list