{"id":8616,"date":"2020-01-27T10:47:53","date_gmt":"2020-01-27T15:47:53","guid":{"rendered":"https:\/\/irreal.org\/blog\/?p=8616"},"modified":"2020-01-27T10:47:53","modified_gmt":"2020-01-27T15:47:53","slug":"another-great-example-of-recursion","status":"publish","type":"post","link":"https:\/\/irreal.org\/blog\/?p=8616","title":{"rendered":"Another Great Example of Recursion"},"content":{"rendered":"<p>Remember how I told you that <a href=\"https:\/\/irreal.org\/blog\/?p=8543\">Joe Marshall was really good at coming up with elegant examples of recursion<\/a>? He\u2019s back with another. This time it\u2019s not one he thought up but it\u2019s too good not to share.<\/p>\n<p>Here\u2019s the problem: You have a server that you can query for time-stamped records by specifying a begin and end date. The problem is that the server will return (only) an error if the number of records you request is too large. There\u2019s no way to tell, <i>a priori<\/i>, how many records exist between any two dates. How do you write the client?<\/p>\n<p>Marshall offers <a href=\"http:\/\/funcall.blogspot.com\/2020\/01\/afraid-of-recursion.html\">a beautiful recursive solution<\/a> that solves the problem in 10 lines of\u2014I guess\u2014Java (I\u2019m not a Java maven so I\u2019m not sure). It\u2019s very much worthwhile reading and understanding the example so you can add the trick to your toolkit.<\/p>\n<p>Marshall\u2019s larger point is that often people are afraid of and resist recursive algorithms but they shouldn\u2019t be. It\u2019s not always the right solution but it is for many problems and usually results in a simple and easy-to-understand solution. As Marshall says, one can imagine solving the above problem without recursion but that solution would be more complicated and probably harder to maintain.<\/p>\n<p>I learned to use and appreciate recursion from reading Paul Graham and it\u2019s amazing how often I\u2019ve been able to amaze my colleagues with a nice recursive solution to some problem. If you\u2019re not comfortable with recursion, you should try to fix that.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Remember how I told you that Joe Marshall was really good at coming up with elegant examples of recursion? He\u2019s back with another. This time it\u2019s not one he thought up but it\u2019s too good not to share. Here\u2019s the &hellip; <a href=\"https:\/\/irreal.org\/blog\/?p=8616\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[135],"class_list":["post-8616","post","type-post","status-publish","format-standard","hentry","category-general","tag-programming"],"_links":{"self":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/8616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8616"}],"version-history":[{"count":1,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/8616\/revisions"}],"predecessor-version":[{"id":8617,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/8616\/revisions\/8617"}],"wp:attachment":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}