<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Joseph Tully</title>
    <description>The latest articles on Forem by Joseph Tully (@jctully).</description>
    <link>https://forem.com/jctully</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F211901%2F05a61af5-9ade-41b2-b8ee-1a0c4ce2f9ab.png</url>
      <title>Forem: Joseph Tully</title>
      <link>https://forem.com/jctully</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jctully"/>
    <language>en</language>
    <item>
      <title>Function Decorators Problem in Python</title>
      <dc:creator>Joseph Tully</dc:creator>
      <pubDate>Sat, 17 Aug 2019 02:33:26 +0000</pubDate>
      <link>https://forem.com/jctully/function-decorators-problem-in-python-34i2</link>
      <guid>https://forem.com/jctully/function-decorators-problem-in-python-34i2</guid>
      <description>&lt;p&gt;I came across an interesting problem recently to do with a concept I was pretty unfamiliar with: function decorators. The problem came from the email subscription Daily Coding Problems. It stated:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last element of that pair. For example, car(cons(3, 4)) returns 3, and cdr(cons(3, 4)) returns 4.&lt;/p&gt;

&lt;p&gt;Given this implementation of cons:&lt;/p&gt;


&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;cons&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pair&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;pair&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Implement car and cdr.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Having some experience with Lisp languages (Scheme) I sort of got what was being asked: Implement these Lisp functions in python. But the stuff going on in the given code was weird to me, and actually there is a lot going on in these few lines of code. We have a function within a function, closure with the inner function accessing a and b although they aren't defined within it, and pair() taking in a function as an argument and cons() having a function as its return value. Also, what the heck is f?&lt;/p&gt;

&lt;p&gt;After looking at the problem for a while I was still not getting it. I thought "cons is a function that returns a function which takes in a function and applies it to what was given as parameters to cons" which was too hard to wrap my head around. After looking up some stuff about function decorators, I had some better intuition and realized cons is modifying pair by telling it what to apply its parameter to. I then restated my analysis as "pair is a function that asks 'what should i do to a and b?' and cons is a function that makes pair functions for certain a's and b's". This made a lot more sense to me, and I was able to come up with the solution below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;car&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;retFirst&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retFirst&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This could be further reduced using lambda functions to my overall answer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;car&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;cdr&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Just something I came across and found interesting, and maybe you will too! Also my first post on the site.&lt;/p&gt;

</description>
      <category>python</category>
      <category>functional</category>
    </item>
  </channel>
</rss>
