Swift BigInteger?

I recently ran across the need for a large integer in Swift – something that needs more than the 64 bits provided by Int64. This is not the first time. I have played around with Swift Decimal in the past (NSDecimal, NSDecimalNumber) only to find they come up short in precisely representing a rational number. Python, for example, makes it trivial to find the Nth decimal place of any rational number by storing the repeating decimal pattern under the hood and making that info available to you.

This poor substitute of mine only supports the binary OR and POPULATION operations and has no string representation. But it worked for what I needed.

 

The Gap

So I’ve been silent here for the better part of a year, and I just wanted to say why: I made  a major career change and started a new job. I write Android apps for a tech company here in Williamsburg, VA now. The job is great, but I have been very busy. I intend to continue posting about iOS, Math, Python, AI, Databases, and now Android.

Cheers!

Heapsort, because I haven’t posted in a while

This is heapsort in a Swift playground. I needed a stable sort, so naturally I wrote an… unstable sort.

Of course, you can just use sort() or sorted(), but if you are inclined to roll your own, the Swifty way to do it is to create an extension. Note that an Array extension with a Generic Where Clause is needed to make element comparisons > < == work.

On to writing mergesort (stable).

Sigh.

Matrix Operations in Swift

I recently completed 10 Days of Statistics on HackerRank. Yay!

In the process, I needed some matrix operations for a medium-difficulty problem. And here they are, code style be damned :

That’s the identity matrix popping out at the end, which validates my implementation.

But what’s this? A 60-line method? Uncle Bob would not be pleased.

Comments should not take the place of good variable/method names. Those section comments give clues as to where my methods should be :

Better. Uncle Bob would be proud (or give me credit for trying, anyway).

One more thing : Thanks to StackOverflow user Alexander, I have an even better way to express that pivot loop :

iOS Williamsburg – Swift Basics

Do you want to learn Swift syntax, have fun, and meet an enthusiastic group of developers? Come by the Williamsburg Library at 515 Scotland Street, Williamsburg, VA, on Saturday, August 12, 2017. We will be going over Swift basics. We have many experienced developers who are eager to share tips and help newcomers.

iOS development basics.

Saturday, Aug 12, 2017, 10:00 AM

Location details are available to members only.

11 Members Attending

Room c

Check out this Meetup →

I hope to see you there!

iOS Swift Developers in Williamsburg, VA

Get together to meet, discuss our plans for future meet ups and learn some Swift

Saturday, Jul 22, 2017, 10:00 AM

Williamsburg Library
515 Scotland Street Williamsburg, VA

7 Members Attending

Bring your laptop, we can go over the basics of swift and see where everyone is skills wise.

Check out this Meetup →

 

If you are near the Williamsburg Library on Scotland St. this Saturday, July 22 2017, at 10AM, and you have an interest in iOS development using the Swift programming language, please stop by. I will be giving a presentation on language basics and the development environment. We have a diverse group of enthusiasts with newbies and app store veterans alike.

Swift Fun with ArraySlice

There is a lovely article by Luna An describing ArraySlice objects in more detail than I do here. It covers Swift 3 at the moment, and you should note that Swift 4 includes support for single-ended ranges, so you can create slices ala [..<count].

I found a good use for an ArraySlice while trying to find quartiles in a set of data.

The problem of finding each quartile is essentially the same problem of finding the median from three different data sets, one being the original input set, the other two being the upper and lower half of the set after removing the original median element, if it exists.

Here, I use findMedian to perform all three tasks. I found that I had to do a bit of extra work because the array slice is not indexed starting at zero. I wonder why they chose to implement slices in this way?

 

Command Design Pattern in Swift

It is the summer of Swift.

I was perusing some wonderful design patterns from Oktawian Chojnacki, and I decided to play around with the Command pattern. The Command pattern represents commands as objects to go between a Caller, which references the commands, and a Receiver, which is referenced by the commands.

I’m reminded of a game :

RoboRally

Here, each command object operates on a Robo, telling it to make a single move. The commands are collected into a program.

The first draft of this code had the Commands store their target Robo in a property. I realized a problem with this in that my program would accept commands for any Robo, when only one Robo belongs to the program. My solution for this was to give control of the command target to the program itself.

Of course, now the commands are little more than glorified functions, which can be stored in arrays in Swift anyway.