avatarAvi Kotzer

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3392

Abstract

er">17</span>]</pre></div><blockquote id="c020"><p>Compact Map()</p></blockquote><p id="b1c2"><code>compactMap</code> function is similar to <code>map</code>, but it also includes an additional step, it filters out any <code>nil</code> values that result from applying the transformation closure. This is particularly useful when you have an array of optional values and you want to transform them while simultaneously filtering out the <code>nil</code> elements.</p><div id="9cf9"><pre><span class="hljs-keyword">extension</span> <span class="hljs-title class_">Array</span> { <span class="hljs-keyword">func</span> <span class="hljs-title function_">compactMap</span><<span class="hljs-type">T</span>>(<span class="hljs-keyword">_</span> <span class="hljs-params">transforms</span>: (<span class="hljs-type">Element</span>) -> <span class="hljs-type">T</span>?) -> [<span class="hljs-type">T</span>] { <span class="hljs-keyword">var</span> result <span class="hljs-operator">=</span> <span class="hljs-type">T</span> <span class="hljs-keyword">for</span> element <span class="hljs-keyword">in</span> <span class="hljs-keyword">self</span> { <span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> element <span class="hljs-operator">=</span> element { result.append(transform(element)) } } <span class="hljs-keyword">return</span> result } }</pre></div><p id="fe9c">Here the only change from <code>Map</code> and <code>Compact Map</code> is avoiding the nil value with a condition.</p><p id="3b42">In the above code</p><ul><li>It takes a closure <code>transform</code> as an argument, which specifies how each element should be transformed.</li><li>Inside <code>compactMap</code>, a new array <code>result</code> is created to store the transformed non-nil elements.</li><li>It then iterates over each element of the original array (<code>self</code>), applies the transformation closure to each element, and checks if the result is non-nil.</li><li>If the result is non-nil, it appends the transformed element to the <code>result</code> array.</li><li>Finally, it returns the <code>result</code> array containing all the non-nil transformed elements.</li></ul><blockquote id="9909"><p>How to consume it?</p></blockquote><div id="41f1"><pre><span class="hljs-keyword">let</span> mapArray <span class="hljs-operator">=</span> [<span class="hljs-number">23</span>, <span class="hljs-number">43</span>, <span class="hljs-number">56</span>, <span class="hljs-literal">nil</span>, <span class="hljs-number">75</span>, <span class="hljs-number">9</span>, <span class="hljs-number">14</span>] <span class="hljs-keyword">let</span> result<span class="hljs-operator">=</span> mapArray.compactMap { <span class="hljs-variable">$0</span> } <span class="hljs-built_in">debugPrint</span>(result) output: [<span class="hljs-number">23</span>, <span class="hljs-number">43</span>, <span class="hljs-number">56</span>, <span class="hljs-number">75</span>, <span class="hljs-number">9</span>, <span class="hljs-number">14</span>]</pre></div><blockquote id="854c"><p>Flat Map()</p></blockquote><p id="b656">FlatMap is typically the same as <code>map</code> does, only difference is that it always return flatten array joining all elements. Typically used to collect specific data in n

Options

ested array, dictionary or set</p><div id="6431"><pre><span class="hljs-keyword">extension</span> <span class="hljs-title class_">Array</span> { <span class="hljs-keyword">func</span> <span class="hljs-title function_">flatMap</span><<span class="hljs-type">T</span>>(<span class="hljs-keyword">_</span> <span class="hljs-params">transform</span>: (<span class="hljs-type">Element</span>) -> [<span class="hljs-type">T</span>]) -> [<span class="hljs-type">T</span>] { <span class="hljs-keyword">var</span> result <span class="hljs-operator">=</span> <span class="hljs-type">T</span> <span class="hljs-keyword">for</span> element <span class="hljs-keyword">in</span> <span class="hljs-keyword">self</span> { result.append(contentsOf: transform(element)) } <span class="hljs-keyword">return</span> result } }</pre></div><p id="75fa">In the above code</p><ul><li>It takes a closure <code>transform</code> as an argument, which specifies how each element should be transformed into a sequence.</li><li>Inside <code>flatMap</code>, a new array <code>result</code> is created to store the flattened elements.</li><li>It then iterates over each element of the original array (<code>self</code>), applies the transformation closure to each element, and concatenates the resulting sequences into the <code>result</code> array.</li><li><code>append(contentsOf: )</code> will add the elements of a sequence to the end of the array.</li><li>Finally, it returns the <code>result</code> array containing all the flattened elements.</li></ul><blockquote id="2285"><p>How to consume it?</p></blockquote><div id="117f"><pre><span class="hljs-keyword">let</span> arrayOfArrays <span class="hljs-operator">=</span> [[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], [<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>], [<span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>]] <span class="hljs-keyword">let</span> transformedArray <span class="hljs-operator">=</span> arrayOfArrays.flatMap { <span class="hljs-variable">$0</span> } <span class="hljs-built_in">print</span>(transformedArray) output: [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>]</pre></div><h1 id="3bda">Conclusion</h1><p id="057c">There are plenty of other use cases for higher-order functions. This is a gist of what we have discovered today:</p><ul><li>If you need to simply transform a value to another value, then use <code>map</code>.</li><li>If you need to remove nil values, then use <code>compactMap</code>.</li><li>If you need to flatten your result one level down, then use <code>flatMap</code>.</li></ul><p id="0a10">Thank you for your time and attention! 👏👏👏</p><p id="33e6">Do clap👏 if you like this and comment your suggestions!!! <i>Happy coding</i>!!!</p><blockquote id="1e0c"><p>Source Code: <a href="https://github.com/Vikassingamsetty/HigherOrderFunctions.git">GitHub</a></p></blockquote></article></body>

