avatarJames Finn

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>

Teaching Love to a Gay Boy (The Hard Way)

A closeted afternoon goes sideways

Image licensed from Adobe Stock

Josh’s heart pounded so hard he was afraid it would break open. He could feel his lips burning even over the furnace of Olivia’s body. He couldn’t see her in the dark, but he could feel her breath brush his cheek as she scooted closer. He smelled bubblegum and squeezed his eyes shut.

Then right through his closed lids, light stabbed him.

“What are you kids doing in that closet!? Get out here NOW!”

Josh heard giggles from the middle of the room as he jumped to his feet. He saw Mrs. Parisi kinda-maybe smiling with her eyes, which didn’t match the anger in her words.

“Mom!” sighed Olivia, pushing past Josh. “Jeez!”

“I didn’t mean for the bottle to land on me,” stuttered Josh. Then he bit his tongue, thinking, “Stupid! Stupid! Stupid!”

Olivia pushed him, but not hard.

Mrs. Parisi put her hands on her hips and looked around at the little cluster of 7th graders just home from school. Her voice came out nice, like a mom’s voice should. “OK, kids. Probably time to go home now. Olivia needs to help me put dinner in the oven.”

Dustin fist bumped Josh on the way out then gave him a hard look, which made his heart pound again.

Image licensed from Adobe Stock

“Dinner! Get out here before it gets cold!”

“OK, Mom,” mumbled Josh, not loud enough for her to hear. He lay on his back on his bed staring up at the dolphins on the ceiling. They looked so happy swimming up there in the stars. He remembered when he used to feel that happy, when his mom let him paint them, even though she said, “what a silly idea” through a big smile.

He picked up his phone and frowned at the notification he’d been ignoring. “Sorry you didn’t get to kiss me?”

He didn’t want to answer Olivia because he didn’t want to lie, but she’d be really mad if he didn’t. He clicked and went to the message, tapping out, “Jeez! Mom reads these! I cant talk KISSING on my phone!”

That took care of tonight, he figured. But he knew she’d walk to school with him in the morning. Then what?

His phone buzzed right away. “Sorry forgot! My mom doesn’t care. She said you cd come over tonight and swing with me on the porch, so wanna?”

He threw his phone down and jumped off the bed. Halfway out the door, he changed his mind. He grabbed his phone and tapped, “Sorry Dustins coming over for homework.”

Two minutes later, spooning up mashed potatoes, he wondered why he lied.

“Morning, Romeo!”

Josh jumped out of a bad dream as fast as he could. He squinted up toward his dolphins but his dad’s scratchy face came into focus instead. “Time to get up, buddy. School day!”

Josh started to throw the covers over his head, then he remembered the nightmare and jumped out of bed faster than he ever had.

His dad squeezed his shoulder. “Wow, anxious to get going, huh, Romeo?”

Josh rubbed his eyes, pushed his dad out of the way, and dragged his feet toward the bathroom. “What do you keep calling me that for?”

“Oh, no reason. But Mrs. Parisi called your mom last night.”

“Oh, my god!”

“Language, kiddo! But, hey, way to go. Olivia’s really pretty. Are you two …”

“Dad! Stop!”

“Sorry, man. Not trying to embarrass you. A first kiss is pretty cool, though. I mean, if you ever want to talk about anything … You don’t have to, but I want to make sure you know nobody’s mad at you.”

Josh slammed the bathroom door shut and reached for his toothbrush. Then he remembered his dream, sank onto the toilet seat, and started to cry.

Image licensed from Adobe Stock

Dustin grabbed Josh during outdoor gym and dragged him toward the bleachers again. He heard the coach’s whistle blow but ignored it. He started to say, “I had a really bad dream about us last night,” but Dustin cut him off.

“Did you like it? Did you like kissing her? Huh?”

He sounded mad, which was so not fair. “I didn’t want the bottle landing on her! I was just there. So were you!”

“The rules weren’t even fair. When the bottle landed on me, you had to spin again. That’s just … I’m not mad at YOU, I’m just mad.”

“Everybody wants to know how I liked my first kiss, and I didn’t even kiss her.”

Dustin smiled as big as Josh had ever seen. “You didn’t? Really?”

Josh reached out a finger toward Dustin’s cheek but stopped short. “Not like it woulda been my first kiss anyway.”

