<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Functional Programming Archives - Codersee blog- Kotlin on the backend</title>
	<atom:link href="https://blog.codersee.com/tag/functional-programming/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>Kotlin &#38; Backend Tutorials - Learn Through Practice.</description>
	<lastBuildDate>Wed, 16 Apr 2025 04:49:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://blog.codersee.com/wp-content/uploads/2025/04/cropped-codersee_logo_circle_2-32x32.png</url>
	<title>Functional Programming Archives - Codersee blog- Kotlin on the backend</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How To Implement TriFunction in Kotlin?</title>
		<link>https://blog.codersee.com/how-to-implement-trifunction-in-kotlin/</link>
					<comments>https://blog.codersee.com/how-to-implement-trifunction-in-kotlin/#respond</comments>
		
		<dc:creator><![CDATA[Piotr]]></dc:creator>
		<pubDate>Wed, 19 Jul 2023 12:48:19 +0000</pubDate>
				<category><![CDATA[Kotlin]]></category>
		<category><![CDATA[Core Kotlin]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<guid isPermaLink="false">https://codersee.com/?p=9007564</guid>

					<description><![CDATA[<p>In this article, I will show you how to implement your custom TriFunction in Kotlin and how SAM conversions, and lambdas can help us with it.</p>
<p>The post <a href="https://blog.codersee.com/how-to-implement-trifunction-in-kotlin/">How To Implement TriFunction in Kotlin?</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading" id="h-1-introduction">1. Introduction</h2>



<p>Hi! If you&#8217;ve ever been wondering how to implement a custom Trifunction in Kotlin, then you just came to the right place. </p>



<p>In this, short article we will cover the following:  </p>



<ul class="wp-block-list">
<li>what is a TriFunction? </li>



<li>what is a functional interface and how can we implement it in Kotlin? </li>



<li>how can we utilize SAM conversions to make our code more concise? </li>



<li>and finally- a slightly different approach, which may be better in some cases. </li>
</ul>



<h2 class="wp-block-heading" id="h-video-tutorial">Video Tutorial</h2>



<p>If you prefer <strong>video content</strong>, then check out my video:</p>



<div style="text-align: center; width: 90%; margin-left: 5%;">
<p></p></div>


<a href="https://blog.codersee.com/how-to-implement-trifunction-in-kotlin/"><img decoding="async" src="https://blog.codersee.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=%2F%2Fi.ytimg.com%2Fvi%2F23KLMMr58kw%2Fhqdefault.jpg" alt="YouTube Video"></a><br /><br /></p>



<p>If you find this content useful,<strong> please leave a subscription&nbsp;</strong> 😉</p>



<h2 class="wp-block-heading" id="h-2-what-is-a-functional-interface">2. What is a Functional Interface? </h2>



<p>Let&#8217;s start with defining what exactly is a <strong>functional interface</strong>. </p>



<p>Well, it is nothing else than an interface with <strong>exactly one abstract method</strong>. </p>



<p>In Kotlin, we can define it using the <strong><em>fun </em></strong>keyword: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">fun interface MyFunctionalInterface {
  fun method()
  fun anotherMethod() // Compilation Error!

  // OK!
  fun nonAbstractMethod() {

  }
}</pre>



<p>As we can see, such interfaces can have multiple non-abstract members. </p>



<p>However, the code won&#8217;t compile whenever we try to implement more than one abstract method:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p></p>
<cite>Fun interfaces must have exactly one abstract method</cite></blockquote>



<h2 class="wp-block-heading" id="h-3-trifunction-and-kotlin-implementation">3. TriFunction and Kotlin Implementation</h2>



<p>The <strong>TriFunction </strong>interface is nothing else than a functional interface with <strong>three input parameters</strong> and <strong>one output.</strong></p>



<p>To better understand it, let&#8217;s take a look at the following code:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">fun interface CustomTriFunction&lt;T, U, V, R> {
  fun someFun(t: T, u: U, v: V): R
}</pre>



<p>In this example, we declare a generic functional interface <em>CustomTriFunction</em> with exactly one abstract function- <em>someFun</em>. </p>



<p>This function has 3 parameters of type T, U, and V, and returns a value of type R. </p>



<h2 class="wp-block-heading" id="h-4-test-function">4. Test Function</h2>



<p>Following, let&#8217;s declare an example test function:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">fun &lt;T, U, V, R> example(
  t: T,
  u: U,
  v: V,
  function: CustomTriFunction&lt;T, U, V, R>,
): R {
  val result = function.someFun(t, u, v)
  // some logic here
  return result
}</pre>



<p>As we can see, the <em>example</em> is a generic function, which we will invoke with 3 values, and as the last one, we will pass the implementation of <em>CustomTriFunction</em>.</p>



<p>And although this example is a bit trivial, we may want to use this strategy in real-life scenarios to keep our code <strong>open for extension and closed for modification</strong>. </p>



<h2 class="wp-block-heading" id="h-5-implementation-no-1-class">5. Implementation No 1- Class</h2>



<p>With all of that done, we can finally use the interface and implement the code responsible for invoking the <em>example</em>.</p>



