<?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>DEV Community: bbrtj</title>
    <description>The latest articles on DEV Community by bbrtj (@bbrtj).</description>
    <link>https://dev.to/bbrtj</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%2F566367%2F487e0bf2-d8ca-40ef-94d4-f818d9ff86b5.png</url>
      <title>DEV Community: bbrtj</title>
      <link>https://dev.to/bbrtj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bbrtj"/>
    <language>en</language>
    <item>
      <title>Thunderhorse beta released!</title>
      <dc:creator>bbrtj</dc:creator>
      <pubDate>Sat, 17 Jan 2026 17:02:04 +0000</pubDate>
      <link>https://dev.to/bbrtj/thunderhorse-beta-released-17jk</link>
      <guid>https://dev.to/bbrtj/thunderhorse-beta-released-17jk</guid>
      <description>&lt;p&gt;Thunderhorse framework was just released as beta. Read more about it on my blog: &lt;a href="https://bbrtj.eu/blog/article/thunderhorse-beta-released" rel="noopener noreferrer"&gt;https://bbrtj.eu/blog/article/thunderhorse-beta-released&lt;/a&gt;&lt;/p&gt;

</description>
      <category>perl</category>
      <category>webdev</category>
      <category>thunderhorse</category>
    </item>
    <item>
      <title>Shuffling my playlist with Perl and quantum mechanics</title>
      <dc:creator>bbrtj</dc:creator>
      <pubDate>Fri, 05 Feb 2021 09:40:24 +0000</pubDate>
      <link>https://dev.to/bbrtj/shuffling-my-playlist-with-perl-and-quantum-mechanics-4e0f</link>
      <guid>https://dev.to/bbrtj/shuffling-my-playlist-with-perl-and-quantum-mechanics-4e0f</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;There are handful of reasons one might want to set up their own radio. A common playlist for a group of friends is one of them. Another one is having a fine-grained control over the logic behind the shuffling mechanism. Thankfully, setting up your own radio is easy now with &lt;a href="https://metacpan.org/pod/Audio%3A%3AStreamGenerator"&gt;Audio::StreamGenerator&lt;/a&gt; Perl module - and you get to utilize all the other great modules written for the language.&lt;/p&gt;

&lt;p&gt;The module I'm using for shuffling is not great at all in terms of popularity, but it is my own creation and I'm having a lot of fun replacing some tedious random number generation and array operations with it. The module takes some general laws of quantum physics - the phenomenon of a state of superposition, to be exact - and applies them to Perl variables. I'm not a physicist at all so don't expect it to be very accurate - the goal here is to allow for some very neat operations right in the code.&lt;/p&gt;

&lt;p&gt;The normal usage of the module is creating a superposition of values - an object that tries to act as if it had all these values at once, like an array on steroids - and then collapsing it to get one of the states randomly. It makes choosing a random element out of a group of elements very easy, and is more declarative way of doing it, rather than imperative. Of course, it is one of the less exciting features of the module - I will talk about more exciting stuff a bit later.&lt;/p&gt;

&lt;p&gt;The module is called &lt;a href="https://metacpan.org/pod/Quantum%3A%3ASuperpositions%3A%3ALazy"&gt;Quantum::Superpositions::Lazy&lt;/a&gt; (that's a fairly long name, so we'll call it by its alias - Q::S::L) and is more or less a rewrite of a very old &lt;a href="https://metacpan.org/pod/Quantum%3A%3ASuperpositions"&gt;Quantum::Superpositions&lt;/a&gt; module by Damian Conway - a famous Perl programmer. I decided to develop a new module not only because the old one was no longer developed, but also because it was lacking some crucial features that I was interested in, like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It was not able assign to weights to states - all states had the same chance of occuring.&lt;/li&gt;
&lt;li&gt;It always kept all states and performed all the operations right away. Hence the &lt;em&gt;::Lazy&lt;/em&gt; part of my module, as it tries to perform operations as late as possible.&lt;/li&gt;
&lt;li&gt;It contained a lot of black magic, and was (in my opinion) a bit hard to handle due to that.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Setting up a playlist
&lt;/h1&gt;

