Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I created an Xcode project with my changes. It makes it easier to #313

Merged
merged 2 commits into from
Dec 16, 2016

Conversation

rzaccone
Copy link
Contributor

I created an Xcode project with my changes. It makes it easier to test and the playground was crashing!

@kelvinlauKL kelvinlauKL self-assigned this Dec 13, 2016
Copy link
Member

@kelvinlauKL kelvinlauKL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't gone through everything yet, but here's some preliminary feedback that should warrant some discussion / modifications.

var currentNode = root
let charactersInWord = Array(word.lowercased().characters)
var index = 0
while index < charactersInWord.count {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A for loop can do exactly what we're trying to do in a cleaner manner in this scenario (I know the site tutorial uses the while construct, but a fellow contributor alerted me of a better way).

for character in word.lowercased().characters {
  if let childNode = currentNode.children[character] {
    currentNode = childNode
  } else {
    currentNode.add(value: character)
    currentNode = currentNode.children[character]!
  }
}

This removes the necessity of the index and charactersInWord properties. The for construct also helps us bind each element in the word.lowercased().characters, alleviating the necessity of picking it out ourselves.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. This is a good change.

guard !currentNode.isTerminating else {
return
}
self.wordCount += 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary reference to self.

///
/// - Parameter word: the word to check for
/// - Returns: true if the word is present, false otherwise.
func contains(word: String) -> Bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacing this with a for loop would simplify things here too:

func contains(word: String) -> Bool {
  guard !word.isEmpty else {
    return false
  }
    
  var currentNode = root
  for character in word.lowercased().characters {
    guard let childNode = currentNode.children[character] else { return false }
    currentNode = childNode
  }
    
  return currentNode.isTerminating
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I agree.

@rzaccone
Copy link
Contributor Author

Here's a slight simplification to the for loop in wordsInSubtrie.

        for childNode in rootNode.children.values {
            let childWords = wordsInSubtrie(rootNode: childNode, partialWord: previousLetters)
            subtrieWords += childWords
        }

@rzaccone
Copy link
Contributor Author

Along the lines of your other good suggestions, here's an improvement to findTerminalNodeOf. It's the same adjustment to the for loop.

    private func findTerminalNodeOf(word: String) -> Node? {
        var currentNode = root
        for character in word.lowercased().characters {
            guard let childNode = currentNode.children[character] else {
                return nil
            }
            currentNode = childNode
        }
        return currentNode.isTerminating ? currentNode : nil
    }

@kelvinlauKL
Copy link
Member

Yep, things are looking pretty Swifty :)

Could you update the PR with the changes?

…be done in a more Swift-like manner. Also updated the project to Xcode 8.2.
@rzaccone
Copy link
Contributor Author

I pushed all my changes and all tests pass.

@kelvinlauKL
Copy link
Member

Looks great. I took a look over everything, and all tests passed.

I'll merge this in now. Cheers!

@kelvinlauKL kelvinlauKL merged commit f14c133 into kodecocodes:master Dec 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants