let highs = [| 2; 4; 6 |]
let lows = [| 1; 5; 10 |]
Run Code Online (Sandbox Code Playgroud)
我想从上面得到2个数组:如果highs中的元素小于lows中的相应元素,则交换它们.所以,我可以得到最后的2个数组:
let trueHighs = [| 2; 5; 10 |]
let trueLows = [| 1; 4; 6 |]
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
与JaredPar的答案类似,但更简单:
let trueHighs, trueLows =
Array.zip highs lows
|> Array.map (fun (x, y) -> if x >= y then (x, y) else (y, x))
|> Array.unzip
Run Code Online (Sandbox Code Playgroud)
另一个更简洁的版本:
let trueHighs, trueLows =
(highs, lows)
||> Array.map2 (fun x y -> if x >= y then (x, y) else (y, x))
|> Array.unzip
Run Code Online (Sandbox Code Playgroud)