@@ -2179,9 +2179,8 @@ module Array =
2179
2179
// Not exists $condition <==> (opposite of $condition is true forall)
2180
2180
exists ( predicate >> not ) array |> not
2181
2181
2182
- [<CompiledName( " TryFindIndex" ) >]
2183
- let tryFindIndex predicate ( array : _ array ) =
2184
- checkNonNull " array" array
2182
+ let inline tryFindIndexAux predicate ( array : _ array ) =
2183
+ checkNonNull ( nameof array) array
2185
2184
2186
2185
let pResult =
2187
2186
Parallel.For(
@@ -2192,32 +2191,38 @@ module Array =
2192
2191
pState.Break())
2193
2192
)
2194
2193
2195
- pResult.LowestBreakIteration |> Option.ofNullable |> Option.map int
2194
+ pResult.LowestBreakIteration
2195
+
2196
+ [<CompiledName( " TryFindIndex" ) >]
2197
+ let tryFindIndex predicate ( array : _ array ) =
2198
+ let i = tryFindIndexAux predicate array
2199
+ if i.HasValue then Some ( int ( i.GetValueOrDefault()))
2200
+ else None
2196
2201
2197
2202
[<CompiledName( " TryFind" ) >]
2198
2203
let tryFind predicate ( array : _ array ) =
2199
- array |> tryFindIndex predicate |> Option.map ( fun i -> array[ i])
2204
+ let i = tryFindIndexAux predicate array
2205
+ if i.HasValue then Some array[ int ( i.GetValueOrDefault())]
2206
+ else None
2200
2207
2201
2208
[<CompiledName( " TryPick" ) >]
2202
2209
let tryPick chooser ( array : _ array ) =
2203
2210
checkNonNull " array" array
2204
- let allChosen = new System.Collections.Concurrent.ConcurrentDictionary <_, _>()
2211
+ let mutable chosen = None
2205
2212
2206
- let pResult =
2213
+ let _pResult =
2207
2214
Parallel.For(
2208
2215
0 ,
2209
2216
array.Length,
2210
2217
( fun i pState ->
2211
2218
match chooser array[ i] with
2212
2219
| None -> ()
2213
2220
| chosenElement ->
2214
- allChosen [ i ] <- chosenElement
2221
+ ignore ( Interlocked.Exchange (& chosen , chosenElement))
2215
2222
pState.Break())
2216
2223
)
2217
2224
2218
- pResult.LowestBreakIteration
2219
- |> Option.ofNullable
2220
- |> Option.bind ( fun i -> allChosen[ int i])
2225
+ chosen
2221
2226
2222
2227
[<CompiledName( " Choose" ) >]
2223
2228
let choose chooser ( array : 'T array ) =
0 commit comments