NamedMap, extract ports from map of instances

Issue #21 resolved
Ville Rantanen created an issue

With earlier Record notation i would:

val segment=Record[SegmentSeeded]("segment")
for ( i <- 1 to 3) {
  segment(i)=SegmentSeeded(in= .... ,
                    parameters....)
}
val segment_mask=Array2Folder(rec2Array(segment,"mask") , fileMode=".")
val segment_perimeter=Array2Folder(rec2Array(segment,"perimeter") , fileMode=".")

The above would pick the Record of component instances "segment" and find the output port "mask" from each of them, and create an array of those. And then same for "perimeter".

I know it's basically a mapping operation, like this: makeArray((for ((k,v) <- rec) yield (k,v(portName))).toSeq:_*)

it is such an often used structure - should there be a simplified notation for it?

Comments (3)

  1. Kristian Ovaska

    Yes. Scala Map supports creating such submaps with mapValues. Here is an example of extracting the attribute String.size.

    val myMap = Map("key1" -> "x", "key2" -> "abc")
    println(myMap mapValues {_.size})
    // Map(key1 -> 1, key2 -> 3)
    

    It's a good idea to read Scaladocs for Map and Seq, they contains lots of useful methods.

  2. Ville Rantanen reporter

    okay, so

    val segment_mask=Array2Folder( segment mapValues {_.mask} , fileMode=".")
    

    works then

  3. Log in to comment