You are on page 1of 1

swift.

org Building Data Structures in Swift A comparison to


python.org
Landon Marchant ‘20
Reconstructing data structures found in traditional # Python has single line comments
/*
””” as well as
languages Example:
multi-line comments
DoubleLinkedList
”””
next: refers to tail, or following (self + 1) node. Is a pointer
prev: refers to head, or previous (self - 1) node. Is a pointer.
Swift is a modern, object-orientated language for programming Pythonis a data-based type-safe language.
data: contains value. Is the data stored in the node.
iPhone and Apple products. 4 cannot be added to the string “three”
head: points to the first node in the list.
tail: points to the last node in the list.
1. Type safe & object orientated Python types are inferred.
*/
2. Supports advanced documentation & version control strategies
3. Supports modern international character sets name = “you” # string variable, mutable
4. Java-like language with closures. name = 42 # integer variable
The code is treated like an object, similar to lambda in Python head data next prev d = 42.0 # float variable
Python uses
indentation
to determine
data next prev
operation order.
Swift is a variable type-safe language.
This prevents passing an Int where the code expects a String.

data next prev Results


// Swift has single examples of variables and constants:
// line comments let π = 3.14159
/* as well as let 你好 = "你好世界" Converted a dozen data structures into Swift,
multi-line comments. let = "dogcow" data next prev including:
*/
linked lists
double linked lists
inferred tail data next prev circular lists
var face = “a string variable in Swift”
specified
// a variable of type string Gained experience in the Xcode IDE, which
var welcomeMessage: String = “hello” Class DoubleLinkedList<T>: Sequence { has advanced version control options and git
let bigNumber: Int = 127 // Head and tail are non-optional variables.
generic repository integration.
func swapTwoValues<T>(_ a: T, _b: T) var head = DoubleNode T
// _ allows for an unspecified function parameter var tail = DoubleNode T Developing an application for use
var count: Int {
... // some properties can be computed with a scrolling marquee sign. This entails
} using Swift to build an application that
Swift uses closures to specify new control mechanisms. func popLast() -> T? { organizes and presents information on the
This prints each character on a line by itself. if count == 0 {
return nil board for lab use. Development requires
welcomeMessage.forEach() { } collecting data from RSS feeds and websites
print($0) else { with Python scripts, and communicating with
} let result = tail.prev!.data!
removeNode(tail.prev!) a serial device through a primative interface.
return result
}
}

You might also like