I hope to see you there!
]]>I am looking forward to meeting many talented developers and learning all about who you are and what you do.
]]>
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.
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?
import Foundation func findMedian(_ data: ArraySlice<Int>) -> (Int, Int, Int) { let index1 = (data.count-1)/2 let index2 = data.count/2 let sliceIndex1 = index1 + data.startIndex let sliceIndex2 = index2 + data.startIndex let median = (data[sliceIndex1]+data[sliceIndex2])/2 return (sliceIndex1, sliceIndex2, median) } let data = [0, 1, 2, 5, 11, 17, 19, 21] let X = data[0..<data.count] let (q2Index1, q2Index2, Q2) = findMedian(X) let L = data[0..<q2Index2] let U = data[(q2Index1+1)..<data.count] let (_, _, Q1) = findMedian(L) let (_, _, Q3) = findMedian(U) print(Q1) print(Q2) print(Q3)
]]>
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 :
Here, each command object operates on a Robo, telling it to make a single move. The commands are collected into a program.
//: Command - Represents commands as objects. import Cocoa class Robo : CustomStringConvertible { let name : String var description: String { return name } init(name: String) { self.name = name } } protocol RoboCommand { func execute(robo: Robo) } class TurnRight : RoboCommand { func execute(robo: Robo) { print("\(robo) turns right ➡️") } } class TurnLeft : RoboCommand { func execute(robo: Robo) { print("\(robo) turns left ⬅️") } } class GoForward : RoboCommand { func execute(robo: Robo) { print("\(robo) goes forward ⬆️") } } class RoboProgram { let robo : Robo var commands : [RoboCommand] = [] init(robo: Robo) { self.robo = robo } init(robo: Robo, commands: [RoboCommand]) { self.robo = robo self.commands = commands } func execute() { for command in commands { command.execute(robo: robo) } } } let robo = Robo(name: "Mikey") let program = RoboProgram(robo: robo) program.commands.append(GoForward()) program.commands.append(TurnLeft()) program.commands.append(GoForward()) program.commands.append(GoForward()) program.commands.append(TurnRight()) program.commands.append(GoForward()) program.execute()
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.
]]>let countme = [1, 2, 3, 4, 5] let sumall = countme.reduce(0){$0+$1} // sumall is now 15
See this if you’re looking for a detailed HowTo.
But dictionary is the more advanced problem. How do I reduce when I have keys and values?
The key here is to note that a dictionary yields the contents of its sequence in tuples containing pairs of values. We still have our familiar $0, $1 arguments from reduce as above, where $0 is the partial sum and $1 is the individual element value. But now $0 and $1 are both tuples, and you get to their contents through $0.0, $0.1, $1.0, and $1.1.
let namesNumbers = ["Mike":21, "Bruce":25, "Alice":27] let (bigname,bignumber) = namesNumbers.reduce(("",0)) {return ($0.0 + $1.0, $0.1 + $1.1)}
This example concatenates the strings and adds the integers, and the two separate data types just happen to use the same symbol for the two operations.
]]>There is a lot of learning material out there, but the best resources start out right away with productive examples that get you working quickly. This tutorial video from Traversy Video does a very good job. I used this React demo from Facebook to model my architecture after.
The least trivial part of the game was the capture algorithm. The game engine must figure out if a newly placed stone results in another stone being surrounded. To figure this out, I implemented a recursive breadcrumb algorithm. The method searches recursively in four directions, resulting in a wide descending tree, but marks visited spots on the matrix to prevent infinite recursion. If at any point an empty space (liberty, in game terms) is found, the call stack starts bubbling up with that information. The recursive call will say whether or not a liberty was ever found. If it was, I run a second method to erase all visited points on the matrix, otherwise I run a different method to restore the marked spots back to original.
Here is my reaction () to React :
Hidden inherited methods are traps waiting to happen. My IDE (Codepen) did not alert me to inherited methods, so this may be my own fault for not using a JetBrains tool or some such. Pick unique method names just in case.
I don’t really see the payoff. I understand the principle of data flow and limiting state on classes, but a real class-driven language does this in a more flexible way without need of a framework.
I know a little bit of CSS and HTML, so I was able to decorate it a bit, so that it kind of looks like an old wood board.
]]>We start by defining the complexity of an algorithm to be an estimate of the number of steps required to solve a problem. This count is going to vary by the size of the problem, and so the complexity must be a function of the problem size.
Sometimes this can result in a nasty looking functions like or . We say that both of these functions are in the category because they have similar growth properties. This is called Bachmann-Landau notation.
In mathematics, this notation describes the asymptotic behavior of a function. if , then we have a guarantee that after a certain point, the graph of will fall below the graph of , if you’re willing to rotate a bit. In computer science, we use this to understand how the performance of an algorithm will change as the size of the problem or input grows. There are actually several Bachmann-Landau notations, each of which gives us some kind of indication on the limits of the growth of a function.
Name | Notation | Analogy |
---|---|---|
Big O | is | |
Little o | is | < |
Big Omega | is | |
Little omega | is | |
Theta | is |
Credit goes to MIT for the excellent table and analogies. The definitions of each can be found in the referenced material.
Additionally, some of these definitions can be expressed using calculus. We start with the definition of in the material says that is if :
That’s a symbolic mouthful. It means, “For all positive values of C, there exists some k such that, if x>k, the absolute value of f(n) is less than or equal to the absolute value of g(n) multiplied by C.
Separately, any calculus text will give this definition for a limit at infinity. I like Paul’s Online Calculus Notes. It says that if
<
Again in plain English, “For any positive value of , there must exist some value for N such that for any value of x that is greater than N, this inequality holds.”
Let’s make some substitutions of variable names in the above equation. If we change out f(x) for f(x)/g(x), 0 for L, C for , n for x, and k for N :
<
or
<
This boils down to an alternative definition of . is if :
This allows us to use other mathematical tricks, like L’Hopitals Rule, to figure out if two given functions follow this relationship.
]]>A lot of small but indispensable tools belong in the software developer’s toolkit. Take Markdown, for example. I am actually writing this post using WordPress Jetpack’s new Markdown composition features. So far, so good! I use Markdown extensively for creating documents. There is only a little bit to learn, but you can create nice documents very quickly. I use Typora for MD docs on my Mac, and I highly recommend it.
Mustache is a template system embedded in several front-end web frameworks. It is very simple to use, and the whole thing can be described in one unix-style man page.
I find it is most instructive to play with frameworks interactively using playgrounds or other REPL environments, so one npm install -g mustache
later I am on the way to learning it.
Mustache works by embedding tags into any type of text data, such as plain text, HTML, or Markdown. Tags are encapsulated using mustache symbols (the curly brace) like this : {{tag}}. Take this example :
I have taught the following courses. My students tell me they are {{bad_adjective}}, but {{good_adjective}}! {{#courses}} {{name}} : {{description}} {{/courses}}
These tags will be replaced by variable values found in another file to follow. Some variables mark sections that can be repeated or left out all together based on their values. There values are found in a separate file.
In Mustache lingo, the above data comprises a template file. It will be formatted using variables contained in the following JSON file, called a view or hash :
{ "good_adjective" : "fun", "bad_adjective" : "challenging", "courses" : [ { "name" : "CPSC 110", "description" : "Introduction to Computing"}, { "name" : "CPSC 125", "description" : "Foundations of Computer Science"}, { "name" : "CPSC 150", "description" : "Computers and Programming I" }, { "name" : "CPSC 250", "description" : "Computers and Programming II" }, { "name" : "CPSC 270", "description" : "Data and File Structures"}, { "name" : "CPSC 425", "description" : "Object Oriented Programming and Design"}, { "name" : "CPEN 414", "description" : "Computer Architecture"}, { "name" : "ENGR 213", "description" : "Discrete Structures for Computer Applications"} ] }
The result is the following
I have taught the following courses. My students tell me they are challenging, but fun! CPSC 110 : Introduction to Computing CPSC 125 : Foundations of Computer Science CPSC 150 : Computers and Programming I CPSC 250 : Computers and Programming II CPSC 270 : Data and File Structures CPSC 425 : Object Oriented Programming and Design CPEN 414 : Computer Architecture ENGR 213 : Discrete Structures for Computer Applications
You can see how this might be more useful if the JSON object was produced by loops in JavaScript and the template data was marked-up HTML instead of simple plain text. But that’s the general idea.
]]>