Dogtooth

An ornament, a fish, a flower, a movie… and a tooth!

Photo by Gabrielle Costa on Unsplash

Today’s New York Times Spelling Bee letters:

Art: Iva Reztok

D, H, N, O, T, U, and center G (all words must include G)

Merriam-Webster says…

Credit: merriam-webster.com

Silly little dictionary! Don’t you know that dogtooth can’t possibly be a word if The New York Times says it ain’t?

For a complete list of rejected words, check out the Spelling Bee Master.

What’s your favorite dord* from today’s puzzle?

My Two Cents

There were slim pickens when to the list of rejected words today, but two of them surprised me.

Credit: https://nytbee.com/Bee_20220812.html

Dogtooth and gundog. How are they not on the list of accepted words?!? Now, I really wanted to write about gundog (a dog trained to work with hunters), since that would allow me to discuss doggies and post many pictures of them. And as everyone who’s ever been on the internet knows, animals rule when it comes to getting viewers. Well, conspiracy theories, too. And fake news. And celebrity gossip. And porn. Okay, okay, so maybe animals don’t rule the internet as much as I wanted to believe. In any case, I don’t have enough time today to get into the subject of hunting dogs.

So dogtooth it is. Now, the Spelling Bee game accepts eyetooth, which is its synonym. Which makes the rejection of dogtooth even more perplexing! Or maybe I’m thinking of eyeteeth, which has nothing to do with canine’s canines, but means “something of great value, especially used in the phrase give one’s eyeteeth”.

Oh, well, i I can find a past puzzle with the letters that form either eyetooth or eyeteeth, I’ll update today’s column. For now, let’s move on with dogtooth and its many, many definitions.

lowercase d

▹ The first definition Merriam-Webster gives us for dogtooth is “canine tooth, eyetooth”, both of which refer to the canine tooth in the upper jaw.

Credit: wikicommons

Also known as cuspids, fangs, and vampire teeth, they are not exclusive to dogs, of course, but have been so closely associated with them that the word canine has become a synonym for human’s best friend. The closest relatives of dogs — the wolves, jackals, foxes, and coyotes–- belong to the Canidae family.

The lower jaw also has canine teeth, but in most animals they are smaller than the ones in the upper set. And in many species of animals, the canines of either jaw, or both, are often much larger in males than in females. The walrus is a good example of this. Humans, on the other hand, don’t show this sexual dimorphism, although there is vampirical dimorphism for those of you who believe in Dracula & Co.

▹ The second definition of dogtooth in the dictionary is “an architectural ornament common in early English Gothic that consisted usually of four leaves radiating from a raised point at the center”.

Pub Likdomein

This pattern can be seen in the moldings of medieval work of the early 12th century, and is thought to have been brought by the Crusaders. Since a picture is worth a thousand words, we are going to save ourselves a lot of typing by simply copying and pasting the Norman arch at St Michael’s and All Angels Church, in Gloucestershire, England.

Photo by Jongleur100

Interestingly, this ornamental pattern was supposedly not named for resembling a canine, but because it looked like the dogtooth violet, which we will get to in a bit.

▹ The third definition offered by the dictionary is the synonym houndstooth, which fans of the University of Alabama and the Australian department store David Jones seem to know well. The houndstooth is a “duotone textile pattern characterized by broken checks or abstract four-pointed shapes, traditionally in black and white or such contrasting dark and light pattern”. Again, let’s save some keystrokes by using an image of the pattern…

Credit: Kotivalo

…and a real-life example:

Credit: sv1ambo

Here’s a linguistic factoid: in French this pattern is known as pied-de-poule, (hen’s foot), while in Spanish it’s called pie de gallo, or “roosters foot”.

