Skip to content

Instantly share code, notes, and snippets.

@zippi-MD
Created September 9, 2023 06:28
Show Gist options
  • Save zippi-MD/2d58477b69bc88db12a1b05fd0447257 to your computer and use it in GitHub Desktop.
Save zippi-MD/2d58477b69bc88db12a1b05fd0447257 to your computer and use it in GitHub Desktop.
Generics class exercises
import Foundation
// Stack -
// last in - first out
// push & pop
//struct Stack<Element> {
// var items = [Element]()
//
// mutating func push(_ newItem: Element) {
// items.append(newItem)
// }
//
// mutating func pop() -> Element? {
// guard !items.isEmpty else { return nil }
//
// return items.removeLast()
// }
//}
//
//var intStack: Stack<Int> = Stack()
intStack.push(1)
intStack.push(2)
print(intStack.pop())
print(intStack.pop())
print(intStack.pop())
var doubleStack: Stack<Double> = Stack()
doubleStack.push(2.0)
print(doubleStack.pop())
print(doubleStack.pop())
map
func myMap<T, U>(_ items: [T], _ transformer: (T) -> (U)) -> [U] {
var result = [U]()
for item in items {
let transformedItem = transformer(item)
result.append(transformedItem)
}
return result
}
let myStrings = ["one", "two", "three"]
let stringsLengths = myMap(myStrings) { $0.count }
print(stringsLengths)
myStack.filter { }
/ Mapping a Stack
struct Stack<Element> {
var items = [Element]()
mutating func push(_ newItem: Element) {
items.append(newItem)
}
mutating func pop() -> Element? {
guard !items.isEmpty else { return nil }
return items.removeLast()
}
func myMap(_ transformer: (Element) -> Bool) -> Stack<Element> {
var result = [U]()
for item in items {
let transformedItem = transformer(item)
result.append(transformedItem)
}
return Stack<U>(items: result)
}
}
let myStack = Stack(items: [1, 2, 3, 4, 5, 6])
let newSack = myStack.myFilter { $0 % 2 == 0 }
//newSack // Stack items [2, 4, 6]
//newSack.pop --> 2
//newSack.pop --> 4
//var intStack = Stack<Int>()
//intStack.push(1)
//intStack.push(2)
//
//var doubledStack = intStack.myMap { $0 * 2}
//
//print(intStack.pop())
//print(intStack.pop())
//
//print(doubledStack.pop())
//print(doubledStack.pop())
//func checkIfEqual<T: Equatable>(_ first: T, _ second: T) -> Bool {
// return first == second
//}
//
//print(checkIfEqual(1, 2.0))
//func checkIfDescriptionsMatch<T: CustomStringConvertible, U: CustomStringConvertible>(_ first: T, _ second: U) -> Bool {
// return first.description == second.description
//}
//
//print(checkIfDescriptionsMatch(Float(1), Double(1.0)))
//MACCY
//protocol IteratorProtocol {
// associatedtype Element
// mutating func next() -> Element?
//}
//
//protocol Sequence {
// associatedtype Iterator: IteratorProtocol
// associatedtype Element where Element == Iterator.Element
//
// func makeIterator() -> Iterator
//}
struct Stack<Element>: Sequence {
// typealias Iterator = StackIterator
// typealias Element = StackIterator<Element>.Element
var items = [Element]()
mutating func push(_ newItem: Element) {
items.append(newItem)
}
mutating func pop() -> Element? {
guard !items.isEmpty else { return nil }
return items.removeLast()
}
mutating func pushAll<S: Sequence>(_ sequence: S) where S.Element == Element {
for item in sequence {
self.push(item)
}
}
func makeIterator() -> StackIterator<Element> {
return StackIterator(stack: self)
}
}
struct StackIterator<T>: IteratorProtocol {
// typealias Element = T
var stack: Stack<T>
mutating func next() -> T? {
return stack.pop()
}
}
var myStack = Stack<Int>()
myStack.push(10)
myStack.push(20)
myStack.push(30)
let mySet: Array<Int> = [1, 2, 3, 4]
myStack.pushAll(mySet)
for value in myStack {
print("for in loop: got \(value)")
}
//var myStackIterator = StackIterator(stack: myStack)
//while let value = myStackIterator.next() {
// print(value)
//}
//
//print(myStack.pop())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment