DEV Community

Nick keers
Nick keers

Posted on

Learning Swift - Part 1

My first dev.to post! I've just bought a subscription to raywenderlich.com 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.

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.

Some background

At work I write code predominantly in Elixir - creating (distributed) systems that can cope with large amounts of traffic - and Go for writing smaller API's / useful applications like load testers and such.

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.

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

The task

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.

Here's how it looks in Elixir:

1..100
|> Enum.filter(fn n -> 
     (rem(n, 3) == 0 or rem(n, 5) == 0) and rem(n, 15) != 0
   end)
|> Enum.sum
Enter fullscreen mode Exit fullscreen mode

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.

([Int](1...100)).filter { (n) -> Bool in
    (n % 3 == 0) || (n % 5 == 0)
}.filter { (n) -> Bool in
    n % 15 != 0
}.reduce(0, {$0 + $1})
Enter fullscreen mode Exit fullscreen mode

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

1...100
Enter fullscreen mode Exit fullscreen mode

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.

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.

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

Thoughts

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.

I like the syntax for higher order functions though, it's quite readable, and the function shorthand is concise which is great.

Until next time!

Top comments (0)