The pride of the south

On the morning of Saturday, Sep. 23, Charleston’s Rainbow Row came out to cover iconic King St. People from all over the state and city showed up dressed in their various rainbow-themed garb some…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Case studies of HOF

Calling the HOF one after another, we can construct a chain of HOF.

On the above example we are getting the sum of first 100 even-integer number by using the HOF chaining.

We will play a game, a game of characters.

The game play goes like following:

Let us define constraints for repetitive characters:

Let us have the printing constraints for each repetitive character:

Now consider if different characters have the same number of appearance.

ok thats becoming confusing. Lets describe using example, We will use the following two quotes

The final output will be

So as we can see o has the max appearance, 4 ,that’s why it has the very first position. Then n has 3 appearance, though there is a cap n, N, which we will discard as we said on the very first constraint.

Rest of the character’s appearance is pretty interesting.
let us ignore t for now.

Remember we said 💚 has the highest precedence, then 💙 and finally 💖. Thats why the chronology is b , i , and finally e.

Again earlier we said; for same colored heart with same number appearance, the chronology will be alphabetically. Thats why i then t for 💙.

So the very first small step is to make a char-counter. The counter will operate on a single quote at a time and will give us the total character count. Now there should be two constraints that the counter has to maintain:

The first line of code is filtering the lower case character then, in the second we are making sure that we at-least have multiple characters.

Now we have a lowerCase dictionary containing only lowercase characters which char count we have to return.

So for each char in lowerCase array we have to perform an operation, Either we will start the count with 1 if the char is yet not been added or increase the char count by one. Does the following ring any Bell?

If not, it should be map. ie lowerCase.map({ })

Let us define a charCount dictionary, var charCount = [Character: Int](), which will hold the char counting.

note: shorthand if; if condition ? executeForTrue : executeForFalse

Wait, one last think, we need filter out the entities, char, which does not posses multiple count. So which HOF should we use?

Right it is a simple choice, filter(). charCount.filter({ $0.value > 1 })

Finally our charCount() becomes:

Here we will combine maximum char count by combining two quotes.

If we have the same quotes then there is nothing to compare, will return nil.

Now let us count character for each of the quote by using the charCount(), we build on the last section.

If both the charCount is nil or count == 0 then there is no benefit of continuing the counting further.

Currently we do not have any data type which can represent the character count with type-of-heart it belongs to. Let us define an enum for Heart.

Let us use CombinedValue to update our existing charCount Array, greenCharCount and blueCharCount.

Our goal is to transform the [Character: Int] Dictionary in to a [Character: (precedence: Heart, count: Int)] Dictionary.

Almost map() right, hmm. Incase we haven’t saw: it is basically a map between values, Int to (Heart, Int). For this case we will use another HOF mapValues() which will only map the values keeping the keys same.

Pretty strait forward. Here $0 is the value, type Int, actually. So for each Int value we are mapping with new Instance of CombinedValue(precedence: Heart, count: Int)

The one last thing is to merge the both dictionary into a single one. So the final Dictionary will have the character as key with type-of-heart and count as value. For this purpose we will use another HOF, merging(). merging() will give us an opportunity to set a closure for common key. As a result we can customize the value based on our requirements.

Here $0 and $1 are CombinedValue type object for greenCombine and blueCombine respectively. And they have the same key.
The value which has the highest precedence is selected by default. If they have the same count then a new CombinedValue is created with equal precedence.

Finally combinedMaxChar as following:

In this section we will sort keys based on the requirements. We will use the combined Dictionary we got from the combinedMaxChar() and will sort its key or character.

On of the sorting requirements was 💚 > 💙 > 💖 when the chart count is same. For fulfilling this requirements we need to add a computed property to Heart which will return the Heart precedence. Let us call it weight.

Now for sorting we will use sorted() HOF. Let us discuss different sorting requirements.

Again for empty or nil Dictionary we will just return nil. Then we are implementing the sorting requirements. Finally we are using map() to get only the keys array or [Character]. Dictionary are not meant for sorting.

On the beginning of the blog we saw a fixed format for printing. So ya we need to have a formatter.

Our formatter will have two input, a combine dictionary that we got from combinedMaxChar and a sorted key array from sortKeys. And the formatter will return the desire formatter string.

On format we will return an empty string if something is not proper.

Here we are using another HOF, forEach. We are traversing the sortedKeys array and getting the corresponding value from the input, [Character: CombinedValue], array. After that we are constructing a String based on our desire format.

So finally it wrapping time. Here on compareQuote()we will wrap everything up.

Come on we don’t need any explanation for this. 😊

Whenever you find a situation where you need to do some operation on a collection type, be sure to use HOF. Smart people does that. Now how will you sharpen you HOF skill? Only one way and the way is same for all skills.

Practice.

So practice more on HOF, you will definitely become a better one. Make sure you understand closure very well. Ask question and solve them by using HOF.

Happy talk on HOF. Thanks. 🌊

Add a comment

Related posts:

What Are the Advantages of Employing an Expert Electrical expert

Are the lights flashing on and off? Is the plug making you miss a piece of your network show? Why you can’t work a few of your machines in a specific region of the kitchen? Indeed, it could be…

MEET DOCUMENT BISHOP THE IKWERRE NINJA

Document Bishop as he is popularly called in the entertainment world is an award winning movie director, scriptwriter, producer, cinematographer also an actor. Document Bishop was born on the 7th Of…

Creative news

Myths About Weight Loss Exposed It probably won’t surprise you to learn that there is a lot of misunderstanding about nutrition. In reality, many individuals reading this essay will have just a…