UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Issues with understanding a reduce() function

Forums > Swift

Hey guys. Here I have an example from one of Paul Hudson's books. Filter, map, super clear.

But reduce does not let me sleep at night.

func countTheCharacterWithReduce(in string: String, character: Character) -> Int {
    return string.reduce(0) {
        $1 == character ? $0 + 1 : $0
    }
}

In a function above, you pass a string and a char to check and return it's count.

what does $1 stands here for? And $0 too. Would anyone be able to explain me that a little better? Thanks!

4      

The function can be rewritten as:

func countTheCharacterWithReduce(in string: String, character: Character) -> Int {
    return string.reduce(0) { (accumulatedValue, nextValue) in
        nextValue == character ? accumulatedValue + 1 : accumulatedValue
    }
}

which hopefully makes it a bit clearer what's going on here. The $0 and $1 are what's known as shorthand argument names and refer to the first and second arguments, respectively, without having to supply names like I did in the rewritten function above.

You can see the same reliance on shorthand argument names in a typical map usage:

let ints: [Int] = ["0", "1", "2"].map { Int($0)! }

which, when written out longways, could be:

let ints: [Int] = ["0", "1", "2"].map { element in Int(element)! }

4      

Hey there. I had to write a reduce code today, so I am a bit refreshed :)

This just works like filter, map etc; essentially like a for-loop and for each step of the loop. First is result which is an object type of what you aimed to reduce to. In your case it is an Integer and notated as $0.

Second one is an object from any kind of list which you have been looping through. Character in your case and notated as $0.

You can change your code as follows. I personally use this $ notation when using filter, map etc, however prefer this way when it comes reduces as I think this way it is more easy understand what is going on.

string.reduce(into: 0, { count, char in
    char == character ? count + 1 : count
}) 

As a note there are two methods for reduce and as you were able to mutate the initial result I thought yours was reduce( into:). The other one is reduce( initialResult:)

Just watching Paul's video and it led me here, so this is my first post on the forum.

I wish this will be helpful. And don't hesitate to experiment!

4      

Great stuff. Thank You both. Im few step closer now!

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.