Trying to convert few of my C# project into Scala, and still trying to figure out how do to some of the conversion. I came across this code and would appreciate if someone can help me to write below code in Scala, i m particularly interested in anonymous code block in Select statement.
static void Main(string[] args)
{
Stock stk = new Stock();
Select(stk, a => new { a.Prices, a.Symbol });
}
public static U Select<T, U>(T data,Func<T,U> item)
{
return item(data);
}
Here's one implementation.
class Stock(val prices: List[Double], val symbol: String)
object CSharp {
def select[T, U](data: T)(f: T => U): U = {
f(data);
}
def main(args: Array[String]): Unit = {
val stk = new Stock(List(1.1, 2.2, 3.3, 4.4), "msft")
select(stk)(a => (a.prices, a.symbol) );
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With