0

There's not much to add here. Of course I'm able to get it done somehow with an imperative coding style, but I'm curious how this can be solved nicely.

def this( arguments: Seq[(String, String)], merges: Seq[(String, String)]) = this
{
    val metadata: Seq[Attribute] = ( arguments ++ merges ).groupBy( _._1 ).map
    {
        case (key, value) => Attribute( None, key, Text( value.map( _._2 ).mkString( " " ) ), Null )
    }

    var result: MetaData = Null

    for( m <- metadata ) result = result.append( m )

    result
}
Taig
  • 6,718
  • 4
  • 44
  • 65

1 Answers1

2

How about this?

metadata.fold(Null)((soFar, attr) => soFar append attr)

So you can do away with intermediate variable altogether if you are so inclined. In full:

def foo(arguments: Seq[(String, String)], merges: Seq[(String, String)]) =
  (arguments ++ merges).groupBy(_._1).map {
    case (key, value) => Attribute(None, key, Text(value.map(_._2).mkString(" ")), Null)
  }.fold(Null)((soFar, attr) => soFar append attr)
Faiz
  • 16,025
  • 5
  • 48
  • 37
  • Thanks for pointing me at fold. I've been overlooking this method so far struggling around with foldLeft and ugly type errors. `.fold( Null )( _ append _ )` is just what I need. – Taig Mar 04 '13 at 07:12