title: Simple composition of implicits with Monoids in Scala
body: <p>I have the following trait:</p>
  
  <pre><code>import scalaz.Monoid
  
  trait Mapper[M, R] {
    def map(m: M): R
  }
  
  object Mapper {
    @inline implicit def listMapper[M, R]
              (implicit mapper: Mapper[M, R], s: Monoid[R]): Mapper[List[M], R] =
      (xs: List[M]) =&gt; xs. foldLeft(s.zero)((r, m) =&gt; s.append(r, mapper.map(m)))
  }
  </code></pre>
  
  <p>Now I want to list mapper for with <code>R = String</code> that produces something like the following <code>[mapped_string1, mapped_string2]</code> or 
  <code>$%%""mapped_string1, mapped_string2""%%$</code>.</p>
  
  <p>The question is the following monoid implementation will not work:</p>
  
  <pre><code>implicit val myStringMonoid: Monoid[String] = new Monoid[String] {
    override def zero = ""
    override def append(f1: String, f2: =&gt; String) =
      if (f1.isEmpty) f2
      else if(f2.isEmpty) f1
      else f1 + ", " + f2
  }
  </code></pre>
  
  <p>So the following line</p>
  
  <pre><code>println(implicitly[Mapper[List[String], String]].map(List("mapped_string1", "mapped_string2")))
  </code></pre>
  
  <p>prints <code>mapped_string1, mapped_string2</code> <strong><em>without angle brackets</em></strong>.</p>
  
  <p>What would be a solution for that case? Maybe just monoids does quite fit my needs. Maybe I need another level of abstraction.</p>
  
  <p>I mean how to generally add some additional operation to be invoked after <code>foldLeft</code> is finished? Without coupling to <code>String</code> or any particular type.</p>