Dustin stared all intense, right into Josh’s eyes, and for half a second he was happy again, like his ceiling dolphins. Then he remembered. “Look, I had this really bad dream about us last night.”

The whistle blew louder. “Get your head in the game, boys! Play basketball!”

Image licensed from Adobe Stock

After school, Josh slipped away from Olivia, yelled, “Text you later!” to Dustin, and slouched toward Mr. Grant’s science class, looking over his shoulder to make sure nobody was following.

“Hey, buddy,” the man said when he noticed Josh hovering in the doorway. “Help you with something?”

“No, I mean … just um, it was cool seeing you at Olive Garden last week. Like you were a real person and everything.”

Mr. Grant laughed and Josh knew it was because he’d joked with his class the year before about how, believe it or not, teachers have regular lives too.

“I wish you were my teacher again this year.”

“Really? Most kids think my classes are hard.”

“They are!”

“I guess so. Anyway … what’s up?”

“I wish, um … nuthin. Just I saw you with your friend at the restaurant and you guys looked. You looked like …”

You saw me with my husband, Josh. But you know I’m not allowed to talk about that. We had that discussion in class last year when they passed that law, remember?”

Josh sighed. “It’s not appropriate for kids, right?”

“I didn’t say that, but I really am not supposed to talk about it. I’m sorry, but …”

Josh took a deep breath and spilled out gallons of words he’d been saving up all day. “But look, that’s why I came. I need to talk to somebody who understands because I had a really bad dream last night about something really good that happened and everybody thinks something different happened and if they knew they’d think really bad things about me because I’m a kid and all this is inappropriate and I don’t know what I’m supposed to do and everybody’s going to hate me.”

Only he wasn’t sure Mr. Grant understood the last few words because they came out all choked.

The young teacher jumped up from his desk and covered the distance to Josh in about two and half giant steps. He reached out like he was going to touch the boy’s shoulder then drew his hand back like from a hot stove.

“Buddy,” he said in a really soft voice. “Sit down.”

Josh sat as Mr. Grant pulled a student desk across the tiled floor, making a huge squeaking sound, then sat in it backwards to face his former student. “Buddy, I promise you there’s nothing to cry about. Everything’s going to be OK.”

“No it’s not! I had this nightmare, only it wasn’t really a nightmare because the kissing part was true. I really kissed him!”

“It’s OK, Josh,” said Mr. Grant with his teeth all gritted up like he was mad.

“But my dad thinks Olivia is my girlfriend and so does she probably and Dustin … Oh, my god I can’t believe I said his name!”

“Josh, breathe. Take deep breathes. In… good job. Now breathe out… Are you all right? Do you want to go talk to the nurse?”

“Can she she talk to me about this?”

“No.”

“Then who?”

“How about your mom and dad?”

“Are you crazy!?”

“Are you afraid they’d hurt you?”

“No, of course not! But … they wouldn’t like it at all. They’d hate me.”

“I doubt that very much.”

“You don’t know them!”

Josh heard screaming as he woke up, but it took him several long seconds to realize the sound was coming from his own mouth. It took him longer to realize the reason he couldn’t move was that his dad was holding him tight and saying his name.

“Josh! Josh! Wake up, it’s just a dream.”

He went limp and stared at his dolphins, wishing they’d woken him up instead of his dad.

His dad rocked him a little, like he used to when he was a little kid. Then he asked in a very soft voice, “How come you always look at the ceiling when I come in here?”

Josh looked down and away. That dream again! Dustin and him behind the bleachers again. Dustin laughing and teasing him and then pretending to punch him and then their faces getting real close and Josh’s lips touching Dustin’s and Dustin kissing back hard and holding him tight like his Dad was now and then the nightmare part and …

Josh jerked away from his dad as hard as he could. “Get off me!”

“You don’t have to be afraid of me, Josh. I’m not mad at you about anything. Not even what you were dreaming about.”

“You can’t know my dreams!”

“I heard you. I heard you shouting his name and telling him you love him. Then you started to scream. Why?”

Josh looked up at his dolphins again then closed his eyes and spoke very softly and very slowly. “I look at the ceiling because I want to be up there with them.”

“Instead of here with people who love you? Why?”