&lt;p&gt;A playlist will be just a filesystem directory with subdirectories categorizing the tracks, which are simply stored as &lt;em&gt;mp3&lt;/em&gt; files. Each category (or genre) will have a different chance of getting selected for the next song. To keep randomness in check, the same track from a category cannot be played before all tracks from the same category were played. This creates a rare situation where a track can be played twice in a row if it is selected as the last one and then the first one from a category - fixing it would require an additional condition, but the situation should be rare enough that we're not going to worry about it.&lt;/p&gt;

&lt;p&gt;Since categories will be played based on a fixed chance - no matter how many songs are in them - the playlist must be hand crafted to meet one's needs. Assigning a high chance to a small category will cause the songs from it to have a very high chance of getting selected. Lets look at an example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;playlist
| - category_a
    | - song_1
    | - song_2
| - category_b
    | - song_3
    | - song_4
    | - song_5
    | - song_6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If we set weight of &lt;em&gt;category_a&lt;/em&gt; to be &lt;code&gt;10&lt;/code&gt; and the weight of &lt;em&gt;category_b&lt;/em&gt; to &lt;code&gt;5&lt;/code&gt;, the chances of songs playing will be as follows:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;weight sum: 15

category_a, songs: 2, weight: 10
    song chance: (10 / 15) / 2 * 100% = 33.(3)%
category_a, songs: 4, weight: 5
    song chance: (5 / 15) / 4 * 100% = 8.(3)%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Each individual song from &lt;em&gt;category_a&lt;/em&gt; will be played four times as often as each individual song from &lt;em&gt;category_b&lt;/em&gt;, but a careless programmer may have thought that they will only be twice as frequent. All in all, categories will be played exactly how was specified, but one will feel that they listen to only two songs most of the time.&lt;/p&gt;

&lt;h1&gt;
  
  
  Quantum-like shuffling
&lt;/h1&gt;

&lt;p&gt;Now that we have a plan for a playlist, it is time to write code that will shuffle it. For this short presentation, we will only implement the &lt;code&gt;get_next_file&lt;/code&gt; function, which will return the name of a file that got selected. If you want to see the code in full or without my interruptions, please refer to &lt;a href="https://github.com/brtastic/perl-radio/blob/master/radio.pl"&gt;the file in my Github repository&lt;/a&gt;. Code between the repository and this article may differ a little, since I try to make it more readable here.&lt;/p&gt;

&lt;p&gt;Lets start with pragmas, imports and common variables that are required:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight perl"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nv"&gt;v5&lt;/span&gt;&lt;span class="mf"&gt;.24&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nv"&gt;warnings&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;Q::S::&lt;/span&gt;&lt;span class="nv"&gt;L&lt;/span&gt; &lt;span class="sx"&gt;qw(:all)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;Path::&lt;/span&gt;&lt;span class="nv"&gt;Tiny&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;# current directory&lt;/span&gt;
&lt;span class="k"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;$current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;__FILE__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;dirname&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;# configuration&lt;/span&gt;
&lt;span class="k"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;$config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;genres&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;category_a&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;category_b&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function will keep some state with the &lt;code&gt;state&lt;/code&gt; Perl keyword, which causes a variable to only be initiazed on the first function call and to keep the state throughout the calls. We also define a superposition with &lt;code&gt;my $shuffle&lt;/code&gt; and then call &lt;code&gt;-&amp;gt;collapse&lt;/code&gt; on it to get a single state randomly. Before returning the result, we also push the randomed state to the &lt;code&gt;$last&lt;/code&gt; array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight perl"&gt;&lt;code&gt;&lt;span class="k"&gt;sub &lt;/span&gt;&lt;span class="nf"&gt;get_next_file&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;state&lt;/span&gt; &lt;span class="nv"&gt;$last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;# only initialized once&lt;/span&gt;

    &lt;span class="k"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;$shuffle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="k"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;$choice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$shuffle&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;collapse&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;push&lt;/span&gt; &lt;span class="nv"&gt;@$last&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$choice&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$choice&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now to implement the &lt;code&gt;...&lt;/code&gt; part, we need to create a superposition that will read all the categories and tracks in each of them. We will use the category (genre) config defined above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight perl"&gt;&lt;code&gt;&lt;span class="k"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;@arr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;$last_files&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;superpos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$last&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;$genre&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;keys&lt;/span&gt; &lt;span class="nv"&gt;$config&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;genres&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;%*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;@all_files&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;glob&lt;/span&gt; &lt;span class="p"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$current&lt;/span&gt;&lt;span class="s2"&gt;/radio/&lt;/span&gt;&lt;span class="si"&gt;$genre&lt;/span&gt;&lt;span class="s2"&gt;/*.mp3&lt;/span&gt;&lt;span class="p"&gt;";&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;$last_files&lt;/code&gt; is a superposition that will be used to obtain a list of songs that have not yet been played this turn. &lt;code&gt;@all_files&lt;/code&gt; should contain a list of all the &lt;em&gt;mp3&lt;/em&gt; files within the category. We can now create a narrowed list of files for the category:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight perl"&gt;&lt;code&gt;&lt;span class="k"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;$pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;superpos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;@all_files&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;fetch_matches&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;every_state&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;$pos&lt;/span&gt; &lt;span class="ow"&gt;ne&lt;/span&gt; &lt;span class="nv"&gt;$last_files&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay, this one is much more complex, so lets spend a little while here.&lt;/p&gt;

