문제

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.

도움이 되었습니까?

해결책 2

I figured out one.

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

다른 팁

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))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top