“Because they’re different. They’re different like I’m different from you and mom and they don’t care and they like me anyway. And they’re always happy like I used to be.”

“Josh, you’re not different from me and Mom. Why would you say that?”

“Yes, I am! You don’t know!”

“Yes, I do. I don’t care if you love Dustin. I don’t care if you kissed him. That doesn’t make you different.”

“But you were happy when you thought I kissed Olivia.”

“I was happy because you’re growing up and I’m proud of you. I was happy because first kisses are special. I still remember mine. If Dustin was your first kiss, I’m just as happy.”

“Are you sure? You don’t even like Dustin!”

Josh’s dad laughed and pulled his son in close. “Only because he broke our window with a baseball that time. He’s all right. Especially if he’s special to you. Even if he isn’t pretty like Olivia.”

“Is too!”

“So what were you screaming about? If you like him so much, why the nightmare?”

“I keep having it. Like everybody sees us kissing behind the bleachers and they start laughing at us and calling us names and we know we don’t have friends anymore and nobody likes us, not even our parents.”

“But why would you think that? Your mom and I don’t have any problem with gay people. That’s silly. It’s 2023, not 1953. Jeez.”

“But you make me go to Sunday School with Grandma and they say all kinds of bad stuff about …”

“Are you kidding me? What stuff? Look, never mind. I don’t care what they say. Your mom and I just thought going to church would be good for you. Like … God damn it, I don’t know what we thought. We weren’t thinking. You don’t have to go anymore if it bothers you.”

Josh stared at the dolphins again for a long time, then he whispered, “What if it doesn’t matter if I don’t go? What if it’s true?”

“It’s not! I swear to you.”

“But Dad! Even the teachers at school aren’t allowed to talk to us about being … you know.”

“Gay? You can say the word, buddy. Nothing wrong with being gay! It’s perfectly normal.”

“Then why is it against the law to talk to kids about it? It’s illegal, Dad!”

Josh gasped as his dad hugged him so hard he couldn’t breathe, then rocked him again. “Oh, my god, buddy. Holy shit. I had no idea you felt like this. No wonder you’re having nightmares. I am so sorry your mom and I didn’t talk to you about this already.”

“Really?”

“Really. Seriously. I am SOOO sorry. But look, can I ask you a question?”

“Maybe.”

“How’s Dustin handling all this?”

“Huh?”

“I mean, you know his dad and I have been friends like twice as long as you and Dustin have been alive. I know for a fact he’d be shocked if he knew you thought I had anything against gay people. So I just wonder if Dustin is in the same boat.”

“Oh!”

“Tell you what. I’m gonna go talk to Mom, and then we’re gonna plan a barbeque for the weekend. Just us and Dustin and his parents. Sound good?

“Really?”

“Really.”

Josh stared at the ceiling long after his dad tiptoed out. When he finally fell asleep, chattering dolphins filled his dreams until Dustin took over, but this time the nightmare part didn’t happen.

Josh woke up smiling. When the happy feeling didn’t fade away, he jumped out of bed and grabbed his phone. He waved at the dolphins as he headed out, tripping over his feet texting Dustin.

“Dude! Wait till you hear what ...”

Want to know more about Josh, Dustin, and Olivia? Each story is standalone, but you can read them in order if you want to:

Author’s note: This short story has a happy ending because while Josh’s parents may not have done the best job letting him know how they feel about LGBTQ people, that was an oversight caused by unconscious expectations and assumptions. Imagine the outcome if Josh’s parents were not supportive. Who would he turn to for help if his state had passed a law forbidding teachers and school professionals from discussing sexual orientation or gender identity? This is not a hypothetical question. Florida is about to enact such a law and several other states are planning to.

This story is a response to the Prism & Pen writing prompt, Queer as Kids: Or … Can We Please Teach Love?

Other stories so far:

James Finn is a columnist for the LA Blade, a former Air Force intelligence analyst, an alumnus of Queer Nation and Act Up NY, and an “agented” but unpublished novelist. Send questions, comments, and story ideas to [email protected].

My writing is always free to readers who click my social media links, but if you’d like to browse more, click here to join Medium. Your nominal membership fee will help support my work. To get an email whenever I publish a new story, Click Here.

LGBTQ
Equality
Education
Fiction
Parenting
Recommended from ReadMedium