@@ -188,9 +188,12 @@ let indexed (source: 'T[]) =
188
188
target.[ i] <- ( i, source.[ i])
189
189
target
190
190
191
- // let truncate (count: int) (source: 'T[]): 'T[] =
192
- // let count = max 0 count
193
- // subArrayImpl source 0 count
191
+ let truncate ( count : int ) ( source : 'T []): 'T [] =
192
+ let count =
193
+ if count < 0 then 0
194
+ elif count > source.Length then source.Length
195
+ else count
196
+ getSubArray source 0 count
194
197
195
198
// let concat (arrays: 'T[] seq) ([<Inject>] cons: Cons<'T>): 'T[] =
196
199
// let arrays =
@@ -288,7 +291,7 @@ let initialize count initializer =
288
291
// if count = 0 then
289
292
// allocateArrayFromCons cons 0
290
293
// else
291
- // subArrayImpl source 0 count
294
+ // getSubArray source 0 count
292
295
293
296
// let takeWhile predicate (source: 'T[]) ([<Inject>] cons: Cons<'T>) =
294
297
// let mutable count = 0
@@ -297,7 +300,7 @@ let initialize count initializer =
297
300
// if count = 0 then
298
301
// allocateArrayFromCons cons 0
299
302
// else
300
- // subArrayImpl source 0 count
303
+ // getSubArray source 0 count
301
304
302
305
// let addInPlace (x: 'T) (source: 'T[]) =
303
306
// // if isTypedArrayImpl source then invalidArg "source" "Typed arrays not supported"
@@ -393,86 +396,81 @@ let initialize count initializer =
393
396
// iFalse <- iFalse + 1
394
397
// res1 |> truncate iTrue, res2 |> truncate iFalse
395
398
396
- // let find (predicate: 'T -> bool) (source: 'T[]): 'T =
397
- // match findImpl predicate source with
398
- // | Some res -> res
399
- // | None -> indexNotFound()
400
-
401
- // let tryFind (predicate: 'T -> bool) (source: 'T[]): 'T option =
402
- // findImpl predicate source
403
-
404
- // let findIndex (predicate: 'T -> bool) (source: 'T[]): int =
405
- // match findIndexImpl predicate source with
406
- // | index when index > -1 -> index
407
- // | _ -> indexNotFound()
408
-
409
- // let tryFindIndex (predicate: 'T -> bool) (source: 'T[]): int option =
410
- // match findIndexImpl predicate source with
411
- // | index when index > -1 -> Some index
412
- // | _ -> None
413
-
414
- // let pick chooser (source: 'T[]) =
415
- // let rec loop i =
416
- // if i >= source.Length then
417
- // indexNotFound()
418
- // else
419
- // match chooser source.[i] with
420
- // | None -> loop(i+1)
421
- // | Some res -> res
422
- // loop 0
423
-
424
- // let tryPick chooser (source: 'T[]) =
425
- // let rec loop i =
426
- // if i >= source.Length then None else
427
- // match chooser source.[i] with
428
- // | None -> loop(i+1)
429
- // | res -> res
430
- // loop 0
431
-
432
- // let findBack predicate (source: 'T[]) =
433
- // let rec loop i =
434
- // if i < 0 then indexNotFound()
435
- // elif predicate source.[i] then source.[i]
436
- // else loop (i - 1)
437
- // loop (source.Length - 1)
438
-
439
- // let tryFindBack predicate (source: 'T[]) =
440
- // let rec loop i =
441
- // if i < 0 then None
442
- // elif predicate source.[i] then Some source.[i]
443
- // else loop (i - 1)
444
- // loop (source.Length - 1)
445
-
446
- // let findLastIndex predicate (source: 'T[]) =
447
- // let rec loop i =
448
- // if i < 0 then -1
449
- // elif predicate source.[i] then i
450
- // else loop (i - 1)
451
- // loop (source.Length - 1)
452
-
453
- // let findIndexBack predicate (source: 'T[]) =
454
- // let rec loop i =
455
- // if i < 0 then indexNotFound()
456
- // elif predicate source.[i] then i
457
- // else loop (i - 1)
458
- // loop (source.Length - 1)
459
-
460
- // let tryFindIndexBack predicate (source: 'T[]) =
461
- // let rec loop i =
462
- // if i < 0 then None
463
- // elif predicate source.[i] then Some i
464
- // else loop (i - 1)
465
- // loop (source.Length - 1)
466
-
467
- // let choose (chooser: 'T->'U option) (source: 'T[]) ([<Inject>] cons: Cons<'U>) =
468
- // let res: 'U[] = [||]
469
- // for i = 0 to source.Length - 1 do
470
- // match chooser source.[i] with
471
- // | None -> ()
472
- // | Some y -> pushImpl res y |> ignore
473
- // match box cons with
474
- // | null -> res // avoid extra copy
475
- // | _ -> map id res cons
399
+ let tryFind ( predicate : 'T -> bool ) ( source : 'T []): 'T option =
400
+ let rec loop i ( source : 'T []) =
401
+ if i >= source.Length then None
402
+ elif predicate source.[ i] then Some source.[ i]
403
+ else loop ( i + 1 ) source
404
+ loop 0 source
405
+
406
+ let find ( predicate : 'T -> bool ) ( source : 'T []): 'T =
407
+ match tryFind predicate source with
408
+ | Some res -> res
409
+ | None -> indexNotFound()
410
+
411
+ let tryFindIndex ( predicate : 'T -> bool ) ( source : 'T []): int option =
412
+ let rec loop i ( source : 'T []) =
413
+ if i >= source.Length then None
414
+ elif predicate source.[ i] then Some i
415
+ else loop ( i + 1 ) source
416
+ loop 0 source
417
+
418
+ let findIndex ( predicate : 'T -> bool ) ( source : 'T []): int =
419
+ match tryFindIndex predicate source with
420
+ | Some res -> res
421
+ | None -> indexNotFound()
422
+
423
+ let tryFindBack predicate ( source : 'T []) =
424
+ let rec loop i ( source : 'T []) =
425
+ if i < 0 then None
426
+ elif predicate source.[ i] then Some source.[ i]
427
+ else loop ( i - 1 ) source
428
+ loop ( source.Length - 1 ) source
429
+
430
+ let findBack predicate ( source : 'T []) =
431
+ match tryFindBack predicate source with
432
+ | Some res -> res
433
+ | None -> indexNotFound()
434
+
435
+ let tryFindIndexBack predicate ( source : 'T []) =
436
+ let rec loop i ( source : 'T []) =
437
+ if i < 0 then None
438
+ elif predicate source.[ i] then Some i
439
+ else loop ( i - 1 ) source
440
+ loop ( source.Length - 1 ) source
441
+
442
+ let findIndexBack predicate ( source : 'T []) =
443
+ match tryFindIndexBack predicate source with
444
+ | Some res -> res
445
+ | None -> indexNotFound()
446
+
447
+ let findLastIndex predicate ( source : 'T []) =
448
+ match tryFindIndexBack predicate source with
449
+ | Some res -> res
450
+ | None -> - 1
451
+
452
+ let tryPick chooser ( source : 'T []) =
453
+ let rec loop i ( source : 'T []) =
454
+ if i >= source.Length then
455
+ None
456
+ else
457
+ match chooser source.[ i] with
458
+ | None -> loop ( i + 1 ) source
459
+ | res -> res
460
+ loop 0 source
461
+
462
+ let pick chooser ( source : 'T []) =
463
+ match tryPick chooser source with
464
+ | Some res -> res
465
+ | None -> indexNotFound()
466
+
467
+ let choose ( chooser : 'T -> 'U option ) ( source : 'T []) = //([<Inject>] cons: Cons<'U>) =
468
+ let res = ResizeArray< 'U>()
469
+ for i = 0 to source.Length - 1 do
470
+ match chooser source.[ i] with
471
+ | None -> ()
472
+ | Some y -> res.Add( y)
473
+ res.ToArray()
476
474
477
475
let fold folder ( state : 'State ) ( source : 'T []) =
478
476
let mutable acc = state
@@ -665,14 +663,14 @@ let iterateIndexed2 action (source1: 'T[]) (source2: 'T[]) =
665
663
// // add each chunk to the res
666
664
// for x = 0 to int(System.Math.Ceiling(float(source.Length) / float(chunkSize))) - 1 do
667
665
// let start = x * chunkSize
668
- // let slice = subArrayImpl source start chunkSize
666
+ // let slice = getSubArray source start chunkSize
669
667
// pushImpl res slice |> ignore
670
668
// res
671
669
672
670
// let splitAt (index: int) (source: 'T[]): 'T[] * 'T[] =
673
671
// if index < 0 || index > source.Length then
674
672
// invalidArg "index" SR.indexOutOfBounds
675
- // subArrayImpl array 0 index, skipImpl array index
673
+ // getSubArray array 0 index, skipImpl array index
676
674
677
675
// let compareWith (comparer: 'T -> 'T -> int) (source1: 'T[]) (source2: 'T[]) =
678
676
// if isNull source1 then
@@ -761,6 +759,9 @@ let reduceBack reduction (source: 'T[]) =
761
759
// Redirected to List.ofArray to avoid dependency (see Replacements)
762
760
// let toList (source: 'T[]) = List.ofArray
763
761
762
+ // Redirected to Seq.ofArray to avoid dependency (see Replacements)
763
+ // let toSeq (source: 'T[]) = Seq.ofArray
764
+
764
765
// let windowed (windowSize: int) (source: 'T[]): 'T[][] =
765
766
// if windowSize <= 0 then
766
767
// failwith "windowSize must be positive"
@@ -782,7 +783,7 @@ let reduceBack reduction (source: 'T[]) =
782
783
// for i = 0 to chunks - 1 do
783
784
// let chunkSize = if i < chunksWithExtraItem then minChunkSize + 1 else minChunkSize
784
785
// let start = i * minChunkSize + (FSharp.Core.Operators.min chunksWithExtraItem i)
785
- // let slice = subArrayImpl source start chunkSize
786
+ // let slice = getSubArray source start chunkSize
786
787
// pushImpl res slice |> ignore
787
788
// res
788
789
0 commit comments