working with arrays and filter, a filter call embedded in a filter call?

so this is a bit of a poser, I can do it without using filter, but I'd like to explore the possibilities.


consider an array of arrays :

var array01 : [String] = ["one","two","three","four","bob","mary","adam","cecil","lambert","fitswidget","oar","twelve"]
var array02 : [String] = ["one","two","three","four","marley","dee","sevil","pour","cdc","astec","plar","been"]
var array03 : [String] = ["one","two","three","four","rent","on","gret","jesus","wonder","see","mixed","poop"]
var array04 : [String] = ["one","two","three","four","gidget","oops","fentonil","john","cerci","may","pole","gouge"]
var array05 : [String] = ["one","two","three","four"]

var glomArray = [array01, array02, array03, array04, array05]

the goal is to get an array of elements that are common to all o fthe subArrays.


we can get a flat map of the glomArray:

let fmap = glomArray.flatMap({$0})


then we can count how many times each element of array01 shows up in the fmap array (no code because it's fairly obvious)


I was wondering if I could do this with a filter. and what I came up with won't work. But could it work?

var hitArray = array01.filter({ fmap.filter({$0 == $0}).count == 5 })


what I'm trying to do is filter array01, and count how many times it shows up in fmap. if the element shows up 5 times, the evaluation is true, and that element is included in the array.

I could easily write a test function, that would evaluate that. What I'm nterested in here, is whether or not it is possible to perform this calulation with a filter, embedded inside another filter?

You know Swift compiler cannot distinguish two `$0`s, one for outer `array01`, another for `fmap`.

You can use explicit argument for a closure than using an implicit argument like `$0`.


let hitArray = array01.filter{outerElt in fmap.filter{outerElt == $0}.count == 5 }


This may not be what you are exploring, but I would write something similar using `Set`.


let commonSet: Set<String> = glomArray.reduce(into: Set(glomArray[0])) {$0.formIntersection($1)}
let hitArray = array01.filter{commonSet.contains($0)}

this is a fishing expedition for me, so any information is helpful.

What i would have done before today would have looked very similar to what you could find in a C program. This puts me further ahead.

thanks.

working with arrays and filter, a filter call embedded in a filter call?
 
 
Q