<?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: Nick Heriana-Keers</title>
    <description>The latest articles on DEV Community by Nick Heriana-Keers (@nickkeers).</description>
    <link>https://dev.to/nickkeers</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%2F67248%2F59adbc34-1915-4dc2-b137-6565cabe0e89.png</url>
      <title>DEV Community: Nick Heriana-Keers</title>
      <link>https://dev.to/nickkeers</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nickkeers"/>
    <language>en</language>
    <item>
      <title>Learning Swift - Part 1</title>
      <dc:creator>Nick Heriana-Keers</dc:creator>
      <pubDate>Thu, 02 May 2019 20:37:18 +0000</pubDate>
      <link>https://dev.to/nickkeers/learning-swift-part-1-37ip</link>
      <guid>https://dev.to/nickkeers/learning-swift-part-1-37ip</guid>
      <description>&lt;p&gt;My first dev.to post! I've just bought a subscription to &lt;a href="https://www.raywenderlich.com" rel="noopener noreferrer"&gt;raywenderlich.com&lt;/a&gt; to get access to their iOS and Android tutorials. I've been interested in writing apps for a while, but have never really got around to it, so I thought I'd finally try and start learning. &lt;/p&gt;

&lt;p&gt;I've worked through part of the Udacity course "Developing Android Apps with Kotlin", but I'm a bit fatigued with the JVM platform at the moment after stepping outside of my element at work and writing a bit of Scala, so I thought I'd try out some Swift instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some background
&lt;/h2&gt;

&lt;p&gt;At work I write code predominantly in &lt;a href="https://elixir-lang.org/" rel="noopener noreferrer"&gt;Elixir&lt;/a&gt; - creating (distributed) systems that can cope with large amounts of traffic - and &lt;a href="https://golang.org/" rel="noopener noreferrer"&gt;Go&lt;/a&gt; for writing smaller API's / useful applications like load testers and such.&lt;/p&gt;

&lt;p&gt;I'm mainly interested in functional programming, so I'm interested to see how I can write some functional code in Swift and to see how that compares to Elixir.&lt;/p&gt;

&lt;p&gt;As a first test, I'm going to try and solve a very simple problem in Swift and compare it against an Elixir solution. &lt;/p&gt;

&lt;h2&gt;
  
  
  The task
&lt;/h2&gt;

&lt;p&gt;Lets try a really simple task first: given a list of numbers 1..100, sum every number that is a multiple of 3 or 5 - but not including numbers that are a multiple of 3 AND 5.&lt;/p&gt;

&lt;p&gt;Here's how it looks in Elixir:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;
&lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; 
     &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;rem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;rem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That gives us 2103, and the code is not too shabby! The main ugliness here is we have to group the clause for checking the multiple of 3 or 5 separate from the clause for checking if the number is divisible by 15. Lets see how it looks in Swift.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="nf"&gt;in&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&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="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;$0&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I struggled to write this a bit in a Swift playground as I'm very new to it, I had to fight the compiler a lot. First I tried to mimic my Elixir code with the range, by just typing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But it looks like this creates a Swift Range type, which I need to cast to an Array to use filter and reduce with - although I'd love to be corrected here.&lt;/p&gt;

&lt;p&gt;I initially had the two filters as just one call, like in the Elixir, but the Swift compiler was telling me that it couldn't type-check it and I need to break it into smaller expressions - that's not very encouraging, I hope that's to do with running it in the playground and not indicative of the Swift compiler itself; although I guess it makes the code more clear.&lt;/p&gt;

&lt;p&gt;And last but not least, I'm disappointed there's no &lt;code&gt;sum&lt;/code&gt; function, but I can live without one when there's a function shorthand, which is actually fairly similar to Elixir &lt;code&gt;{$0 + $1}&lt;/code&gt; vs &lt;code&gt;&amp;amp;(&amp;amp;1 + &amp;amp;2)&lt;/code&gt; for example.&lt;/p&gt;

&lt;h3&gt;
  
  
  Thoughts
&lt;/h3&gt;

&lt;p&gt;Its too early to say much right now, I hope that when I get to using XCode properly and writing some basic apps I won't be hamstrung by the compiler. &lt;/p&gt;

&lt;p&gt;I like the syntax for higher order functions though, it's quite readable, and the function shorthand is concise which is great.&lt;/p&gt;

&lt;p&gt;Until next time!&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>macos</category>
    </item>
  </channel>
</rss>
