Hi all. I'm a mostly C/C++ developer with very little Mac dev experience - just command line tools, really. But, over the past few weeks, I've been trying to learn Xcode and Swift. So I thought I'd post my progress here and hopefully some of you more experienced developers could give me some pointers.
I decided I'd take a crack at writing Towers of Hanoi in Swift with output to the console, and here's what I came up with:
// Declare three stacks or pegs, and preload
// one stack with values.
var stacks = [
[Int](), [Int](), [Int]()
]
stacks[0] = [ 5, 4, 3, 2, 1 ]
// Display all 3 stacks to the console.
func dump() {
var stidx=0
for stack in stacks {
print("\(stidx): ", terminator:"")
for value in stack {
print("\(value) ", terminator:"")
}
print()
stidx += 1
}
print("---")
}
// Move 'count' values from one stack to another.
// Stacks are given by indices 'from' and 'to'.
// Use stack 'extra' for recursive moves.
func hanoiMove( _ count: Int, from: Int, to: Int, extra: Int ) {
let doRecurse = count > 1
if doRecurse {
hanoiMove( count-1, from: from, to: extra, extra: to)
}
// Move the single top value from source stack.
stacks[to] += [ stacks[from].removeLast() ]
dump()
if doRecurse {
hanoiMove( count-1, from: extra, to: to, extra: from)
}
}
dump()
hanoiMove( stacks[0].count, from: 0, to: 2, extra: 1 )
If you folks could review this code and share your thoughts, I'd love the feedback. But I do have some specific questions:
- Output using print seems awkward, especially the need to supply the empty string terminator if I don't want a carriage return. I assume print is a wrapper around a more fully function stream class, but I'm having trouble finding example code. Could someone rewrite my dump function using a more flexible output method - not print? If so, I'd love to see how it's really done.
- In the hanoiMove function, I was frustrated that I couldn't have a reference to an array. I really wanted something like
let dest = stacks[from]
and have that be a reference rather than a copy. Is there no way to have references to arrays? - For my next project, I'll be writing a MacOS command line tool. The only problem is, I can't figure out how to access the command line arguments argh and argue in C/C++. Advice? Hints?
And any general advice would be greatly appreciated. Thanks!
Comments
Code Review
Hi Paul,
Quick one on the for looping around the stacks ...
You can write it like this:
for (index, stack) in stacks.enumerated() {
print("Item \(index): \(stack)")
}
This saves keeping track of the stidx variable.
Regards
Scott
Code review - dump function
I think the only way you could improve the dump function would be to start to move towards encapsulating the tower and stacks, by having a Tower class and a Stack class.
These classes would conform to CustomStringConvertible and then you can provide a description property that prints the state of the tower and in turn would print each stack.
Your Tower would then contain an array of Stacks and your move method would then be a function on the Tower class.
Nice dump() rewrite
I love the much more concise dump() loop. Thanks, Scott.
I'll have to look into classes and adding the description method you described. Thanks for the pointer. Also learning AppleScript and iOS dev. Too much to learn, too little time. But it's totally fun so far.
Code Changes
Hi Paul, feel free to post any changes and I'm more than happy to help. Coding is great fun :)
I haven't done any C++ for many, many years, but from memory you get passed number of args in argc and an array (pointer) in argv. You can then loop the array and 'read' the args.
Apologies if this is no longer current.
I would also recommend Apple's swift playgrounds iPad app if you haven't tried this yet. I'm told they are accessible.
Hello Paul,
Hello Paul,
When learning Apple development you ahve to separate two very different concerns or learning areas to speak this on another way.
1- Learning programming stuf.
2- Learning how to deal with Apple development environments and other related areas.
As I understand your question, it is related with area 1 here as I classified that. You ahve questions about swift and algorithms and are asking for help on how to code things.
Although this is extremely valid, this is the kind of question that is not blind or accessibility related, meaning that you are likely to find lots of help on stackoverflow and other specialized groups as well as in several programming lists, either for blind people or not. These groups have people interested in learning and teaching programming itself
Although I am not an editorial team member, it seems for me that AppleVis is more towarded to daily use of Apple's products by blind people and not to blind developpers. This means that the kind of people that come here are less likely to be able to help you or to get interested on this topic than on a programming forum.
For example, you really should read about classes and object oriented development as a means to help you to start to code. But, again, what you asked here might be very well answered in a global, common programming forum. Blindness does not add anything special to the cake when learning a programming language, so I recomend you to integrate with sighted and blind programmers globally in order to get yourself going.
Now, xcode is something different. Note that xcode has nothing to do with programming itself, It is an IDE made from Apple where professional Apple developpers have to develop software specially in huge shared projects.
The xcode product has its own accessibility and usability chalenges for blind people. Most Apple tutorials dealing with xcode will ask you to make things with the mouse that can not be intuitively translated to something you can achieve with VoiceOver. These questions are very specific to blind users who are using xcode, so it iis worth asking for help here.
To summarize, from my point of view:
What is a class? This is something you can learn out of blindness sites and not something that AppleVis can offer special help for you to learn.
How can I emprove this code? This is something you can learn out of blindness sites and not something that AppleVis can offer special help for you to learn.
How can I connect a outlet to a component laid out on the interface builder? Well, this is worth asking here. Sighted people will instruct you to use the mouse but obviously you can not do it, so asking for other blind people who have plaied with Apple development can help and you will find some of them here.
How can I use ayuto layout to build a good graphical user interface on my storeboard on the interface builder? Well, this is worth asking here. Sighted people will instruct you to use the mouse but obviously you can not do it, so asking for other blind people who have plaied with Apple development can help and you will find some of them here.
Good point
Well, you've made a very good point, thanks. Of course I already know what you've stated. And that makes me scratch my head and wonder why I posted this here in the first place, as opposed to stackOverflow. Doh!
Okay, I'll continue with my Swift education elsewhere, and ping AppleVis as needed for visual impairment related code dev issue.
You will like to know though
You will like to know though that there is a list called mv-dev http://groups.google.com/group/MV-Dev specific to blind people wanting to dive into Apple Development.
It is not as active as I would preffer but there are great folks over there whiling to help.