Я не знаю производительности RegEx, но вот еще одна альтернатива для RegEx использует собственный HashSet и работает в сложности O (max (str.length, delimeter.length)):
var multiSplit = function(str,delimiter){
if (!(delimiter instanceof Array))
return str.split(delimiter);
if (!delimiter || delimiter.length == 0)
return [str];
var hashSet = new Set(delimiter);
if (hashSet.has(""))
return str.split("");
var lastIndex = 0;
var result = [];
for(var i = 0;i<str.length;i++){
if (hashSet.has(str[i])){
result.push(str.substring(lastIndex,i));
lastIndex = i+1;
}
}
result.push(str.substring(lastIndex));
return result;
}
multiSplit('1,2,3.4.5.6 7 8 9',[',','.',' ']);
// Output: ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
multiSplit('1,2,3.4.5.6 7 8 9',' ');
// Output: ["1,2,3.4.5.6", "7", "8", "9"]
Вы можете сделать
let xs = ["1";"2"]
let ys = ["a";"b";"c"]
let zs = ["1";"2";"3";"4"]
let combinations =
xs
|> List.collect (fun x ->
ys
|> List.collect (fun y ->
zs
|> List.map (fun z -> x + y + z)))
Вопрос в том, что это намного лучше, чем ваше решение.
Создание @TeaDrivenDev, поместив имя коллекции (xs
) в ту же строку, что и операция (List.
), делает ее более эквивалентной.
let combinations =
xs |> List.collect (fun x ->
ys |> List.collect (fun y ->
zs |> List.map (fun z -> x + y + z)))
let combinations2 =
[ for x in xs do
for y in ys do
for z in zs do
yield x + y + z ]