Luciano
Luciano

Reputation: 8582

Apply a function to elements inside a list of lists

Let's say I have a function:

def f(i: Int): String = i.toString

How can I define xxx such as (assume I can add methods to list without implicit conversions):

val source1: List[List[Int]] = List(List(1,2), List(3,4))
val source2: List[Int] = List(1,2)

val result1: List[List[String]] = source1.xxx(f)
val result2: List[String] = source2.xxx(f)

because with

List [ _ ]

I can't tell if _ can have map applied or not. Is it possible?

Upvotes: 2

Views: 1000

Answers (3)

Andy
Andy

Reputation: 5218

It looks like you're trying to apply your function to the inner most item of the collection regardless of how deep you're values are nested (in this case, List[T] and List[List[T]]).

trait CanMapInner[WrappedV, WrappedB, V, B] {
  def mapInner(in: WrappedV, f: V ⇒ B): WrappedB
}

// simple base case (no nesting involved).
implicit def simpleMapper[V, B] = new CanMapInner[V, B, V, B] {
  def mapInner(in: V, f: (V) ⇒ B): B = f(in)
}

// drill down one level of "List".
implicit def wrappedMapper[V, B, InnerV, InnerB](implicit innerMapper: CanMapInner[InnerV, InnerB, V, B]) =
  new CanMapInner[List[InnerV], List[InnerB], V, B] {
    def mapInner(in: List[InnerV], f: (V) ⇒ B): List[InnerB] =
      in.map(innerMapper.mapInner(_, f))
  }

implicit class XXX[WrappedV](list: List[WrappedV]) {
  def xxx[V, B, WrappedB](f: V ⇒ B)(implicit mapper: CanMapInner[WrappedV, WrappedB, V, B]) = {
    list.map(inner ⇒ mapper.mapInner(inner, f))
  }
}

Adapted from qmajor's solution for Map.

Usage:

def f(i: Int): String = "Hello " + i.toString

val source1: List[List[Int]] = List(List(1, 2), List(3, 4))
val source2: List[Int] = List(1, 2)

val result1: List[List[String]] = source1.xxx(f)
val result2: List[String] = source2.xxx(f)

Console println source1
// > List(List(1, 2), List(3, 4))
Console println source2
// > List(1, 2)
Console println result1
// > List(List(Hello 1, Hello 2), List(Hello 3, Hello 4))
Console println result2
// > List(Hello 1, Hello 2)

I changed your f function for demo purposes.

Upvotes: 1

gzm0
gzm0

Reputation: 14842

You could use shapeless' Scrap Your Boilerplate to do that. Example copied from readme:

val nested = List(Option(List(Option(List(Option(23))))))

val succ = everywhere(inc)(nested)
succ == List(Option(List(Option(List(Option(24))))))

Up to you if you are ready to include a library to do that.

Upvotes: 0

Beryllium
Beryllium

Reputation: 12988

You could try this:

object Container1 {
  implicit class RichList1(l: List[List[Int]]) {
    def xxx(f: (Int) => String): List[List[String]] = {
      l.map(l => l.map(n => f(n)))
    }
  }
}

object Container2 {
  implicit class RichList2(l: List[Int]) {
    def xxx(f: (Int) => String): List[String] = {
      l.map(n => f(n))
    }
  }
}

object Mapping extends App {
  def f(i: Int): String = i.toString

  val source1: List[List[Int]] = List(List(1, 2), List(3, 4))
  val source2: List[Int] = List(1, 2)

  import Container1._
  val result1a: List[List[String]] = source1.xxx(f)
  println(result1a)

  import Container2._
  val result2a: List[String] = source2.xxx(f)
  println(result2a)

}

Upvotes: 1

Related Questions