&lt;p&gt;In the inner block, we compare two superpositions with &lt;code&gt;ne&lt;/code&gt;, which in Perl means not equal when comparing as strings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight perl"&gt;&lt;code&gt;&lt;span class="nv"&gt;$pos&lt;/span&gt; &lt;span class="ow"&gt;ne&lt;/span&gt; &lt;span class="nv"&gt;$last_files&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This causes every state of &lt;code&gt;$pos&lt;/code&gt; to be compared with &lt;code&gt;ne&lt;/code&gt; against every state of &lt;code&gt;$last_files&lt;/code&gt;. If &lt;code&gt;$pos&lt;/code&gt; contains &lt;code&gt;[track1, track2, track3]&lt;/code&gt; and &lt;code&gt;$last_files&lt;/code&gt; contain &lt;code&gt;[track1, track3]&lt;/code&gt;, then the following comparisons will be performed:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;track1 ne track1
track1 ne track3
track2 ne track1
track2 ne track3
track3 ne track1
track3 ne track3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, to obtain a result from these comparisons, we need to join them somehow. I call this a reduction strategy, and there are currently three of them - &lt;em&gt;"any"&lt;/em&gt;, &lt;em&gt;"all"&lt;/em&gt; and &lt;em&gt;"one"&lt;/em&gt;. By default, &lt;em&gt;"any"&lt;/em&gt; strategy is used, which would create the following operation:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;track1 ne track1 ||
track1 ne track3 ||
track2 ne track1 ||
track2 ne track3 ||
track3 ne track1 ||
track3 ne track3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Can you see the problem here?&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;ne&lt;/code&gt; with &lt;em&gt;"any"&lt;/em&gt; strategy is often a mistake. It will work if both superpositions have a single element, but if we have two in either of them, it will always be true that one is not equal to the other. &lt;code&gt;track1 ne track3&lt;/code&gt; is enough to cause the entire operation to return true, even though an &lt;code&gt;eq&lt;/code&gt; operation (which is the opposite of &lt;code&gt;ne&lt;/code&gt;) will also return true. I'm considering introducing an exception to the &lt;em&gt;"any by default"&lt;/em&gt; rule in the module, but for now I have decided to keep it as it is.&lt;/p&gt;

&lt;p&gt;Thankfully we can control the reduction strategy by wrapping the operation in a function call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight perl"&gt;&lt;code&gt;&lt;span class="nv"&gt;every_state&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;$pos&lt;/span&gt; &lt;span class="ow"&gt;ne&lt;/span&gt; &lt;span class="nv"&gt;$last_files&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;em&gt;"all"&lt;/em&gt; strategy will be used, which will produce the following operation, which is correct:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;track1 ne track1 &amp;amp;&amp;amp;
track1 ne track3 &amp;amp;&amp;amp;
track2 ne track1 &amp;amp;&amp;amp;
track2 ne track3 &amp;amp;&amp;amp;
track3 ne track1 &amp;amp;&amp;amp;
track3 ne track3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, lets move on to the next wrapper, which is &lt;code&gt;fetch_states&lt;/code&gt;. Normally, &lt;code&gt;ne&lt;/code&gt; operation between two superpositions would return a boolean value. In this case we don't want a boolean value, but we'd rather like to know which states from the first superposition meet the &lt;code&gt;ne&lt;/code&gt; condition.&lt;/p&gt;

