score:3
Not so pretty (scala wanted type ascription), but if you really want one-liner:
def f1(): (Int, Int) = (2,3)
def f2(a: Int, b: Int, c: Int) = 0
(f2(1, _: Int, _: Int)).tupled(f1())
Explanation:
tupled
is a method defined for everyFunction
instance (as first-class citizen lambda). It returns a function that can accept tuples.f2(1, _: Int, _: Int)
is a partial application - it returns a function from second and third argument here, so it could be "tupled" afterwards
P.S. You could avoid type-ascription ugliness, by redefining f2
as:
def f2(a: Int)(b: Int, c: Int) = 0
f2(1) _ tupled f1()
Update. If you don't want to break tail-recursion, use TailCalls:
import scala.util.control.TailCalls._
def f2(a: Int)(b: Int, c: Int): TailRec[Int] =
if (false) tailcall(f2(1) _ tupled f1()) else done(0)
f2(1)(2, 3).result
The additional advantage here is that if your f2
gets more complex - it's easier to trace tail-positioned calls in the code. It also supports things like mutual tail-recursion.
Explanation:
tailcall
marks a tail-recursive calldone
marks value you want to return in the end of loop.result
runs stack-safe computation and extracts result fromTailCall[T]
. You can also notice thatTailCall
wrapper plays simillar role to@tailrec
- it doesn't allow non-tail-positioned call, as it would require to "unwrap" the result. Compiler-level optimization is being replaced by trampolined computation, which is also stack-safe.
score:2
I don't think that there is a cleaner way out of box. Perhaps it's possible to do something using macros, but it kinda defeats the purpose.
Only alternative I can think of (sort of cleaner, because it doesn't pollute the namespace) is following:
someFunction(foo, bar) match {
case (two, three) => someOtherFunction(one, two, three)
}
score:2
If someOtherFunction
were defined with two parameter groups then it'd be easy.
Instead of this ...
val (two, three) = someFunction(foo, bar)
someOtherFunction(one)(two, three)
... you could do this.
someOtherFunction(one) _ tupled someFunction(foo, bar)
But short of that you'll probably have to break the tuple into its parts.
Source: stackoverflow.com
Related Query
- Function calls with returned tuple without intermediate value
- How to call returned function with implicits without assigning to val
- Get value from map returned from function with implicit in Scala
- play framework 2 work with Request.session value without Action function
- Difference between function with parentheses and without
- Scala Function.tupled and Function.untupled equivalent for variable arity, or, calling variable arity function with tuple
- scala observable unify observable with a sequence without intermediate datastructure update
- Applicative instance for a tuple with monoid and function inside
- Scala not found: value x when unpacking returned tuple
- Play: How to remove the fields without value from JSON and create a new JSON with them
- Scala - currying function with implicit value
- Scala ambiguity with paren-less function calls
- In Scala invoking no-parameter function with and without brackets is executed in different way
- Scala map with partial function as value
- Model multiple function calls with a stream (in a safe, FP way)
- scala passing function with underscore produces a function not a value
- Match Value with Function based on Type
- Pass a lazy value as a function or method parameter without evaluating it?
- Scala correct syntax to use underscore("_") in function literal with tuple
- How to mock a function that returns a value class instance with Mockito?
- Set column value depending on previous ones with Spark without repeating grouping attribute
- Function reference as implicit parameter with default value not as expected
- Scala function arguments with default value followed by multiarguments
- Overriding a trait method with a function value
- Can a function receive a tuple with an undertermined number of arguments?
- println with anonymous function not printing expected value
- Spark - how to handle with lazy evaluation in case of iterative (or recursive) function calls
- scala - type of tuple with partial applied function
- Shapeless HList Polymorphic Function with a Tuple and Function
- How to pass the initial value to foldLeft from a filtered List with function chaining?
More Query from same tag
- json4s: Deserializing specific fields with a custom serializer
- Akka Cluster "Connection Refused" Confusion
- how to server html pages with play
- Fan in/fan out concurrency with Monix
- How to send results of a Scala StaticQuery SELECT query direct to JSON object
- Scala stream computation throws StackOverflowError
- Scala: flatMap with tuples
- Scala String Trim
- Document a recurring argument with a built-in type
- How to handle OnComplete message with internal queuing reactive stream subscriber?
- Scala program - difficult to understand
- object slick is not a member of package scala
- Why do you need Arbitraries in scalacheck?
- How can colored terminal output be disabled for sbt/play?
- Combine key and values of a MAP
- Implicit materialization in the same module
- How to define partitioning of DataFrame?
- Any scalatest matchers for matching json
- Set default env variable for test configuration in sbt
- Two related existential type parameters
- Compose FunctionK by foldMap
- Processing a list of Scalaz6 Validation
- How to count the number of occurences of an element with scala/spark?
- I want my function to return a Stream[T], but I can't figure out how to make it type check
- Scala XML processing is skipping a value
- Idiomatic Haskell-like iterate in Scala?
- dropDuplicates with non-numeric condition
- lift don't rewrite the value
- In Scala, how do immutable and mutable sets and maps compare with regard to garbage collection?
- Scala SortedMap under the hood