Solving algorithms with reduce

We can solve algorithms with reduce by following a functional approach. The following lines declare a new getNamesSeparatedBy method for our previously coded GameRepository class that solves an algorithm by calling the reduce method. The code file for the sample is included in the swift_3_oop_chapter_07_31 folder:

    open func getNamesSeparatedBy(separator: String) -> String { 
      let gamesNames = getUppercasedNames() 
      return gamesNames.reduce("") { 
        concatenatedGameNames, gameName in 
        print(concatenatedGameNames) 
        let separatorOrEmpty = (gameName == gamesNames.last) ? "" : 
        separator 
        return "(concatenatedGameNames)(gameName)(separatorOrEmpty)" 
      } 
    }  

The getNamesSeparatedBy method receives a separator argument of the String type and returns a String value. The code calls the getUppercasedNames method and saves the result in the gamesNames reference constant. Then, the code calls the reduce method with an empty string as the initial value for an accumulated value. The code uses a trailing closure to specify the closure expression for combine, that is, the second argument for the reduce method.

The trailing closure receives concatenatedGameNames and gameName. First, the closure prints the value of concatenatedGameNames. This way, we will be able to understand how the algorithm completes the concatenated game names in each execution. Then, an expression determines whether the string specified in separator or an empty string has to be used as a separator. In case the gameName is equal to the last game in the Array<String>, the code uses an empty string because the last game shouldn't have the separator after it. Finally, the code returns a string composed of the names concatenated so far, concatenatedGameNames; the game name that is being concatenated, gameName; and the separator or an empty string, separatorOrEmpty.

The following line uses the GameRepository instance called gameRepository to call the previously added getSeparatedGamesNames method to generate a string with all the uppercase game names separated by a semicolon followed by a space. The code file for the sample is included in the swift_3_oop_chapter_07_31 folder:

print(gameRepository.getNamesSeparatedBy(separator: "; "))

The following lines show the results for the previous line where we can see how the concatenated game names start with the initial value specified in the initial argument for the reduce method and accumulates the strings generated so far. Finally, the value returned by the getNamesSeparatedBy method includes all the game names in uppercase separated by a semicolon and followed by a space:

INVADERS 2017; 
INVADERS 2017; MINECRAFT; 
INVADERS 2017; MINECRAFT; MINECRAFT STORY MODE; 
INVADERS 2017; MINECRAFT; MINECRAFT STORY MODE; SOCCER WARRIORS; INVADERS 2017; MINECRAFT; MINECRAFT STORY MODE; SOCCER WARRIORS; 
THE WALKING DEAD STORIES; 
INVADERS 2017; MINECRAFT; MINECRAFT STORY MODE; SOCCER WARRIORS; THE WALKING DEAD STORIES; ONCE UPON A TIME IN WONDERLAND; 
INVADERS 2017; MINECRAFT; MINECRAFT STORY MODE; SOCCER WARRIORS; THE WALKING DEAD STORIES; ONCE UPON A TIME IN WONDERLAND; CARS FOREVER; 
INVADERS 2017; MINECRAFT; MINECRAFT STORY MODE; SOCCER WARRIORS; THE WALKING DEAD STORIES; ONCE UPON A TIME IN WONDERLAND; CARS FOREVER; JAKE & PETER PAN; 
INVADERS 2017; MINECRAFT; MINECRAFT STORY MODE; SOCCER WARRIORS; THE WALKING DEAD STORIES; ONCE UPON A TIME IN WONDERLAND; CARS FOREVER; JAKE & PETER PAN; KONG STRIKES BACK; 
INVADERS 2017; MINECRAFT; MINECRAFT STORY MODE; SOCCER WARRIORS; THE WALKING DEAD STORIES; ONCE UPON A TIME IN WONDERLAND; CARS FOREVER; JAKE & PETER PAN; KONG STRIKES BACK; MARIO KART 2017

The following screenshot shows the results of executing the previous lines in the Playground:

Solving algorithms with reduce

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.51.77