<?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: Xliff</title>
    <description>The latest articles on DEV Community by Xliff (@xliff).</description>
    <link>https://dev.to/xliff</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%2F1070250%2Ffd15afc8-562a-48f2-baf8-31726c598d5a.png</url>
      <title>DEV Community: Xliff</title>
      <link>https://dev.to/xliff</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xliff"/>
    <language>en</language>
    <item>
      <title>Serializing Basic Types to XML</title>
      <dc:creator>Xliff</dc:creator>
      <pubDate>Sun, 02 Feb 2025 11:15:51 +0000</pubDate>
      <link>https://dev.to/xliff/serializing-basic-types-to-xml-4bl2</link>
      <guid>https://dev.to/xliff/serializing-basic-types-to-xml-4bl2</guid>
      <description>&lt;p&gt;While I am aware that for serializing data, JSON is the lay of the land. However, in the name of completeness, I realize that XML is not quite dead. Far to the contrary XML is still used, particularly when serializing classes. &lt;/p&gt;

&lt;p&gt;We have XML::Class, which covers most use-cases, however I was shocked to realize that the basic &lt;strong&gt;Cool&lt;/strong&gt; type was left in the cold.&lt;/p&gt;

&lt;p&gt;To this effect I've knocked together the following. Please share your thoughts, comments, additions and &lt;em&gt;constructive&lt;/em&gt; thoughts in the comments section!&lt;/p&gt;