<p>Let&#8217;s start with the most basic idea- adding a new class: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class CustomImplementation : CustomTriFunction&lt;String, Long, Int, Double> {
  override fun someFun(t: String, u: Long, v: Int): Double {
    // some logic
    return 10.0
  }
}</pre>



<p>The above code shouldn&#8217;t be surprising even if we are pretty new to Kotlin. </p>



<p>We implement the <em>CustomTriFunction</em> just like every generic interface and provide the body for <em>someFun</em>.</p>



<p>Nextly, the only thing we need to do is to create a new instance of our class and pass it as the last argument: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">val customImplementation = CustomImplementation()
val result = exampleOne("", 0L, 0, customImplementation)</pre>



<p> Of course, depending on our case the <em>CustomImplementation</em> could be a Kotlin object. </p>



<h2 class="wp-block-heading" id="h-6-idea-2-anonymous-object">6. Idea 2- Anonymous Object</h2>



<p>But sometimes, we don&#8217;t want to explicitly define a new class. Maybe we want to pass a very specific logic, which definitely won&#8217;t be used anywhere else in the code. </p>



<p>For such a one-time operation, we can use the anonymous object:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">val anonymousObject = object : CustomTriFunction&lt;String, Long, Int, Double> {
  override fun someFun(t: String, u: Long, v: Int): Double {
    return 10.0
  }
}
val result = exampleOne("", 0L, 0, anonymousObject)</pre>



<p>As we can see, the instance of an anonymous object (also known as an anonymous class) looks almost the same, as in the previous example. With one, important difference- it does not have a name. </p>



<p>And as I mentioned previously- this approach may be really helpful for one-time use.</p>



<p></p>



<h2 class="wp-block-heading" id="h-7-implementation-3-better-sam-conversion">7. Implementation 3 (Better)- SAM Conversion</h2>



<p>In Kotlin, <strong>functional interfaces</strong> are also known as <strong>Single Abstract Method (SAM) interfaces</strong>. </p>



<p>And instead of defining anonymous objects, we can make use of <strong>SAM conversions</strong>, which allow us to use lambda expressions instead: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">val samConversionOne = CustomTriFunction { t: String, u: Long, v: Int -> 10.0 }
val resultOne = exampleOne("", 0L, 0, samConversionOne)

// or alternatively:
val samConversionTwo = CustomTriFunction&lt;String, Long, Int, Double> { t, u, v -> 10.0 }
val resultTwo = exampleOne("", 0L, 0, samConversionTwo)

// or even:
val resultThree = exampleOne("", 0L, 0) { t, u, v -> 10.0 }

// Note: in Kotlin, if the last argument is a function,
// we can put it outside of round brackets- (). </pre>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p></p>
<cite>With a SAM conversion, Kotlin can convert any lambda expression whose signature matches the signature of the interface&#8217;s single method into the code, which dynamically instantiates the interface implementation.<br><br>Source: <a href="https://kotlinlang.org/docs/fun-interfaces.html#sam-conversions" target="_blank" rel="noreferrer noopener">https://kotlinlang.org/docs/fun-interfaces.html#sam-conversions</a> </cite></blockquote>



<p>And to put it simply, with this approach we can implement our <em>TriFunction</em> in a much more concise manner (which is often the case with Kotlin ;). </p>



<h2 class="wp-block-heading" id="h-8-approach-4-high-order-function-without-interface">8. Approach 4- High-Order Function Without Interface</h2>



<p>As the last example, let&#8217;s take a look at the high-order function. </p>



<p>In Kotlin, a <strong>high-order function</strong> is a function, which <strong>takes another function as an argument</strong> or <strong>returns a function</strong>. </p>



<p>So at the end of the day, instead of explicitly creating a custom TriFunction interface, we can <strong>define a parameter</strong> of <strong>function type</strong>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">fun &lt;T, U, V, R> example(
  t: T,
  u: U,
  v: V,
  function: (T, U, V) -> R,
): R {
  val result = function.invoke(t, u, v) // or simply function(t, u, v)
  // some logic here
  return result
}</pre>



<p>As we can see, the <em>function</em> parameter simply informs the compiler that we expect a function with three input arguments (T, U, V) and return type R. </p>



<p>And then, we can invoke it by passing a lambda to it: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">val lambda = { t: String, u: Long, v: Int -> 10.0 }
val result = example("", 0L, 0, lambda)

// or even: 
val result = exampleTwo("", 0L, 0) { t, u, v -> 10.0 }</pre>



<p>And this approach might be a great choice whenever we would like to have a TriFunction without explicitly defining a new interface.</p>



<h2 class="wp-block-heading" id="h-9-summary">9. Summary</h2>



<p>And that&#8217;s all for this article about how to implement the Kotlin TriFunction interface in Kotlin. </p>



<p>I hope you enjoy my content and if you&#8217;d like to learn more about Kotlin and support my work, then I highly encourage you to check my <a href="https://blog.codersee.com/kotlin-handbook-learn-through-practice-course/">Kotlin Handbook Course</a>. </p>
<p>The post <a href="https://blog.codersee.com/how-to-implement-trifunction-in-kotlin/">How To Implement TriFunction in Kotlin?</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.codersee.com/how-to-implement-trifunction-in-kotlin/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 

Served from: blog.codersee.com @ 2026-05-13 13:40:51 by W3 Total Cache
-->