▹ The dictionary lists dogtooth violet, which we mentioned earlier, as a separate entry:

They don’t provide a photo, but we have one of the Erythronium dens-canis.

Photo by JM Planchon

There are more than 20 species of plants in the Erythronium genus, but the above example is probably the best, considering the species name means “tooth of dog”. Despite their name, dogtooth violets are not real violets, who belong to the genus Viola. They are in the same family as both the lilies and the tulips, however.

So what do you think? Was the architectural dogtooth named for the canine or the flower? Have another look:

Pub Likdomein

▹ The dogtooth tuna (Gymnosarda unicolor) isn’t in the dictionary, but apparently does exist.

Photo by Tchami

At more than two meters in length and over 250 pounds in weight, it’s much bigger than the above photo. They are brilliant blue green on the back, whitish on the belly, and always swim with their mouths open. I mention these features just in case you ever get a hankering for tune and decide to catch one on your own. But be careful. The dogtooth tuna is an aggressive predator that’s “capable of taking a wide variety of prey”. Not sure if humans are included in the wide variety, but I don’t want to be the one to find out.

▹ The dogtooth spar is a speleothem, or geological formation by mineral deposits that accumulate over time in natural caves. Its name comes from the fact that the large crystals look like dog’s teeth. Again, you be the judge:

Photo by DanielCD

(You may need to zoom in or widen the screen.)

▹ Finally, the dogtooth extension is a break in the front edge of an airplane’s wing. Here we will use two thousand words’ worth of images, first with a diagram…

Image by Steelpillow

…and then with a real-life example:

Photo by Arpingstone

In the above picture, the dogtooth extension is easier to spot on the wing at the top.

Uppercase D

Dogtooth, North Dakota is a ghost town that got its name because the nearby buttes look like dog molars. According to Wikipedia (which cites sources):

Dogtooth was first established in 1876 as a station along the Deadwood — Bismarck Trail. The station closed in 1880 when the Northern Pacific Railroad was completed, but settlers continued to homestead in the area. A post office was established and operated by Robert Pearce March 20, 1900. The November 26, 1909, edition of the Mandan Pioneer reported that Dogtooth had “a great out look for a thriving metropolis as the Milwaukee have surveyed a town near where Dogtooth now stands . . . this part of the country is settled with people who will do all in their power to make it a good town as they will certainly appreciate so near a town after having to haul their grain fifty and sixty miles to a railroad.”

Then, a year later, Charles Leonard moved his store to Raleigh, seemingly “putting the finishing touches” on the destiny of Dogtooth.

▹ A nice way to end this article and usher in the weekend is by recommending a movie I just discovered and haven’t seen. Namely, Greece’s finalist for Best Foreign Language Film at the 83rd Academy Awards that took place in 2011. Somehow the movie was considered a 2010 film even though it was released in 2009. It made its debut at that year’s Cannes Film Festival, winning the Un Certain Regard prize that recognizes young talent encourages innovative and daring works.

And Dogtooth (Kynodontas in Greek) is certainly an innovative and daring work. Nick Riganas summarized the plot on IMDB as follows:

Kept on a short leash through a volatile mixture of indoctrination, misinformation and fear, three grown-up siblings — two sisters and one brother — find themselves confined to their lavish, isolated villa. As a result, holed up in their beautiful, high-walled prison, the siblings rely on their cold, manipulative parents to learn about the ways of the world, taking reality-defying lessons from instructional cassette tapes. And, as the cruel patriarch and his frigid wife deliberately keep their unsuspecting, submissive children in the dark, freedom is nothing but a word. Then, a bad influence in the shape of security guard Christina enters the equation, and suddenly, sweet, baffling temptation threatens years of meticulous mind-programming. Is the grass greener on the other side of the fence? With this in mind, how can an ordinary dogtooth stand in the way of happiness?

Dogtooth was Greece’s first film to get nominated for an Oscar since 1977’s Iphigenia, but it lost out to Denmark’s In a Better World. Here is the trailer:

That’s it for today. We covered nine (9!) definitions of Dogtooth. Hopefully, this might convince the Spelling Bee to accept the word next time they offer a puzzle with the same seven letters.

Now you know. Next time you and your friends happen upon a feral gang of dogs and they snarl menacingly at you, you can show off your knowledge by pointing and saying “Wow, that’s a really long and sharp dogtooth!” Don’t be surprised if you friends don’t understand what you’re saying. Not because they’re terrified and are in survival flight mode… but because the editors of the Spelling Bee decided that dogtooth is a dord*.

You can check out my previous entry on another dord* here:

*What the heck is a dord, you ask? Here’s the answer:

Spelling Bee
Language
Architecture
Dogs
Film
Recommended from ReadMedium