&lt;p&gt;Now, to the code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sub xml ($_, :$value) {
    when Hash {
      for .pairs {
        "&amp;lt;key&amp;gt;{ .key }&amp;lt;/key&amp;gt;" ~
        "&amp;lt;value&amp;gt;{ xml( .value, :!value ) }&amp;lt;/value&amp;gt;"
      }
    }

    when Array {
      "&amp;lt;array&amp;gt;{ .map({ xml($_) }).join }&amp;lt;/array&amp;gt;"
    }

    when Str {
      [~](
        $value ?? '&amp;lt;value&amp;gt;' !! '',
        '"$_"',
        $value ?? '&amp;lt;/value&amp;gt;' !! ''
      );
    }

    when Numeric {
      [~](
        $value ?? '&amp;lt;value&amp;gt;' !! '',
        '$_',
        $value ?? '&amp;lt;/value&amp;gt;' !! ''
      )
    }

    default {
      "&amp;lt;warning&amp;gt;Unhandle-able type { .^name }&amp;lt;/warning&amp;gt;"
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is just the first draft. I suspect I can add in a section for XML::Class objects in the very near future.&lt;/p&gt;

&lt;p&gt;Also, the code is untested. Please mention all bugs and they will be fixed.&lt;/p&gt;

</description>
      <category>raku</category>
      <category>rakulang</category>
      <category>xml</category>
    </item>
    <item>
      <title>Using a Supply to Track the Number of Items in a Box</title>
      <dc:creator>Xliff</dc:creator>
      <pubDate>Sun, 23 Apr 2023 16:06:46 +0000</pubDate>
      <link>https://dev.to/xliff/using-a-supply-to-track-the-number-of-items-in-a-box-3d1b</link>
      <guid>https://dev.to/xliff/using-a-supply-to-track-the-number-of-items-in-a-box-3d1b</guid>
      <description>&lt;p&gt;It all starts with a box. Every so often the box has a certain number of things put into it. What would be the best way to track the number of items in the box with Raku?&lt;/p&gt;

&lt;p&gt;The obvious answer would be to use &lt;code&gt;$*SCHEDULER.cue&lt;/code&gt;, wouldn't it? But what happens when I have another box with another set of things that are put into it.&lt;/p&gt;

&lt;p&gt;The other option is &lt;code&gt;Supply.interval&lt;/code&gt;, which would work great if the number of items at each interval is 1... which it isn't.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introducing Supply.interval-with-value
&lt;/h2&gt;

&lt;p&gt;Which takes the normal .interval, and adds a &lt;code&gt;value&lt;/code&gt; parameter to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class IntervalValue does Tappable {
    has $!scheduler;
    has $!interval;
    has $!delay;
    has $!value;

    submethod BUILD(
        :$!scheduler, 
        :$!interval, 
        :$!value,
        :$!delay 
        --&amp;gt; Nil
    ) { }

    method tap(&amp;amp;emit, &amp;amp;, &amp;amp;, &amp;amp;tap) {
        my $i = 0;
        my $lock = Lock::Async.new;
        $lock.protect: {
            my $cancellation = $!scheduler.cue(
                {
                    CATCH { 
                        $cancellation.cancel if $cancellation
                    }

                    $lock.protect: { 
                        emit [ $!value, $i++ ] 
                    };
                }, 
                :every($!interval), 
                :in($!delay)
             );
             my $t = Tap.new({ $cancellation.cancel });
             tap($t);
             $t
        }
    }

    method live(--&amp;gt; False) { }
    method sane(--&amp;gt; True) { }
    method serial(--&amp;gt; True) { }
}

use MONKEY-TYPING;

augment class Supply {
  method interval-with-value(
    Supply:U: 
      $interval, 
      $value, 
      $delay = 0, 
      :$scheduler = $*SCHEDULER
  ) {
    Supply.new(
        IntervalValue.new(
            :$interval, 
            :$delay, 
            :$value, 
            :$scheduler
        )
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not one to throw away data, this supply has two parameters. The first parameter is the &lt;code&gt;value&lt;/code&gt;, which is the amount of items going into the box. The last parameter is the &lt;code&gt;count&lt;/code&gt;, which is the same as the original &lt;code&gt;Supply.interval&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So doing the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Supply.interval-with-value(2, 30).tap( -&amp;gt; *@a {
  say "Value: { @a.head } / Count { @a.tail.succ }";
});
sleep 10;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yields the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Value: 30 / Count: 1
Value: 30 / Count: 2
Value: 30 / Count: 3
Value: 30 / Count: 4
Value: 30 / Count: 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This now works a treat!&lt;/p&gt;

&lt;p&gt;Of course... what happens when one needs &lt;code&gt;value&lt;/code&gt; to change? Do we need to refactor?&lt;/p&gt;

&lt;p&gt;Well.... not really.&lt;/p&gt;

&lt;p&gt;You see...&lt;code&gt;value&lt;/code&gt; is untyped.&lt;/p&gt;

&lt;p&gt;So it can be &lt;em&gt;anything&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Like a closure!&lt;/p&gt;

&lt;p&gt;So something like this would work just fine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my $scalable = sub { 30 * $bonus };

Promise.in(5).then({ 
  $bonus = 2;
  say "Scalable is now { $scalable() }";
});

Supply.interval-with-value(2, $scalable).tap( -&amp;gt; *@a {
  say "Value: { @a.head.() } / Count { @a.tail.succ }";
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running the above code produces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Value: 30 / Count: 1
Value: 30 / Count: 2
Scalable is now 60
Value: 60 / Count: 3
Value: 60 / Count: 4
Value: 60 / Count: 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which works just fine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Putting it all together...
&lt;/h2&gt;

&lt;p&gt;So I now have everything I need to handle my box counting.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Box {
  has $.count is rw = 0;
}

my $b = Box.new;

my $items = 30;
my $counter = sub { $items };

Supply.interval-with-value(2, $counter).tap( -&amp;gt; *@a {
  my $delta = @a.head.();
  say "Count += { $delta }";
  $b.count += $delta;
});

# Introduce randomness by changing count 4 times
for ^4 {
  Promise.in($_ * 2).then({
    my $delta = (-5..5).grep( *.so ).pick;
    say "Adjusting item count by $delta";
    $items +=  $delta;
  });
}
sleep 15;

say "Final count: { $b.count }";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running this new code, I get the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Count += 30
Adjusting item count by 2
Count += 32
Adjusting item count by -3
Count += 29
Adjusting item count by -5
Count += 24
Adjusting item count by -2
Count += 22
Count += 22
Count += 22
Count += 22
Final count: 203
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which is...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;raku -e 'say 88 + 24 + 29 + 32 + 30'
203
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...correct!&lt;/p&gt;

&lt;p&gt;So this is one way to increment box by an arbitrary delta at a specific interval. Here's hoping this helps you solve one of your mysterious use cases one day. &lt;/p&gt;

</description>
      <category>rakulang</category>
      <category>supply</category>
    </item>
  </channel>
</rss>
