BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

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      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.