Swift implementation#13
Swift implementation#13mephistophele-s wants to merge 1 commit intoHowProgrammingWorks:masterfrom mephistophele-s:master
Conversation
GYFK
left a comment
There was a problem hiding this comment.
@anastasiaSTR just a brief review with few changes. I suggest revisioning the following work by yourself and making proper changes with having what was commented in HowProgrammingWorks/LinkedList#16 in mind as there are lots of improvements that have to be done to this implementation.
| import Foundation | ||
|
|
||
|
|
||
| class Node<T>: CustomStringConvertible where T: CustomStringConvertible, T: Comparable { |
There was a problem hiding this comment.
That is not the preferred code style and is wrong in the means of your task. It is mainly used when you have several Generic and try to constrain them like:
class Nodes<A, B, C> where A: Comparable, B: Equatable, C: CustomStringConvertible {
}It may be better to use &here.
class Node<T>: CustomStringConvertible where T: CustomStringConvertible & Comparable {because you use only one Generic and only a few protocols. When you have more protocols It is better to use a typealias
typealias NodeRelatedProtocol = CustomStringConvertible & Comparable & Equatable
class Node<T>: CustomStringConvertible where T: NodeRelatedProtocol {Yet for this task you should use extensions combined with the element-based behavior.
public class Node<T> {
public typealias Element = T
public let value: Element
public init(with value: Element) {
self.value = value
}
}
public extension Node where Node.Element: Equatable {
func nodeValueEqualTo(valueOf aNode: Node) -> Bool {
return self.value == aNode.value
}
}| } | ||
|
|
||
| func delete() { | ||
| parentNode!.subNodes = parentNode!.subNodes.filter({ $0 !== self }) |
There was a problem hiding this comment.
That's a very bad idea combined with using the value of parentNode as a Node? with the write access from outside the class.
If the user tries for some reason doing the following
let tree = Tree<Int>()
tree.root.value = 99
tree.root.parentNode = nil
tree.root.delete()The result is predictable.
GreatAndPowerfulKing
left a comment
There was a problem hiding this comment.
I completely agree with @GYFK. Also I would advise you not to require conformance to CustomStringConvertible from T.
No description provided.