&lt;p&gt;This is exactly what &lt;code&gt;fetch_states&lt;/code&gt; is doing. A return value of it is a new superposition filled with states that matched. For a state to be included, it requires to meet the condition like the above, but a bit different:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;track1 will be included if:
    track1 ne track1 &amp;amp;&amp;amp;
    track1 ne track3

track2 will be included if:
    track2 ne track1 &amp;amp;&amp;amp;
    track2 ne track3

track3 will be included if:
    track3 ne track1 &amp;amp;&amp;amp;
    track3 ne track3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;From this example, only &lt;code&gt;track2&lt;/code&gt; will be included, which is what was expected.&lt;/p&gt;

&lt;p&gt;With that out of the way, lets come back to the creation of &lt;code&gt;$shuffle&lt;/code&gt; superposition. At this stage, it should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight perl"&gt;&lt;code&gt;&lt;span class="k"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;@arr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;$last_files&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;superpos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$last&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;$genre&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;keys&lt;/span&gt; &lt;span class="nv"&gt;$config&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;genres&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;%*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;@all_files&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;glob&lt;/span&gt; &lt;span class="p"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$current&lt;/span&gt;&lt;span class="s2"&gt;/radio/&lt;/span&gt;&lt;span class="si"&gt;$genre&lt;/span&gt;&lt;span class="s2"&gt;/*.mp3&lt;/span&gt;&lt;span class="p"&gt;";&lt;/span&gt;

    &lt;span class="k"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;$pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;superpos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;@all_files&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nv"&gt;$pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;fetch_matches&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;every_state&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;$pos&lt;/span&gt; &lt;span class="ow"&gt;ne&lt;/span&gt; &lt;span class="nv"&gt;$last_files&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;$pos&lt;/code&gt; superposition for the current genre is more or less ready to be added to &lt;code&gt;@arr&lt;/code&gt; which holds the states of the entire superposition, but we must first take care of clearing &lt;code&gt;$last&lt;/code&gt; of files if all tracks were already played. This code will take care of that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight perl"&gt;&lt;code&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nv"&gt;$pos&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;states&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;superpos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;@all_files&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nv"&gt;$last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;every_state&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="vg"&gt;$_&lt;/span&gt; &lt;span class="ow"&gt;ne&lt;/span&gt; &lt;span class="nv"&gt;$pos&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nv"&gt;$last&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;-&amp;gt;states&lt;/code&gt; method returns a complete list of states for the superposition. If this is empty, we create it anew from the files found and remove all the files in this category from the last played files.&lt;/p&gt;

&lt;p&gt;The last step is to populate the &lt;code&gt;@arr&lt;/code&gt; array, and create a new superposition out of it outside the loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight perl"&gt;&lt;code&gt;    &lt;span class="nb"&gt;push&lt;/span&gt; &lt;span class="nv"&gt;@arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$config&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;genres&lt;/span&gt;&lt;span class="p"&gt;}{&lt;/span&gt;&lt;span class="nv"&gt;$genre&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nv"&gt;$pos&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nv"&gt;superpos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;\&lt;/span&gt;&lt;span class="nv"&gt;@arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Populating &lt;code&gt;@arr&lt;/code&gt; is done with an array reference, which contains two elements. The first one is the weight of the state, and the second one is value, in this case the superposition we just created. The module is smart enough to collapse positions recursively, so collapsing the parent superposition will also collapse the nested superpositions.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;With relatively short code we have achieved, in my opinion, quite a complex shuffling mechanism which includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pure randomness within categories&lt;/li&gt;
&lt;li&gt;categories selected according to their weight&lt;/li&gt;
&lt;li&gt;playback history and avoiding duplications until all tracks are played&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Capabilities of the module does not end here. It contains a built in statistics module, which can provide information on most probable states, median state and even expected deviation (if the data is numeric). It can also mutate superpositions with mathematical operations, string operations and even a custom, user-specified function. I am also actively developing it, so it should get better with every release.&lt;/p&gt;

&lt;p&gt;(this article is also available on &lt;a href="https://bbrtj.eu/blog/article/shuffling-playlist"&gt;my personal blog&lt;/a&gt;)&lt;/p&gt;

</description>
      <category>programming</category>
      <category>perl</category>
      <category>music</category>
    </item>
  </channel>
</rss>
