{"id":5443,"date":"2020-01-21T16:27:27","date_gmt":"2020-01-21T16:27:27","guid":{"rendered":"https:\/\/www.blopig.com\/blog\/?p=5443"},"modified":"2020-01-21T22:27:27","modified_gmt":"2020-01-21T22:27:27","slug":"functional-programming-in-python","status":"publish","type":"post","link":"https:\/\/www.blopig.com\/blog\/2020\/01\/functional-programming-in-python\/","title":{"rendered":"Functional Programming in Python"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>The difficulty of reasoning about the behaviour of stateful programs, especially in concurrnent enviroments, has led to increased in intrest in a programming paradigm called <strong>functional programming<\/strong>. This style emphasises the connection between programs and mathematics, encouraging code that is easy to understand and, in some critical cases, even possible to prove properties of.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">First Class Functions<\/h2>\n\n\n\n<p>One of the first and  most important features of functional programming is <strong>first class functions<\/strong>. This means that a function can be treated as a <strong>value<\/strong> by the programming language. This enables the use of <strong>higher order functions<\/strong>, or functors: functions that take functions amoungst their arguments. The classic example of this is the <strong>map<\/strong> function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def map(func, iterable):\n    results = [func(value) for value in iterable]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Purity<\/h2>\n\n\n\n<p>Functional programs heavily stress the control of <strong>side effects<\/strong>. A side effect can be thought of as any modification to the state of the program (or the enviroment it runs in). As a function with no side effects at all is obviously of limited usefulness (being unable to even print to the screen), functional programs do not eliminate side effects but rather capture them in language abstraction so that their effect on the running of the program is easy to reason about.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def pure(x)\n    y = x.value + 1  # the value pointed to by x has not changed\n    return y\n\ndef impure(y):\n    y.value = y.value + 1  # The value pointed to by y has now changed throughout the whole program!\n    return y<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Recursion<\/h2>\n\n\n\n<p>Because traditional iteration, such as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for x in [1, 2, 3]:\n    print(x)<\/code><\/pre>\n\n\n\n<p>is inherently stateful (in this case the state variable is x as it takes different variable in different parts of the loop), functional programs often prefer<strong> recursion<\/strong>. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def printx(range):\n    if len(x) >= 1:\n        print(x[0])\n    else:\n        printx(x[1:])\n    return \nprintx([1,2,3])<\/code><\/pre>\n\n\n\n<p>Many algorithms are much more naturally expressed in terms of recursive function calls than as  imperative style loops. This style of writing iteration has close links the mathematical proof by induction, which can often be used to reason about the behaviour of such functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Referential Transparency<\/h2>\n\n\n\n<p>One of the most attractive features of functional programming is <strong>referential transparency<\/strong>. Functional programs do not feature assignment, which means that the value of some identifier <em>never changes<\/em>. This means that all expressions in the program are trivally <strong>reducible<\/strong>. This is the key feature of functional programing that contrast.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Imperative\nx = 1  # assignment\nx = 2  # REASIGNMENT\n\n# Functional (Python does not feature definition so we borrow the syntax of Haskell)\nx := 1  # definition\nx := 2 (gramatically incorrect in a functional language, and a deviation from the )\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>So, would a functional style be helpful for your coding needs? Is the abstraction worth the penalty to performance? Or do you think the inneficiancy born of a refusal to exploit the inherent statefulness of hardware is a waste?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction The difficulty of reasoning about the behaviour of stateful programs, especially in concurrnent enviroments, has led to increased in intrest in a programming paradigm called functional programming. This style emphasises the connection between programs and mathematics, encouraging code that is easy to understand and, in some critical cases, even possible to prove properties of.<\/p>\n","protected":false},"author":56,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","wikipediapreview_detectlinks":true,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"ngg_post_thumbnail":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[29,227],"tags":[290,19],"ppma_author":[497],"class_list":["post-5443","post","type-post","status-publish","format-standard","hentry","category-code","category-python-code","tag-functional-programming","tag-programming"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"authors":[{"term_id":497,"user_id":56,"is_guest":0,"slug":"conor","display_name":"Conor Wild","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/aae97a248f2eacf487f0938ce418327373299c8222379446dc7bde572e63271d?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/5443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/users\/56"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/comments?post=5443"}],"version-history":[{"count":2,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/5443\/revisions"}],"predecessor-version":[{"id":5445,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/5443\/revisions\/5445"}],"wp:attachment":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/media?parent=5443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/categories?post=5443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/tags?post=5443"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=5443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}