{"id":28778,"date":"2023-01-02T16:56:44","date_gmt":"2023-01-02T11:26:44","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/"},"modified":"2023-01-02T16:56:44","modified_gmt":"2023-01-02T11:26:44","slug":"solved-equivalent-kotlin-code-to-my-f-functional-code-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/","title":{"rendered":"[Solved] Equivalent Kotlin code to my F# functional code [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-48722690\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"48722690\" data-parentid=\"48722642\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>The main problem I found was finding an equivalent operator to the pipeOperator<code>|&gt;<\/code>, then i found three potential alternatives:<\/p>\n<p>using <code>.let<\/code><br \/>\nusing <code>.run<\/code><br \/>\nusing <code>infix function<\/code><\/p>\n<p>Below example was good startup for me to solve my issue:<\/p>\n<pre><code>fun main(args: Array&lt;String&gt;) {\n    var a = 3\n    val b: Int = 6\n\n\n    println(\"Hello World! doubling, then tripling the sume of $a and $b is \" +\n            \"${(sumx(a,b)\n                    next ::dbl\n                    next ::trpl\n                    )}\")\n\n    println(\"Hello World! doubling, then tripling the sume of $a and $b is \" +\n            \"${(sumx(a,b)\n                    .let (::dbl)\n                    .let (::trpl)\n                    )}\")\n    println(\"Hello World! doubling, then tripling the sume of $a and $b is \" +\n            \"${(sumx(a,b)\n                    .run (::dbl)\n                    .run (::trpl)\n                    )}\")\n\n    println(\"Hello World! doubling, then tripling the sume of $a and $b is \" +\n            \"${(sumx(a,b)\n                    into (::dbl)\n                    into (::trpl)\n                    )}\")\n}\n\nfun sumx (x: Int, y: Int) : Int = x + y\nfun dbl (x: Int): Int = x * 2\nfun trpl (x: Int): Int = x * 3\n\ninfix fun &lt;T, R&gt; T.next(map : (T) -&gt; R) : R = map(this)\ninfix fun &lt;T, R&gt; T.into(func: (T) -&gt; R) = func(this)\n<\/code><\/pre>\n<p><strong>UPDATE<\/strong><\/p>\n<p>I Created the gradle project using this command: <code>gradle init --type java-library<\/code><\/p>\n<p>Deleted the <code>src\/main<\/code> and the <code>src\/test<\/code> folders<\/p>\n<p>Created the <code>src\/kotlin<\/code> and <code>src\/resources<\/code> folders<\/p>\n<p>Created my file at <code>src\/kotlin\/Main.kt<\/code> as below:<\/p>\n<pre><code>import kotlin.coroutines.experimental.buildSequence\n\nfun main(args: Array&lt;String&gt;) {\n    println(\"Hi, Let's start\")\n\n    val series = listOf(\n            30, 21, 29, 31, 40, 48, 53, 47, 37, 39, 31, 29, 17, 9, 20, 24, 27, 35, 41, 38,\n            27, 31, 27, 26, 21, 13, 21, 18, 33, 35, 40, 36, 22, 24, 21, 20, 17, 14, 17, 19,\n            26, 29, 40, 31, 20, 24, 18, 26, 17, 9, 17, 21, 28, 32, 46, 33, 23, 28, 22, 27,\n            18, 8, 17, 21, 31, 34, 44, 38, 31, 30, 26, 32\n    )\n\n    fun initialSeasonalComponents(series: List&lt;Int&gt;, slen: Int): Map&lt;Int, Double&gt; {\n        val nSeasons = (series.size \/ slen).toFloat()\n        val grouped = series.map { it.toFloat() }.chunked(slen)\n        val seasonAverages = grouped.map { it.average() }\n        return (0 until slen).associate {\n            Pair(it, grouped.zip(seasonAverages)\n                    .fold(0.0) { s, (els, av) -&gt; els[it] + s - av }\n                    \/ nSeasons)\n        }\n    }\n\n    println(\"Seasons Average\u00df: \\n ${initialSeasonalComponents(series, 12)}\")\n\n    fun initialTrend(series: List&lt;Int&gt;, slen: Int): Double =\n            series.windowed(slen)\n                    .fold(0) { s, x -&gt; x.last() - x.first() + s }\n                    .toFloat() \/ slen.toDouble()\n\n    println(\"Initial Trend: \\n ${initialTrend(series, 12)}\")\n\n    fun tripleExponentialSmoothing(series: List&lt;Int&gt;, slen: Int, alpha: Double, beta: Double, gamma: Double, nPreds: Int): Sequence&lt;Double&gt; {\n        var smooth = 0.0\n        var trend = 0.0\n        val seasonals = initialSeasonalComponents(series, 12).toMutableMap()\n        return buildSequence {\n            for (i in 0 until (series.size + nPreds)) {\n                when {\n                    i == 0 -&gt; {\n                        smooth = series.first().toDouble()\n                        trend = initialTrend(series, slen)\n                        yield(series.first().toDouble())\n                    }\n                    i &gt;= series.size -&gt; {\n                        val m = i - series.size + 1\n                        yield((smooth + m * trend) + (seasonals[i % slen] ?: 0.0 \/* Need default values because of null safety *\/))\n                    }\n                    else -&gt; {\n                        val v = series.first().toDouble()\n                        val lastSmooth = smooth\n                        smooth = alpha * (v - (seasonals[i % slen] ?: 0.0)) + (1.0 - alpha) * (smooth + trend)\n                        trend = beta * (smooth - lastSmooth) + (1.0 - beta) * trend\n                        seasonals[i % slen] = gamma * (v - smooth) + (1.0 - gamma) * (seasonals[i % slen] ?: 0.0)\n                        yield(smooth + trend + (seasonals[i % slen] ?: 0.0))\n                    }\n                }\n            }\n        }\n    }\n\n    val f = tripleExponentialSmoothing(series, 12, 0.716, 0.029, 0.993, 24).toList()\n    val res = f.map {it.format(2)} \/\/ used to format the numbers, can be avoided.\n\n    println(\"Forecast: \\n $res\")\n}\n\nfun Double.format(digits: Int) = java.lang.String.format(\"%.${digits}f\", this) \/\/ Extension function to format the numbers\n<\/code><\/pre>\n<p>The <code>0.0<\/code> is the initial state for the <code>fold<\/code> function, <code>{ s, (els, av) -&gt; }<\/code> is the operation applied to each element, where s is the accumulator and <code>(els, av)<\/code> is the current element (in this case an entry of a map), which has been deconstructed1, leaving <code>els<\/code> as the key and <code>av<\/code> as the value of the current entry.<\/p>\n<p>My <code>gradle.build<\/code> is:<\/p>\n<pre><code>buildscript {\n    ext.kotlin_version = '1.2.21'\n\n    repositories {\n        mavenCentral()\n    }\n    dependencies {\n        classpath \"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version\"   \/\/ for gradle build\n    }\n}\napply plugin: 'kotlin'\n\nrepositories {     jcenter()    }\n\ndependencies {\n    compile \"org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version\"\n}\n\nsourceSets.main {\n    kotlin.srcDirs += 'src\/kotlin'\n    resources.srcDirs += 'src\/resources'\n}\n\njar {\n    baseName 'myApp'\n    manifest.attributes 'Main-Class': 'MainKt'  \/\/ '[namespace].[arctifact\/file]Kt'\n    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }\n}\n<\/code><\/pre>\n<p>The generated <code>jar<\/code> file was saved at <code>build\/libs\/MyApp.jar<\/code> and i run it using command <code>java -jar myApp.jar<\/code><\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Equivalent Kotlin code to my F# functional code [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The main problem I found was finding an equivalent operator to the pipeOperator|&gt;, then i found three potential alternatives: using .let using .run using infix function Below example was good startup for me to solve my issue: fun main(args: Array&lt;String&gt;) { var a = 3 val b: Int = 6 println(&#8220;Hello World! doubling, then &#8230; <a title=\"[Solved] Equivalent Kotlin code to my F# functional code [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/\" aria-label=\"More on [Solved] Equivalent Kotlin code to my F# functional code [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[600],"class_list":["post-28778","post","type-post","status-publish","format-standard","hentry","category-solved","tag-kotlin"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Equivalent Kotlin code to my F# functional code [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Equivalent Kotlin code to my F# functional code [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The main problem I found was finding an equivalent operator to the pipeOperator|&gt;, then i found three potential alternatives: using .let using .run using infix function Below example was good startup for me to solve my issue: fun main(args: Array&lt;String&gt;) { var a = 3 val b: Int = 6 println(&quot;Hello World! doubling, then ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-02T11:26:44+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Equivalent Kotlin code to my F# functional code [closed]\",\"datePublished\":\"2023-01-02T11:26:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/\"},\"wordCount\":142,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"kotlin\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/\",\"name\":\"[Solved] Equivalent Kotlin code to my F# functional code [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-01-02T11:26:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Equivalent Kotlin code to my F# functional code [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Equivalent Kotlin code to my F# functional code [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Equivalent Kotlin code to my F# functional code [closed] - JassWeb","og_description":"[ad_1] The main problem I found was finding an equivalent operator to the pipeOperator|&gt;, then i found three potential alternatives: using .let using .run using infix function Below example was good startup for me to solve my issue: fun main(args: Array&lt;String&gt;) { var a = 3 val b: Int = 6 println(\"Hello World! doubling, then ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/","og_site_name":"JassWeb","article_published_time":"2023-01-02T11:26:44+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Equivalent Kotlin code to my F# functional code [closed]","datePublished":"2023-01-02T11:26:44+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/"},"wordCount":142,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["kotlin"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/","name":"[Solved] Equivalent Kotlin code to my F# functional code [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-01-02T11:26:44+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-equivalent-kotlin-code-to-my-f-functional-code-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Equivalent Kotlin code to my F# functional code [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/28778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=28778"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/28778\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=28778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=28778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=28778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}