There is a method in Scala that looks something like this.
List[(A, B)] = List.fill(n)(doSomething(arg))
My question is, is there any way to do this in Java, or if it needs to be done through a series of long ones forand what you have.
There is in Java Collections.fill, but it does not seem to do what I want.
Scala is executed as follows:
def buyCoffee(cc: CreditCard): (Coffee, Charge) =
{
val cup = new Coffee()
(cup, Charge(cc, cup.price))
}
def buyCoffees(cc: CreditCard, n: Int): (List[Coffee], Charge) =
{
val p: List[(Coffee, Charge)] = List.fill(n)(buyCoffee(cc))
}
This does not look like Java to me, or not from what I know about Java, or what I have been able to find in the documentation so far.
This Scala code can be found on page 7 of functional programming in Scala by Paul Kiyana and Runar Bjarnason.
source
share