Question

I have the following code.

let s1 = [(12, "abcde12345"); (23, "bcdef2345"); (12, "xyzafg3838")]
let s2 = ["bcd"; "345"]

What's the best way to find all items in s1 which second item has sub-string of any one in s2?

(12, "abcde12345"); (23, "bcdef2345")

In my real code s1 is a Seq.

Was it helpful?

Solution 2

I figured out one.

s1 |> Seq.filter (fun i -> List.exists (fun e -> (snd i).Contains(e)) s2)

OTHER TIPS

Seq.filter (fun (_, x) -> List.exists (x.Contains) s2) s1

Concat all of the items from the second set into a regular expression, then apply it on each item in the first set.

open System
open System.Text.RegularExpressions

let setA  = [ "One"; "Two"; "Three" ]
let setB = [ "o"; "n" ];

let pattern = String.Join("|", setB);
let regex = new Regex(pattern);

let results = setA |> List.filter (fun str -> regex.Match(str).Success)

results |> List.iter (fun result -> Console.WriteLine(result))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top