{"id":59,"date":"2011-10-04T00:38:16","date_gmt":"2011-10-04T07:38:16","guid":{"rendered":"http:\/\/www.lorrin.org\/blog\/?p=59"},"modified":"2012-03-16T23:28:10","modified_gmt":"2012-03-17T06:28:10","slug":"scalas-missing-splat-operator","status":"publish","type":"post","link":"https:\/\/www.lorrin.org\/blog\/2011\/10\/04\/scalas-missing-splat-operator\/","title":{"rendered":"Scala&#8217;s missing splat operator"},"content":{"rendered":"<p>Ruby, Python, and many other dynamic languages have a so-called splat operator that lets you easily invoke a function by providing a list of argument values:<\/p>\n<pre class=\"brush:ruby\">def f(x,y)\r\n  x*y\r\nend\r\n\r\n&gt; fArgs = [6,7.0]\r\n=&gt; [6, 7.0]\r\n\r\n&gt; f(*fArgs)\r\n=&gt; 42.0<\/pre>\n<p>Scala does not have a splat operator per se, but you can achieve the same effect without too much work. Sadly the syntax is different for fixed-<a href=\"https:\/\/secure.wikimedia.org\/wikipedia\/en\/wiki\/Arity\">arity<\/a> and <a href=\"https:\/\/secure.wikimedia.org\/wikipedia\/en\/wiki\/Variadic_function\">variadic<\/a> functions.<\/p>\n<h3>Scala splat for variadic functions<\/h3>\n<p>For variadic functions there effectively is a splat operator. If you invoke a variadic function and append <code>:_*<\/code> to the argument the compiler will perform the splat:<\/p>\n<pre class=\"brush:scala\">&gt; def g(xs:Int*) = (0 \/: xs) (_ + _)\r\ng: (xs: Int*)Int\r\n\r\n&gt; val gArgs = List(1,2,3,4)\r\ngArgs: List[Int] = List(1, 2, 3, 4)\r\n\r\n&gt; g(gArgs:_*)\r\nres23: Int = 10<\/pre>\n<h3>Scala splat for fixed-arity functions<\/h3>\n<pre class=\"brush:scala\">&gt; def f(x:Int, y:Double) = x * y\r\nf: (x: Int, y: Double)Double\r\n\r\n&gt; val fArgs = (6, 7.0)\r\nfArgs: (Int, Double) = (6,7.0)\r\n\r\n&gt; f _ tupled fArgs\r\nres8: Double = 42.0<\/pre>\n<p>Magic! The first part, <code>f _<\/code>, is the syntax for a partially applied function in which none of the arguments have been specified. This works as a mechanism to get a hold of the function object. <code>tupled<\/code> returns a new function which of arity-1 that takes a single arity-n tuple. It is defined in the Scala <a href=\"http:\/\/www.scala-lang.org\/api\/current\/index.html#scala.Function$\">Function object<\/a>,<\/p>\n<p>However, given a <em>List<\/em> of arguments to pass to f, I&#8217;m not sure how to easily convert the List to a Tuple.<\/p>\n<p>p.s. There&#8217;s a stackoverflow post about this called &#8220;<a href=\"http:\/\/stackoverflow.com\/questions\/3568002\/scala-tuple-unpacking\">scala tuple unpacking<\/a>.&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ruby, Python, and many other dynamic languages have a so-called splat operator that lets you easily invoke a function by providing a list of argument values: def f(x,y) x*y end &gt; fArgs = [6,7.0] =&gt; [6, 7.0] &gt; f(*fArgs) =&gt; 42.0 Scala does not have a splat operator per se, but you can achieve the <a href='https:\/\/www.lorrin.org\/blog\/2011\/10\/04\/scalas-missing-splat-operator\/' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[48],"tags":[18],"_links":{"self":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts\/59"}],"collection":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/comments?post=59"}],"version-history":[{"count":7,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":157,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts\/59\/revisions\/157"}],"wp:attachment":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/media?parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/categories?post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/tags?post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}