Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous types in Scala

Tags:

scala

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);
}
like image 603
GammaVega Avatar asked Apr 17 '13 15:04

GammaVega


1 Answers

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) );
  }
}
like image 59
Ben Kyrlach Avatar answered Nov 15 '22 08:11

Ben Kyrlach