90 lines
No EOL
19 KiB
XML
90 lines
No EOL
19 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Caught in the Net (Posts about buridan'ass)</title><link>francescomecca.eu</link><description></description><atom:link href="francescomecca.eu/categories/buridanass.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2024 <a href="mailto:francescomecca.eu">Francesco Mecca</a> </copyright><lastBuildDate>Wed, 28 Feb 2024 09:29:26 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>The Buridan&#8217;s donkey paradox</title><link>francescomecca.eu/blog/2015/9/20/the-buridans-donkey-paradox/</link><dc:creator>Francesco Mecca</dc:creator><description><p style="text-align: left;">
|
||
The Buridan’s donkey is an illustration of a paradox regarding the philosophy of moral determinism and free will.
|
||
</p>
|
||
|
||
<p style="text-align: left;">
|
||
The paradox shows an hypothetical situation in which a donkey searching for food is caught in the middle of two equally appealing stacks of hay located at the same distance from the donkey. Because the donkey has no real reason to choose one over the other he dies of starvation.
|
||
</p>
|
||
|
||
<p><a href="https://en.wikipedia.org/wiki/Buridan's_ass"><img class="aligncenter size-full wp-image-209" src="http://francescomecca.eu/wp-content/uploads/2015/09/Deliberations_of_Congress.jpg" alt="Deliberations_of_Congress" width="431" height="425" srcset="http://francescomecca.eu/wp-content/uploads/2015/09/Deliberations_of_Congress-300x296.jpg 300w, http://francescomecca.eu/wp-content/uploads/2015/09/Deliberations_of_Congress.jpg 431w" sizes="(max-width: 431px) 100vw, 431px"></a>I have decided to write a cli program that chooses for me when I can’t make up my mind.</p>
|
||
<p>The program is written in C++ and when invoked along with two or more arguments it puts them in a vector and then changes the order randomly.</p>
|
||
<p>.. code:: c</p>
|
||
<div class="code"><pre class="code literal-block"><span class="n">#include</span><span class="w"> </span><span class="o">&lt;</span><span class="n">iostream</span><span class="o">&gt;</span>
|
||
<span class="n">#include</span><span class="w"> </span><span class="o">&lt;</span><span class="n">vector</span><span class="o">&gt;</span>
|
||
<span class="n">#include</span><span class="w"> </span><span class="o">&lt;</span><span class="n">algorithm</span><span class="o">&gt;</span>
|
||
<span class="n">#include</span><span class="w"> </span><span class="o">&lt;</span><span class="n">random</span><span class="o">&gt;</span>
|
||
<span class="n">#include</span><span class="w"> </span><span class="o">&lt;</span><span class="n">string</span><span class="o">&gt;</span>
|
||
<span class="n">#include</span><span class="w"> </span><span class="o">&lt;</span><span class="n">sys</span><span class="o">/</span><span class="n">poll</span><span class="p">.</span><span class="n">h</span><span class="o">&gt;</span>
|
||
<span class="k">using</span><span class="w"> </span><span class="n">namespace</span><span class="w"> </span><span class="n">std</span><span class="p">;</span>
|
||
|
||
<span class="n">struct</span><span class="w"> </span><span class="n">pollfd</span><span class="w"> </span><span class="n">stdin_poll</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="err">{</span>
|
||
<span class="w"> </span><span class="p">.</span><span class="n">fd</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fileno</span><span class="w"> </span><span class="p">(</span><span class="n">stdin</span><span class="p">),</span><span class="w"> </span><span class="p">.</span><span class="n">events</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">POLLIN</span>
|
||
<span class="err">}</span><span class="p">;</span>
|
||
|
||
<span class="n">void</span><span class="w"> </span><span class="n">read_from_piped_input</span><span class="w"> </span><span class="p">(</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">string</span><span class="o">&gt;&amp;</span><span class="w"> </span><span class="n">lst</span><span class="p">)</span>
|
||
<span class="err">{</span>
|
||
<span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="n">x</span><span class="p">;</span>
|
||
<span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">getline</span><span class="w"> </span><span class="p">(</span><span class="n">cin</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">))</span><span class="w"> </span><span class="err">{</span>
|
||
<span class="w"> </span><span class="n">lst</span><span class="p">.</span><span class="n">push_back</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
|
||
<span class="w"> </span><span class="err">}</span>
|
||
<span class="err">}</span>
|
||
|
||
<span class="n">void</span><span class="w"> </span><span class="n">read_from_arguments</span><span class="w"> </span><span class="p">(</span><span class="n">const</span><span class="w"> </span><span class="nc">int</span><span class="o">&amp;</span><span class="w"> </span><span class="n">argc</span><span class="p">,</span><span class="w"> </span><span class="nc">char</span><span class="o">*</span><span class="w"> </span><span class="n">argv</span><span class="err">[]</span><span class="p">,</span><span class="w"> </span><span class="n">vector</span><span class="o">&lt;</span><span class="n">string</span><span class="o">&gt;&amp;</span><span class="w"> </span><span class="n">lst</span><span class="p">)</span>
|
||
<span class="err">{</span>
|
||
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">argc</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="err">{</span>
|
||
<span class="w"> </span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="ss">"Usage: asino [string] [string] ..."</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">endl</span><span class="p">;</span>
|
||
<span class="w"> </span><span class="k">exit</span><span class="p">;</span>
|
||
<span class="w"> </span><span class="err">}</span>
|
||
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">string</span><span class="o">&gt;::</span><span class="n">size_type</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">argc</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="err">{</span>
|
||
<span class="w"> </span><span class="n">lst</span><span class="p">.</span><span class="n">push_back</span><span class="w"> </span><span class="p">(</span><span class="n">argv</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">);</span>
|
||
<span class="w"> </span><span class="err">}</span>
|
||
<span class="err">}</span>
|
||
|
||
<span class="nc">int</span><span class="w"> </span><span class="n">main</span><span class="w"> </span><span class="p">(</span><span class="nc">int</span><span class="w"> </span><span class="n">argc</span><span class="p">,</span><span class="w"> </span><span class="nc">char</span><span class="o">*</span><span class="w"> </span><span class="n">argv</span><span class="err">[]</span><span class="p">)</span>
|
||
<span class="err">{</span>
|
||
<span class="w"> </span><span class="n">vector</span><span class="o">&lt;</span><span class="n">string</span><span class="o">&gt;</span><span class="w"> </span><span class="n">lst</span><span class="p">;</span>
|
||
<span class="w"> </span><span class="nc">int</span><span class="w"> </span><span class="n">poll_ret</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">poll</span><span class="w"> </span><span class="p">(</span><span class="o">&amp;</span><span class="n">stdin_poll</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
|
||
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">poll_ret</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="err">{</span>
|
||
<span class="w"> </span><span class="n">read_from_piped_input</span><span class="w"> </span><span class="p">(</span><span class="n">lst</span><span class="p">);</span>
|
||
<span class="w"> </span><span class="err">}</span>
|
||
<span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="err">{</span>
|
||
<span class="w"> </span><span class="n">read_from_arguments</span><span class="w"> </span><span class="p">(</span><span class="n">argc</span><span class="p">,</span><span class="w"> </span><span class="n">argv</span><span class="p">,</span><span class="w"> </span><span class="n">lst</span><span class="p">);</span>
|
||
<span class="w"> </span><span class="err">}</span>
|
||
|
||
<span class="w"> </span><span class="n">random_device</span><span class="w"> </span><span class="n">rd</span><span class="p">;</span>
|
||
<span class="w"> </span><span class="n">mt19937</span><span class="w"> </span><span class="n">m</span><span class="p">(</span><span class="n">rd</span><span class="p">());</span>
|
||
<span class="w"> </span><span class="n">shuffle</span><span class="w"> </span><span class="p">(</span><span class="n">lst</span><span class="p">.</span><span class="k">begin</span><span class="w"> </span><span class="p">(),</span><span class="w"> </span><span class="n">lst</span><span class="p">.</span><span class="k">end</span><span class="w"> </span><span class="p">(),</span><span class="w"> </span><span class="n">m</span><span class="p">);</span>
|
||
|
||
<span class="w"> </span><span class="nc">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
|
||
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">string</span><span class="o">&gt;::</span><span class="n">iterator</span><span class="w"> </span><span class="n">it</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lst</span><span class="p">.</span><span class="k">begin</span><span class="w"> </span><span class="p">();</span><span class="w"> </span><span class="n">it</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">lst</span><span class="p">.</span><span class="k">end</span><span class="w"> </span><span class="p">();</span><span class="w"> </span><span class="o">++</span><span class="n">it</span><span class="p">)</span><span class="w"> </span><span class="err">{</span>
|
||
<span class="w"> </span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="ss">". "</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="o">*</span><span class="n">it</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">endl</span><span class="p">;</span>
|
||
<span class="w"> </span><span class="err">}</span>
|
||
<span class="err">}</span>
|
||
</pre></div>
|
||
|
||
<p>I have used the <a href="http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine" target="_blank">Mersenne Twister PRNG</a> just to give it a try.</p>
|
||
<p style="text-align: left;">
|
||
One of the challenges was to read from stdin instead of arguments when the program is piped after another program in the shell:
|
||
</p>
|
||
<p>.. code:: sh</p>
|
||
<div class="code"><pre class="code literal-block">ls /media/my_movies/ | buridan
|
||
</pre></div>
|
||
|
||
<p>So I have used poll() that checks for a specified amount of time if the selected device (/dev/stdin in my case) can perform I/O operations; in my code:</p>
|
||
<p>.. code:: bash</p>
|
||
<div class="code"><pre class="code literal-block">poll (&amp;stdin_poll, 1, 0)
|
||
</pre></div>
|
||
|
||
<p>I selected the POLLIN as event so poll() only checks if there is data to read, 1 as the number of items in the fds array, 0 milliseconds of timeout because when the program is invoked /dev/stdin may already contain input.</p>
|
||
<p>The program should be compiled this way:</p>
|
||
<p>.. code:: bash</p>
|
||
<div class="code"><pre class="code literal-block">g++ -std=c++11 ./program.cpp -o output
|
||
</pre></div>
|
||
|
||
<p>You are free to reuse this little piece of code as you wish.</p>
|
||
<p>EDIT: 02-04-2016
|
||
The original idea for the Buridan's donkey came from my mentor <a href="https://twitter.com/bassosimone">Simone Basso</a> who wrote the original one in haskell.</p>
|
||
<p style="text-align: right;">
|
||
Francesco Mecca
|
||
</p></description><category>buridan</category><category>buridan c++</category><category>buridan donkey</category><category>buridan'ass</category><category>c++11</category><category>PesceWanda</category><category>poll()</category><category>poll.h</category><guid>francescomecca.eu/blog/2015/9/20/the-buridans-donkey-paradox/</guid><pubDate>Sun, 20 Sep 2015 10:34:36 GMT</pubDate></item></channel></rss> |