<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by J Banks on Medium]]></title>
        <description><![CDATA[Stories by J Banks on Medium]]></description>
        <link>https://medium.com/@jjbanks?source=rss-cc515701007e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*dZ3_OXfgHPQXidNLLE8Ddw.jpeg</url>
            <title>Stories by J Banks on Medium</title>
            <link>https://medium.com/@jjbanks?source=rss-cc515701007e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 07:00:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jjbanks/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Visualize your Python prompt engineering with ell-studio]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/software-tidbits/visualize-your-python-prompt-engineering-with-ell-studio-9c871213c387?source=rss-cc515701007e------2"><img src="https://cdn-images-1.medium.com/max/1024/1*GQbzrwiiiFGXV46EbG-oxg.png" width="1024"></a></p><p class="medium-feed-snippet">Building on a previous session, an introduction to ell, in this software tidbit we explore ell&#x2019;s visualization capabilities using&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/software-tidbits/visualize-your-python-prompt-engineering-with-ell-studio-9c871213c387?source=rss-cc515701007e------2">Continue reading on Software Tidbits »</a></p></div>]]></description>
            <link>https://medium.com/software-tidbits/visualize-your-python-prompt-engineering-with-ell-studio-9c871213c387?source=rss-cc515701007e------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c871213c387</guid>
            <category><![CDATA[prompt-engineering]]></category>
            <category><![CDATA[openai]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[J Banks]]></dc:creator>
            <pubDate>Wed, 18 Sep 2024 21:06:18 GMT</pubDate>
            <atom:updated>2024-09-18T21:06:18.041Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Prompt Engineering using ‘ell’ — language model programming lib]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/software-tidbits/prompt-engineering-using-ell-language-model-programming-lib-3658287c96e5?source=rss-cc515701007e------2"><img src="https://cdn-images-1.medium.com/max/1024/1*nnI3I2SP7bmoFOIZ5SsojA.png" width="1024"></a></p><p class="medium-feed-snippet">Ell is a lightweight Python prompt engineering library treating prompts as functions.</p><p class="medium-feed-link"><a href="https://medium.com/software-tidbits/prompt-engineering-using-ell-language-model-programming-lib-3658287c96e5?source=rss-cc515701007e------2">Continue reading on Software Tidbits »</a></p></div>]]></description>
            <link>https://medium.com/software-tidbits/prompt-engineering-using-ell-language-model-programming-lib-3658287c96e5?source=rss-cc515701007e------2</link>
            <guid isPermaLink="false">https://medium.com/p/3658287c96e5</guid>
            <category><![CDATA[openai]]></category>
            <category><![CDATA[prompt-engineering]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[J Banks]]></dc:creator>
            <pubDate>Sun, 15 Sep 2024 14:37:43 GMT</pubDate>
            <atom:updated>2024-09-20T15:01:39.725Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[An Introduction to Mojo]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/software-tidbits/an-introduction-to-mojo-72a383774fd2?source=rss-cc515701007e------2"><img src="https://cdn-images-1.medium.com/max/1024/0*pFdGgZXaWvYPFa75" width="1024"></a></p><p class="medium-feed-snippet">Mojo</p><p class="medium-feed-link"><a href="https://medium.com/software-tidbits/an-introduction-to-mojo-72a383774fd2?source=rss-cc515701007e------2">Continue reading on Software Tidbits »</a></p></div>]]></description>
            <link>https://medium.com/software-tidbits/an-introduction-to-mojo-72a383774fd2?source=rss-cc515701007e------2</link>
            <guid isPermaLink="false">https://medium.com/p/72a383774fd2</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[mojo]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[J Banks]]></dc:creator>
            <pubDate>Wed, 13 Sep 2023 00:22:19 GMT</pubDate>
            <atom:updated>2024-11-08T03:52:17.319Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[ProtoArc 2 in 1 Hub Mouse]]></title>
            <link>https://medium.com/software-tidbits/protoarc-2-in-1-hub-mouse-d2e0bffd5cb5?source=rss-cc515701007e------2</link>
            <guid isPermaLink="false">https://medium.com/p/d2e0bffd5cb5</guid>
            <category><![CDATA[workstation]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[keyboard]]></category>
            <category><![CDATA[mouse]]></category>
            <category><![CDATA[review]]></category>
            <dc:creator><![CDATA[J Banks]]></dc:creator>
            <pubDate>Wed, 23 Aug 2023 01:50:53 GMT</pubDate>
            <atom:updated>2023-10-09T02:32:28.705Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HqTYsRhmSlyLbU560Du1mg.png" /></figure><p>Do you need a full featured mouse that is stylish and isn’t your typical clunky computer pointing device? If so, you should definitely check out the ProtoArc 2-in-1 hub mouse product.</p><h3>Key Features</h3><p>The mouse has a dual mode with three-channels, which is not visibly expected given its slim size. There are two different color options available at current (either dark grey or white) to complement your workspace equipment.</p><p>Powering the device comes from a built-in rechargeable 300mAh battery. Also, packaged with the mouse is a USB-C cable, which is a nice touch. The charge is expected to last for 60 days on a single full charge.</p><p>A pleasant surprise is the mouse includes a compartment that houses a removable expansion hub allowing connection to a computer via a Type-C connector.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*OoY_H-ChAyNRxYKxLoJRAQ.png" /></figure><p>The expansion hub is composed of the following:</p><ul><li>Two USB ports (USB-A 3.0 and quick charge USB-C)</li><li>One 4K HDMI port</li></ul><h3>Usage</h3><p>I used the mouse on a granite surface without a pad and did not notice any skipping or loss of the mouse pointer on the screen. It tracks really well compared to some other mice I’ve used in the past.</p><p>The weight of the mouse doesn’t feel too light or too heavy and will be a great option for travelling given the encapsulated hub.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pyVSfGuDR5YJQrsPlgnIyA.png" /></figure><h3>Conclusion</h3><p>If you want an efficient, compact, and feature packed mouse for everyday use, the 2-in-1 hub model from ProtoArc won’t disappoint. Especially, if you want flexibility for connecting various devices, while having a slim and fully functional mouse. This product comes with a 2-year limited hardware warranty and was less than $80 at time of the review.</p><p>Checkout the ProtoArc products <a href="https://www.protoarc.com/?ref=tNK_YCoCpyRkT9">here</a>.</p><p>#ProtoArc #ProtoArcHUBMouse</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2e0bffd5cb5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/software-tidbits/protoarc-2-in-1-hub-mouse-d2e0bffd5cb5">ProtoArc 2 in 1 Hub Mouse</a> was originally published in <a href="https://medium.com/software-tidbits">Software Tidbits</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ProtoArc Advanced Ergonomic Keyboard and Mouse Combo]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@jjbanks/protoarc-advanced-ergonomic-keyboard-and-mouse-combo-4fe05a864807?source=rss-cc515701007e------2"><img src="https://cdn-images-1.medium.com/max/600/0*xQLaDTpk4G4unDs5" width="600"></a></p><p class="medium-feed-snippet">A review of my recent use of ProtoArc&#x2019;s advanced ergonomic keyboard &amp; mouse combo. Specifically the EKM01 model.</p><p class="medium-feed-link"><a href="https://medium.com/@jjbanks/protoarc-advanced-ergonomic-keyboard-and-mouse-combo-4fe05a864807?source=rss-cc515701007e------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@jjbanks/protoarc-advanced-ergonomic-keyboard-and-mouse-combo-4fe05a864807?source=rss-cc515701007e------2</link>
            <guid isPermaLink="false">https://medium.com/p/4fe05a864807</guid>
            <category><![CDATA[keyboard]]></category>
            <category><![CDATA[ergonomics]]></category>
            <category><![CDATA[office]]></category>
            <category><![CDATA[review]]></category>
            <category><![CDATA[mouse]]></category>
            <dc:creator><![CDATA[J Banks]]></dc:creator>
            <pubDate>Fri, 21 Jul 2023 14:38:15 GMT</pubDate>
            <atom:updated>2023-07-21T14:38:15.132Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Capability Sharing]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@jjbanks/capability-sharing-6239f0dfb4ce?source=rss-cc515701007e------2"><img src="https://cdn-images-1.medium.com/max/612/0*y6xKm55F9Q86-bR-" width="612"></a></p><p class="medium-feed-snippet">With all of the political, technical, and economic challenges many are experiencing in their lives, it&#x2019;s important to take a break from&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@jjbanks/capability-sharing-6239f0dfb4ce?source=rss-cc515701007e------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@jjbanks/capability-sharing-6239f0dfb4ce?source=rss-cc515701007e------2</link>
            <guid isPermaLink="false">https://medium.com/p/6239f0dfb4ce</guid>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[humanity]]></category>
            <category><![CDATA[mentorship]]></category>
            <category><![CDATA[knowledge]]></category>
            <category><![CDATA[training]]></category>
            <dc:creator><![CDATA[J Banks]]></dc:creator>
            <pubDate>Sun, 09 Jul 2023 14:26:59 GMT</pubDate>
            <atom:updated>2023-07-09T14:26:59.426Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[An Introduction to Rust]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/software-tidbits/an-introduction-to-rust-dc68552b0b8a?source=rss-cc515701007e------2"><img src="https://cdn-images-1.medium.com/max/1600/0*yXojevvu6askJSRO" width="1600"></a></p><p class="medium-feed-snippet">Introduction to Rust &#x2014; Software Tidbit Rust Session #1</p><p class="medium-feed-link"><a href="https://medium.com/software-tidbits/an-introduction-to-rust-dc68552b0b8a?source=rss-cc515701007e------2">Continue reading on Software Tidbits »</a></p></div>]]></description>
            <link>https://medium.com/software-tidbits/an-introduction-to-rust-dc68552b0b8a?source=rss-cc515701007e------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc68552b0b8a</guid>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[software]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[J Banks]]></dc:creator>
            <pubDate>Tue, 31 Jan 2023 01:14:57 GMT</pubDate>
            <atom:updated>2024-09-23T01:37:28.057Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Kotlin — Copy to Mutate]]></title>
            <link>https://medium.com/software-tidbits/kotlin-copy-to-mutate-956562753839?source=rss-cc515701007e------2</link>
            <guid isPermaLink="false">https://medium.com/p/956562753839</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[J Banks]]></dc:creator>
            <pubDate>Sun, 21 Jun 2020 16:15:14 GMT</pubDate>
            <atom:updated>2020-06-21T16:15:14.985Z</atom:updated>
            <content:encoded><![CDATA[<h3>Kotlin — Copy to Mutate</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*e9vtUMJQ4Mzvsv6xD6hVBA.jpeg" /></figure><p>In this software tidbit, we examine immutability in Kotlin and how to efficiently work with the immutable object when updates are desired. Immutability is provided out of the box with Kotlin. Compared with Java’s options for immutability Kotlin takes much less effort.</p><p>A common use case with immutable objects is the need to update a small percentage of the object’s state while ensuring everything else is intact. This usually requires that a new instance is created and all of the immutable state is copied over using a constructor or builder.</p><blockquote>With Kotlin’s data classes, a <strong>copy </strong>function is available to handle this state transfer with ease.</blockquote><p>Let’s start with a data class called <strong>DataRecord </strong>that is used to store three values: firstName, lastName, and zip.</p><pre>// Immutable data class definition<br><strong>data class </strong>DataRecord(<br>        <strong>val </strong>firstName: String, <br>        <strong>val </strong>lastName: String, <br>        <strong>val </strong>zip: Int)</pre><p>When creating an instance of the DataRecord, and identifying the instance variables using the <strong>val </strong>keyword, the access to the three fields are restricted as read-only. To make these mutable, the class would need to declare the instance variables using a <strong>var </strong>keyword.</p><p>Kotlin’s data classes provide a built-in <strong>copy </strong>function that comes for free. This function assists with transferring the state from one object to another while being able to specify changes.</p><pre>val originalRecord = DataRecord(&quot;Samuel&quot;, &quot;Davies&quot;, 50322)</pre><pre>val updatedRecord = originalRecord.<strong>copy</strong>(zip = 80322)</pre><p>Above, the constructor is invoked with the original state of the immutable object. An update is desired and only the zip code field’s value is modified. The change is represented by specifying the new value when invoking the <strong>copy </strong>function. In this case <strong>zip=80322</strong>.</p><pre>DataRecord(firstName=Samuel, lastName=Davies, zip=50322)</pre><pre>DataRecord(firstName=Samuel, lastName=Davies, zip=<strong>80322</strong>)</pre><p>But wait, what if there is a need to specify more differences than a single field for the new object? In this case, let’s update the <strong>firstName </strong>and the <strong>zip </strong>from the original record.</p><pre>val updatedRecordOfTwoFields = originalRecord.copy(zip=<strong>55555</strong>,   <br>        firstName=&quot;<strong>Sammy</strong>&quot;)</pre><p>Notice how the order isn’t important as the instance variable name is addressed with its own value. When the object is printed out it results in the the following state.</p><pre>DataRecord(firstName=<strong>Sammy</strong>, lastName=Davies, zip=<strong>55555</strong>)</pre><p>Now, to double check that the state of the other two objects (original and second object) used in the example are not modified, they are printed out (after the final update) just to be sure!</p><pre>// Original instance<br>DataRecord(firstName=Samuel, lastName=Davies, zip=50322)</pre><pre>// First copied instance (Just the zip change)<br>DataRecord(firstName=Samuel, lastName=Davies, zip=80322)</pre><pre>// Final copied instance (first name change and zip change)<br>DataRecord(firstName=Sammy, lastName=Davies, zip=55555)</pre><p>There you have it, <strong>immutable objects</strong> with Kotlin and the built-in <strong>copy </strong>function supplied with Kotlin’s data classes. I hope you enjoyed this <a href="http://softwaretidbits.com/"><strong>software tidbit</strong></a>.</p><p>Checkout the <a href="https://github.com/jeffbanks/kotlin-practices"><strong>shared source</strong></a> and don’t forget to like this if you find it useful in your Kotlin development!</p><p>Until next time …</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=956562753839" width="1" height="1" alt=""><hr><p><a href="https://medium.com/software-tidbits/kotlin-copy-to-mutate-956562753839">Kotlin — Copy to Mutate</a> was originally published in <a href="https://medium.com/software-tidbits">Software Tidbits</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kotlin — Using When]]></title>
            <link>https://medium.com/software-tidbits/kotlin-using-when-2ada35541507?source=rss-cc515701007e------2</link>
            <guid isPermaLink="false">https://medium.com/p/2ada35541507</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software]]></category>
            <dc:creator><![CDATA[J Banks]]></dc:creator>
            <pubDate>Sat, 13 Jun 2020 12:46:01 GMT</pubDate>
            <atom:updated>2020-06-13T14:08:06.402Z</atom:updated>
            <content:encoded><![CDATA[<h3>Kotlin — Using When</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6eeVKqlU8rRIeiJ4WDQCEw.jpeg" /></figure><p>In this software tidbit, we use Kotlin’s <strong>when </strong>keyword to simplify matching of nucleotide codes to ranges, single values, and Kotlin types.</p><h3>What is When?</h3><p>A nice offering of Kotlin is the <strong>when </strong>keyword, which replaces the <strong>switch </strong>operator found in other languages.</p><p>The <strong>when </strong>keyword checks the target argument against branch conditions until it locates a match. Interesting enough, the <strong>when </strong>keyword can be used as an <em>expression </em>or as a <em>statement</em>.</p><p>When being used as an an <strong>expression, </strong>the <strong>when </strong>becomes the value of the satisfied branch. In this case, the <strong>else </strong>branch is required unless all possible cases are covered with branching conditions. Not using an else branch is possible when the compiler can guarantee the <strong>when</strong> always returns a value. Checkout using <strong>when </strong>with Kotlin’s <strong>sealed</strong> or <strong>enum </strong>classes to further enhance its usage.</p><p>If using the <strong>when </strong>keyword as a <strong>statement</strong>, the result is the value of the last expression in the block.</p><h3>Example Usage</h3><p>Below is a scenario where a String value is returned back to the caller when the supplied code(a String type) matches specific value. Here, the <strong>when </strong>keyword is being used as part of an <strong>expression</strong>.</p><pre>fun nucleotideName(code: String?): String = <strong>when </strong>(code) {<br>    null -&gt; throw IllegalArgumentException(&quot;code supplied is null&quot;)<br>    &quot;T&quot; -&gt; &quot;thymine&quot;<br>    &quot;A&quot; -&gt; &quot;adenine&quot;<br>    &quot;C&quot; -&gt; &quot;cytosine&quot;<br>    &quot;G&quot; -&gt; &quot;guanine&quot;<br>    else -&gt; &quot;unknown code supplied: $code&quot;<br>}</pre><p>Notice how the input code is allowed to be null in the example above. In Kotlin, this input can be protected by removing the ‘?’, negates the need for a null check in the <strong>when expression</strong>.</p><pre>fun nucleotideNameCodeNotNull(code: String): String = <strong>when </strong>(code) {<br>    &quot;T&quot; -&gt; &quot;thymine&quot;<br>    &quot;A&quot; -&gt; &quot;adenine&quot;<br>    &quot;C&quot; -&gt; &quot;cytosine&quot;<br>    &quot;G&quot; -&gt; &quot;guanine&quot;<br>    else -&gt; &quot;unknown code supplied: $code&quot;<br>}</pre><p>Nice, but what if we wanted to match a branch <strong>based on a range</strong> instead of a single letter? Let’s see how Kotlin handles this with another <strong>when </strong>expression handling pH value ranges.</p><p>Here, the input is an <strong>Int </strong>value and if the value is found within a range, the proper pH type is returned as a String.</p><pre>fun phType(value: Int?) : String = <strong>when </strong>(value) {<br>    null -&gt; throw IllegalArgumentException(&quot;value supplied is null&quot;)<br>    in 0..6 -&gt; &quot;acidic&quot;<br>    in 7..7 -&gt; &quot;neutral&quot;<br>    in 8..14 -&gt; &quot;alkaline&quot;<br>    else -&gt; &quot;out of range detected: $value&quot;<br>}</pre><blockquote>A single String value is still returned, however the value passed in can be checked for a match <strong>within a range of Int values</strong>.</blockquote><p>Below is an example usage of <strong>when, </strong>used as a statement within a function. Notice how the <strong>when </strong>keyword is no longer on the right side of an equals sign!</p><pre>fun phTypeWhenStatement(value: Int?) {<br>    <strong>when </strong>(value) {<br>        null -&gt; throw IllegalArgumentException(&quot;value supplied is null&quot;)<br>        in 0..6 -&gt; <em>println</em>(&quot;acidic&quot;)<br>        in 7..7 -&gt; <em>println</em>(&quot;neutral&quot;)<br>        in 8..14 -&gt; <em>println</em>(&quot;alkaline&quot;)<br>    }<br>}</pre><p>In this case, the pH types are simply printed with no return String. Also, there is no requirement for an else statement as none of the branches need to match. However, for sake of defensive coding, one should be included for announcing an “out of range” value.</p><h3>Using With and Is</h3><p>The final example shows how flexible this becomes when having to support more complicated use cases. The logic has to support pH value inputs of varying types: <strong>String, Int, Long, Float, and Double</strong>. When a String is supplied, it is recommended to put checks around the provided value as it could be anything from an actual number to be converted or a non-convertible value (e.g. “foobar123”).</p><p>Instead of making the <strong>phType</strong>() function handle this complexity, a new function is created called <strong>normalizePhValueType</strong>(), which returns the String value representing the pH type and accepts any input type (using Kotlin’s <strong>Any </strong>type) with the job of normalizing to an <strong>Int </strong>value.</p><pre>fun normalizePhValueType(value: Any?): String? = <strong>when </strong>(value) {<br>    <br>    null -&gt; throw IllegalArgumentException(&quot;value supplied is null <br>        or not able to be normalized to number&quot;)<br>    <br>    is <strong>String </strong>-&gt; {<br>        val stringToValue: Any? =<br>            value.<em>toDoubleOrNull</em>()<br>                ?: value.<em>toFloatOrNull</em>()<br>                ?: value.<em>toIntOrNull</em>()<br>        <strong>normalizePhValueType</strong>(stringToValue)<br>    }<br>    is <strong>Int </strong>-&gt; phType(value)<br>    is <strong>Float </strong>-&gt; phType(value.<em>roundToInt</em>())<br>    is <strong>Double </strong>-&gt; phType(value.<em>roundToInt</em>())<br>    is <strong>Long </strong>-&gt; phType(value.toInt())</pre><pre>    else -&gt; throw java.lang.IllegalArgumentException(&quot;pH value&#39;s   <br>       type can&#39;t be converted&quot;)<br>}</pre><p>The normalized value is used as the input to the <strong>phType</strong>(value:Int?) function, where the result is returned to the caller.</p><blockquote>Notice how when the String type is handled, <strong>recursion </strong>is used to invoke the same function currently executing. This, in turn, will handle the values for null (or garbage), Int, Float, Double, or Long.</blockquote><p>There you have it, uses of <strong>when </strong>with Kotlin. I hope you enjoyed this <a href="http://softwaretidbits.com/">software tidbit</a>. Checkout the <a href="https://github.com/jeffbanks/kotlin-practices">shared source</a> and don’t forget to like this if you find it useful in your Kotlin development!</p><p>Until next time …</p><h3>References</h3><p><a href="https://steemit.com/health/@paulspujats/how-does-ph-level-affect-body-s-immune-response">Immune system responses and pH levels</a></p><p><a href="https://knowgenetics.org/nucleotides-and-bases/">Nucleotides</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2ada35541507" width="1" height="1" alt=""><hr><p><a href="https://medium.com/software-tidbits/kotlin-using-when-2ada35541507">Kotlin — Using When</a> was originally published in <a href="https://medium.com/software-tidbits">Software Tidbits</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kotlin — Try/Catch as Expression]]></title>
            <link>https://medium.com/software-tidbits/kotlin-try-catch-as-expression-dbc5d70976d9?source=rss-cc515701007e------2</link>
            <guid isPermaLink="false">https://medium.com/p/dbc5d70976d9</guid>
            <category><![CDATA[software]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[J Banks]]></dc:creator>
            <pubDate>Sat, 13 Jun 2020 02:29:05 GMT</pubDate>
            <atom:updated>2020-06-13T02:29:05.493Z</atom:updated>
            <content:encoded><![CDATA[<h3>Kotlin — Try/Catch as Expression</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*UQ7IK16_ngkEAd_cKnUo2w.jpeg" /><figcaption>Try &amp; Catch</figcaption></figure><p>In this software tidbit, we use Kotlin’s try/catch capabilities to perform a type conversion for variables representing numbers. The ability to use try/catch as an expression makes for code that is defensive and clean.</p><h3>Problem</h3><p>We need to convert a String value received from a file record into an Int value. For this example, the requirement is to invoke a method included with a third-party library. The API throws a checked <strong>NumberFormatException </strong>when the value supplied can’t be parsed to a number.</p><p>This exception is documented with the API, so we want to handle and provide a default value when the constant can’t be converted. A naive approach is to create a default value and if the exception occurs, log it, and move forward with the calculation.</p><pre>var constantFromFile = 1</pre><pre><strong>try </strong>{<br>    constantFromFile = Integer.valueOf(getConstantFromFile())<br>} <strong>catch </strong>(ex: NumberFormatException) {<br>    <em>println</em>(&quot;Can&#39;t convert to number, defaulting to 1!&quot;)<br>}<br><br>val calculation = 100 * constantFromFile</pre><p>This approach solves the problem, but over time could cause subtle defects that are hard to locate and test. Imagine having more lines of code added and an unintended modification is made to the constant variable’s initialized value.</p><pre>var constantFromFile = 123</pre><pre>/* Lots of other code added over time */</pre><pre><strong>try </strong>{<br>    constantFromFile = Integer.valueOf(getConstantFromFile())<br>} <strong>catch </strong>(ex: NumberFormatException) {<br>    <em>println</em>(&quot;Can&#39;t convert to number, defaulting to 1!&quot;)<br>}<br><br>val calculation = 100 * constantFromFile</pre><blockquote>This is where having a more direct way of handling the exception and assigning the value of the constant would be handy.</blockquote><h3>Solution</h3><p>In Kotlin, a <strong>try/catch</strong> can simply be used as an <strong>expression</strong>, so instead of throwing the exception back up the chain or relying on a previously set value, <strong>handle it with a Kotlin expression and an immutable variable</strong>.</p><p>Here we detect the <strong>NumberFormatException </strong>for the provided String value and default its value to 1 if the exception occurs.</p><pre>fun usingTryCatchAsExpression(inputValue: String): Number {<br>    <strong>return try </strong>{<br>        return invokeLibWith(inputValue)<br>    } <strong>catch </strong>(ex: NumberFormatException) {<br>        1<br>    }<br>}</pre><p>Using the try/catch as an expression allows you to maintain the conversion in one place with the side-effects of easier comprehension and easier testing.</p><h3>Running</h3><p>The results of the try/catch as an expression can be illustrated when invoking the usingTryCatchExpression function passing in different immutable constant values. First, a string with value “1000” followed by a string with value “abc123”.</p><pre>val app = TryCatch()<br><br>val firstConstant = &quot;1000&quot;<br>val firstReturn = app.<strong>usingTryCatchAsExpression</strong>(firstConstant)<br><em>println</em>(&quot;First Run: $firstReturn&quot;)<br><br>val secondConstant = &quot;abc123&quot;<br>val secondReturn = app.<strong>usingTryCatchAsExpression</strong>(secondConstant)<br><em>println</em>(&quot;Second Run: $secondReturn&quot;)</pre><pre><strong>Output</strong>:</pre><pre>First Run: 1000<br>Second Run: 1</pre><p>There you have it <strong>try/catch</strong> used as an expression with Kotlin. I hope you enjoyed this <a href="http://softwaretidbits.com/">software tidbit</a>. Checkout the <a href="https://github.com/jeffbanks/kotlin-practices">shared source</a> and don’t forget to like this if you find it useful in your Kotlin development!</p><p>Until next time …</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dbc5d70976d9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/software-tidbits/kotlin-try-catch-as-expression-dbc5d70976d9">Kotlin — Try/Catch as Expression</a> was originally published in <a href="https://medium.com/software-tidbits">Software Tidbits</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>