Implicit map, or something like that

Here is what I have so far. What I want to achieve is making containers behave like the type they contain.

implicit def transformer[T, A](f: T => A)
    : List[T] => List[A] = 
      t       => t map f

implicit def combiner[T, U, A](f: (T, U) => A)
    : (List[T], List[U]) => List[A] = 
      (t      , u      ) => t zip u map { x => f(x._1, x._2) }

val x = List(1, 2, 3)
val f: Int => Int = (_ + 1)
val fx = f(x) // fx = List(2,3,4)

val g: (Int, Int) => Int = (_ + _)
val gxx = g(x, x) // gxx = List(2,4,6)

Now, this works as I want for functions. But I have a problem with methods. For instance, this will obviously not work :

val double = x + x

This does not work, because + has to be a method of List[Int], which it is not.

I would need to extract the method name, see if there is a function in scope with that name that can be applied to the underlying type, and then lift it with the implicit definition.

Or maybe that can be done with a type constructor that would somehow scan the underlying type on instanciation, and define the right methods.

Both are quite ugly solutions, and I am not even sure they are feasible. Is there any other way to achieve what I want, that doesn't involve writing explicitely all methods ?

Answers


Have you had a look at Dynamics? In combination with standard Java Reflection, this might solve your problem. I don't know if Content Assist in IDE's get's confused by this, though...


Need Your Help

Top 20% Spender's Retention Rate

sql sql-server sql-server-2008

So, to start, this is finding our top 20% of highest spenders in 2010:

Meta Box Not Showing up in The Admin Page

wordpress wordpress-plugin-dev custom-post-type meta-boxes

Using WordPress 3.7.1 and PHP 5.4.12 I am trying to add a Meta box - text field to my Custom Post Type. My custom Post Type name is "news" and this is my code:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.