<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by vivekpratap singh on Medium]]></title>
        <description><![CDATA[Stories by vivekpratap singh on Medium]]></description>
        <link>https://medium.com/@vivekprcs?source=rss-71315b74c825------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*cs5317axv6aKiblC.</url>
            <title>Stories by vivekpratap singh on Medium</title>
            <link>https://medium.com/@vivekprcs?source=rss-71315b74c825------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 19:07:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vivekprcs/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Exploring Higher-Order Functions in Kotlin]]></title>
            <link>https://medium.com/@vivekprcs/exploring-higher-order-functions-in-kotlin-294eff9d0ef3?source=rss-71315b74c825------2</link>
            <guid isPermaLink="false">https://medium.com/p/294eff9d0ef3</guid>
            <dc:creator><![CDATA[vivekpratap singh]]></dc:creator>
            <pubDate>Fri, 14 Jun 2024 14:52:08 GMT</pubDate>
            <atom:updated>2024-06-14T14:52:08.491Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LvbsqDAS2DcA9QvzxJd1kg.png" /></figure><h4>What are Higher-Order Functions?</h4><p>Higher-order functions are functions that can take other functions as parameters, return functions, or do both. This capability allows for more abstract and flexible code, enabling patterns like callbacks, custom control structures, and function composition.</p><p>In Kotlin, functions are first-class citizens, meaning they can be stored in variables, passed as arguments, and returned from other functions. This feature is the foundation upon which higher-order functions are built.</p><h4>Syntax and Usage</h4><p>Let’s begin by examining the syntax of higher-order functions in Kotlin:</p><pre>fun operateOnNumbers(a: Int, b: Int, operation: (Int, Int) -&gt; Int): Int {<br>    return operation(a, b)<br>}</pre><p>In the above example, operateOnNumbers is a higher-order function that takes three parameters: a and b of type Int, and operation which is a function taking two Int parameters and returning an Int. Inside operateOnNumbers, the operation function is invoked with a and b as arguments, and its result is returned.</p><h4>Example Usage</h4><p>Now, let’s see how we can use the operateOnNumbers function with different operations:</p><pre>fun main() {<br>    val sum = operateOnNumbers(10, 5) { a, b -&gt; a + b }<br>    println(&quot;Sum: $sum&quot;)  // Output: Sum: 15<br>    <br>    val difference = operateOnNumbers(10, 5) { a, b -&gt; a - b }<br>    println(&quot;Difference: $difference&quot;)  // Output: Difference: 5<br>}</pre><p>In this example, we use lambda expressions { a, b -&gt; a + b } and { a, b -&gt; a - b } to define the operation parameter inline. This demonstrates Kotlin&#39;s concise syntax for defining functions on the fly, making code more readable and expressive.</p><h4>Practical Applications</h4><p>Higher-order functions are particularly useful in scenarios such as:</p><ul><li><strong>Filtering Collections</strong>: Using functions like filter, map, and reduce to process collections based on custom criteria.</li><li><strong>Callback Mechanisms</strong>: Passing functions as callbacks to handle asynchronous operations or event handling.</li><li><strong>Custom DSLs (Domain-Specific Languages)</strong>: Building domain-specific languages by defining functions that mimic natural language constructs, enhancing readability and maintainability.</li></ul><h4>Conclusion</h4><p>In conclusion, higher-order functions in Kotlin empower developers to write more flexible and reusable code by treating functions as first-class citizens. They facilitate functional programming techniques like function composition, currying, and more, leading to more robust and expressive software solutions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=294eff9d0ef3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>