2016-05-01 11:13:57 +02:00
<!DOCTYPE html>
< html lang = "en-us" >
2017-05-09 11:29:54 +02:00
2016-05-01 11:13:57 +02:00
< head >
2017-05-09 11:29:54 +02:00
< meta charset = "UTF-8" >
< title > Caught in the Net< / title >
< meta name = "viewport" content = "width=device-width, initial-scale=1" >
< meta name = "theme-color" content = "#157878" >
< link rel = "stylesheet" href = "/css/normalize.css" >
<!-- <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'> -->
< link rel = "stylesheet" href = "/fonts/opensans.css" >
< link rel = "stylesheet" href = "/css/cayman.css" >
2016-05-01 11:13:57 +02:00
< / head >
2017-05-09 11:29:54 +02:00
< body >
< section class = "page-header" >
< h1 class = "project-name" > Caught in the Net< / h1 >
< h2 class = "project-tagline" > La rete ti cattura ma libera il pensiero< / h2 >
< a class = "btn" href = "/" > Home< / a >
2017-05-09 12:17:48 +02:00
< a class = "btn" href = "/about/" > About me< / a >
2017-05-09 11:29:54 +02:00
< a class = "btn" href = "/contattami/" > Contact me< / a >
2017-05-10 20:26:52 +02:00
< a class = "btn" href = "/archive/" > Archive< / a >
< a class = "btn" href = "/feed.xml" > RSS< / a >
2017-09-22 14:07:26 +02:00
< a class = "btn" href = "http://francescomecca.eu/git/explore/repos" > Personal Git< / a >
2017-05-09 11:29:54 +02:00
< a class = "btn" href = "https://github.com/FraMecca" > Github< / a >
2017-05-09 11:38:51 +02:00
< a class = "btn" href = "/curriculum/CV_Mecca_Francesco.pdf" > Curriculum< / a >
2017-05-09 11:29:54 +02:00
< / section >
2016-05-01 11:13:57 +02:00
2017-05-09 11:29:54 +02:00
< section class = "main-content" >
2016-05-01 11:13:57 +02:00
< div class = "post" >
< h1 class = "post-title" > The Buridan’ s donkey paradox< / h1 >
< span class = "post-date" > 20 Sep 2015< / span >
< 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 >
< figure class = "highlight" > < pre > < code class = "language-c" data-lang = "c" > < span class = "cp" > #include < iostream>
#include < vector>
#include < algorithm>
#include < random>
#include < string>
#include < sys/poll.h>
< / span > < span class = "n" > using< / span > < span class = "n" > namespace< / span > < span class = "n" > std< / span > < span class = "p" > ;< / span >
< span class = "k" > struct< / span > < span class = "n" > pollfd< / span > < span class = "n" > stdin_poll< / span > < span class = "o" > =< / span > < span class = "p" > {< / span >
< span class = "p" > .< / span > < span class = "n" > fd< / span > < span class = "o" > =< / span > < span class = "n" > fileno< / span > < span class = "p" > (< / span > < span class = "n" > stdin< / span > < span class = "p" > ),< / span > < span class = "p" > .< / span > < span class = "n" > events< / span > < span class = "o" > =< / span > < span class = "n" > POLLIN< / span >
< span class = "p" > };< / span >
< span class = "kt" > void< / span > < span class = "nf" > read_from_piped_input< / span > < span class = "p" > (< / span > < span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "n" > string< / span > < span class = "o" > > & < / span > < span class = "n" > lst< / span > < span class = "p" > )< / span >
< span class = "p" > {< / span >
< span class = "n" > string< / span > < span class = "n" > x< / span > < span class = "p" > ;< / span >
< span class = "k" > while< / span > < span class = "p" > (< / span > < span class = "n" > getline< / span > < span class = "p" > (< / span > < span class = "n" > cin< / span > < span class = "p" > ,< / span > < span class = "n" > x< / span > < span class = "p" > ))< / span > < span class = "p" > {< / span >
< span class = "n" > lst< / span > < span class = "p" > .< / span > < span class = "n" > push_back< / span > < span class = "p" > (< / span > < span class = "n" > x< / span > < span class = "p" > );< / span >
< span class = "p" > }< / span >
< span class = "p" > }< / span >
< span class = "kt" > void< / span > < span class = "nf" > read_from_arguments< / span > < span class = "p" > (< / span > < span class = "k" > const< / span > < span class = "kt" > int< / span > < span class = "o" > & < / span > < span class = "n" > argc< / span > < span class = "p" > ,< / span > < span class = "kt" > char< / span > < span class = "o" > *< / span > < span class = "n" > argv< / span > < span class = "p" > [],< / span > < span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "n" > string< / span > < span class = "o" > > & < / span > < span class = "n" > lst< / span > < span class = "p" > )< / span >
< span class = "p" > {< / span >
< span class = "k" > if< / span > < span class = "p" > (< / span > < span class = "n" > argc< / span > < span class = "o" > ==< / span > < span class = "mi" > 1< / span > < span class = "p" > )< / span > < span class = "p" > {< / span >
< span class = "n" > cout< / span > < span class = "o" > < < < / span > < span class = "s" > "Usage: asino [string] [string] ..."< / span > < span class = "o" > < < < / span > < span class = "n" > endl< / span > < span class = "p" > ;< / span >
< span class = "n" > exit< / span > < span class = "p" > ;< / span >
< span class = "p" > }< / span >
< span class = "k" > for< / span > < span class = "p" > (< / span > < span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "n" > string< / span > < span class = "o" > > ::< / span > < span class = "n" > size_type< / span > < span class = "n" > i< / span > < span class = "o" > =< / span > < span class = "mi" > 1< / span > < span class = "p" > ;< / span > < span class = "n" > i< / span > < span class = "o" > < < / span > < span class = "n" > argc< / span > < span class = "p" > ;< / span > < span class = "o" > ++< / span > < span class = "n" > i< / span > < span class = "p" > )< / span > < span class = "p" > {< / span >
< span class = "n" > lst< / span > < span class = "p" > .< / span > < span class = "n" > push_back< / span > < span class = "p" > (< / span > < span class = "n" > argv< / span > < span class = "p" > [< / span > < span class = "n" > i< / span > < span class = "p" > ]);< / span >
< span class = "p" > }< / span >
< span class = "p" > }< / span >
< span class = "kt" > int< / span > < span class = "nf" > main< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "n" > argc< / span > < span class = "p" > ,< / span > < span class = "kt" > char< / span > < span class = "o" > *< / span > < span class = "n" > argv< / span > < span class = "p" > [])< / span >
< span class = "p" > {< / span >
< span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "n" > string< / span > < span class = "o" > > < / span > < span class = "n" > lst< / span > < span class = "p" > ;< / span >
< span class = "kt" > int< / span > < span class = "n" > poll_ret< / span > < span class = "o" > =< / span > < span class = "n" > poll< / span > < span class = "p" > (< / span > < span class = "o" > & < / span > < span class = "n" > stdin_poll< / span > < span class = "p" > ,< / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span > < span class = "mi" > 0< / span > < span class = "p" > );< / span >
< span class = "k" > if< / span > < span class = "p" > (< / span > < span class = "n" > poll_ret< / span > < span class = "o" > > < / span > < span class = "mi" > 0< / span > < span class = "p" > )< / span > < span class = "p" > {< / span >
< span class = "n" > read_from_piped_input< / span > < span class = "p" > (< / span > < span class = "n" > lst< / span > < span class = "p" > );< / span >
< span class = "p" > }< / span >
< span class = "k" > else< / span > < span class = "p" > {< / span >
< span class = "n" > read_from_arguments< / span > < span class = "p" > (< / span > < span class = "n" > argc< / span > < span class = "p" > ,< / span > < span class = "n" > argv< / span > < span class = "p" > ,< / span > < span class = "n" > lst< / span > < span class = "p" > );< / span >
< span class = "p" > }< / span >
< span class = "n" > random_device< / span > < span class = "n" > rd< / span > < span class = "p" > ;< / span >
< span class = "n" > mt19937< / span > < span class = "n" > m< / span > < span class = "p" > (< / span > < span class = "n" > rd< / span > < span class = "p" > ());< / span >
< span class = "n" > shuffle< / span > < span class = "p" > (< / span > < span class = "n" > lst< / span > < span class = "p" > .< / span > < span class = "n" > begin< / span > < span class = "p" > (),< / span > < span class = "n" > lst< / span > < span class = "p" > .< / span > < span class = "n" > end< / span > < span class = "p" > (),< / span > < span class = "n" > m< / span > < span class = "p" > );< / span >
< span class = "kt" > int< / span > < span class = "n" > i< / span > < span class = "o" > =< / span > < span class = "mi" > 1< / span > < span class = "p" > ;< / span >
< span class = "k" > for< / span > < span class = "p" > (< / span > < span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "n" > string< / span > < span class = "o" > > ::< / span > < span class = "n" > iterator< / span > < span class = "n" > it< / span > < span class = "o" > =< / span > < span class = "n" > lst< / span > < span class = "p" > .< / span > < span class = "n" > begin< / span > < span class = "p" > ();< / span > < span class = "n" > it< / span > < span class = "o" > !=< / span > < span class = "n" > lst< / span > < span class = "p" > .< / span > < span class = "n" > end< / span > < span class = "p" > ();< / span > < span class = "o" > ++< / span > < span class = "n" > it< / span > < span class = "p" > )< / span > < span class = "p" > {< / span >
< span class = "n" > cout< / span > < span class = "o" > < < < / span > < span class = "n" > i< / span > < span class = "o" > ++< / span > < span class = "o" > < < < / span > < span class = "s" > ". "< / span > < span class = "o" > < < < / span > < span class = "o" > *< / span > < span class = "n" > it< / span > < span class = "o" > < < < / span > < span class = "n" > endl< / span > < span class = "p" > ;< / span >
< span class = "p" > }< / span >
< span class = "p" > }< / span > < / code > < / pre > < / figure >
< 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 >
< figure class = "highlight" > < pre > < code class = "language-sh" data-lang = "sh" > ls /media/my_movies/ | buridan< / code > < / pre > < / figure >
< 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 >
< figure class = "highlight" > < pre > < code class = "language-bash" data-lang = "bash" > poll < span class = "o" > (< / span > & stdin_poll, 1, 0< span class = "o" > )< / span > < / code > < / pre > < / figure >
< 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 >
< figure class = "highlight" > < pre > < code class = "language-bash" data-lang = "bash" > g++ -std< span class = "o" > =< / span > c++11 ./program.cpp -o output< / code > < / pre > < / figure >
< 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 >
2017-03-20 00:28:27 +01:00
2016-05-01 11:13:57 +02:00
< / div >
2016-09-17 15:13:02 +02:00
<!-- <div class="related"> -->
<!-- <related - posts /> -->
<!-- <h2>Related Posts</h2> -->
<!-- <ul class="related - posts"> -->
2018-03-27 04:11:48 +02:00
<!-- -->
<!-- <li> -->
<!-- <h3> -->
<!-- <a href="/pescewanda/2018/03/27/reddit/"> -->
<!-- Addio Reddit -->
<!-- <small>27 Mar 2018</small> -->
<!-- </a> -->
<!-- </h3> -->
<!-- </li> -->
2017-10-05 06:35:49 +02:00
<!-- -->
<!-- <li> -->
<!-- <h3> -->
<!-- <a href="/pescewanda/2017/10/02/minidoxguide/"> -->
<!-- Minidox, a guide for the Europeans and the Scrooges -->
<!-- <small>02 Oct 2017</small> -->
<!-- </a> -->
<!-- </h3> -->
<!-- </li> -->
2017-05-10 20:26:52 +02:00
<!-- -->
<!-- <li> -->
<!-- <h3> -->
<!-- <a href="/pescewanda/2017/05/09/vaporwave/"> -->
<!-- Cyber - utopia and vaporwave -->
<!-- <small>09 May 2017</small> -->
<!-- </a> -->
<!-- </h3> -->
<!-- </li> -->
2017-05-07 12:21:36 +02:00
<!-- -->
<!-- <li> -->
<!-- <h3> -->
<!-- <a href="/pescewanda/2017/05/07/latestage_handbrake/"> -->
<!-- Late Stage Capitalism meets FOSS -->
<!-- <small>07 May 2017</small> -->
<!-- </a> -->
<!-- </h3> -->
<!-- </li> -->
2017-03-22 13:07:09 +01:00
<!-- -->
<!-- <li> -->
<!-- <h3> -->
2017-03-22 19:38:23 +01:00
<!-- <a href="/pescewanda/2017/03/20/spazio - digitale - rant - facebook__eng/"> -->
2017-03-22 13:07:09 +01:00
<!-- Some shallow thoughts from my tiny virtual space -->
<!-- <small>20 Mar 2017</small> -->
<!-- </a> -->
<!-- </h3> -->
<!-- </li> -->
2017-02-24 07:32:48 +01:00
<!-- -->
2016-09-17 15:13:02 +02:00
<!-- </ul> -->
<!-- </div> -->
2017-03-22 19:38:23 +01:00
2016-09-17 15:13:02 +02:00
2018-03-27 04:11:48 +02:00
2016-09-17 15:13:02 +02:00
2018-03-27 04:11:48 +02:00
2016-09-17 15:13:02 +02:00
2017-05-10 20:26:52 +02:00
2016-09-17 15:13:02 +02:00
2016-11-18 20:45:39 +01:00
2016-09-17 15:13:02 +02:00
2016-11-19 18:38:14 +01:00
2016-09-17 15:13:02 +02:00
2016-11-18 20:45:39 +01:00
2016-09-17 15:13:02 +02:00
2017-10-05 06:35:49 +02:00
2016-09-17 15:13:02 +02:00
2018-03-27 04:11:48 +02:00
2016-09-17 15:13:02 +02:00
2016-11-18 20:45:39 +01:00
2018-03-27 04:11:48 +02:00
2016-09-17 15:13:02 +02:00
2016-11-19 18:38:14 +01:00
2016-09-17 15:13:02 +02:00
2018-03-27 04:11:48 +02:00
2016-09-17 15:13:02 +02:00
2016-11-18 20:45:39 +01:00
2017-05-10 20:26:52 +02:00
2017-03-22 13:07:09 +01:00
2016-09-17 15:13:02 +02:00
2018-03-27 04:11:48 +02:00
2016-09-17 15:13:02 +02:00
2016-11-18 20:45:39 +01:00
2016-09-17 15:13:02 +02:00
2016-11-18 20:45:39 +01:00
2017-05-07 12:21:36 +02:00
2017-02-24 07:32:48 +01:00
2018-03-27 04:11:48 +02:00
2016-11-18 20:45:39 +01:00
2016-09-17 15:13:02 +02:00
2017-05-10 20:26:52 +02:00
2016-09-17 15:13:02 +02:00
2016-05-01 11:13:57 +02:00
2017-05-09 11:29:54 +02:00
< footer class = "site-footer" >
2017-05-10 20:26:52 +02:00
<!-- <span class="site - footer - owner"><a href="http://francescomecca.eu">Caught in the Net</a> is maintained by <a href="contattami">Francesco Mecca</a>.</span> -->
2017-05-09 11:29:54 +02:00
< span > CC BY-SA 4.0 International.< / br > < / span >
< span class = "site-footer-credits" > < a href = "https://jekyllrb.com" > Jekyll< / a > , < a href = "https://github.com/jasonlong/cayman-theme" > Cayman theme< / a > .< / span >
< / footer >
< / section >
2016-05-01 11:13:57 +02:00
< / body >
< / html >