orx-parallel 3.4.0

High performance, configurable and expressive parallel computation library.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
use crate::default_fns::{map_count, reduce_sum, reduce_unit};
use crate::runner::{DefaultRunner, ParallelRunner};
use crate::{
    ChunkSize, IterationOrder, NumThreads, ParCollectInto, ParThreadPool, RunnerWithPool, Sum,
};
use core::cmp::Ordering;

/// A parallel iterator for which the computation either completely succeeds,
/// or fails and **early exits** with None.
///
/// # Examples
///
/// To demonstrate the difference of fallible iterator's behavior, consider the following simple example.
/// We parse a series of strings into integers.
/// We try this twice:
/// * in the first one, all inputs are good, hence, we obtain Some of parsed numbers,
/// * in the second one, the value in the middle is faulty, we expect the computation to fail.
///
/// In the following, we try to achieve this both with a regular parallel iterator ([`ParIter`]) and a fallible
/// parallel iterator, `ParIterOption` in this case.
///
/// You may notice the following differences:
/// * In the regular iterator, it is not very convenient to keep both the resulting numbers and a potential error.
///   Here, we make use of `filter_map`.
/// * On the other hand, the `collect` method of the fallible iterator directly returns an `Option` of the computation
///   which is either Some of all parsed numbers or None if any computation fails.
/// * Also importantly note that the regular iterator will try to parse all the strings, regardless of how many times
///   the parsing fails.
/// * Fallible iterator, on the other hand, stops immediately after observing the first None and short circuits the
///   computation.
///
/// ```
/// use orx_parallel::*;
///
/// let expected_results = [Some((0..100).collect::<Vec<_>>()), None];
///
/// for expected in expected_results {
///     let expected_some = expected.is_some();
///     let mut inputs: Vec<_> = (0..100).map(|x| x.to_string()).collect();
///     if !expected_some {
///         inputs.insert(50, "x".to_string()); // plant an error case
///     }
///
///     // regular parallel iterator
///     let results = inputs.par().map(|x| x.parse::<u32>().ok());
///     let numbers: Vec<_> = results.filter_map(|x| x).collect();
///     if expected_some {
///         assert_eq!(&expected, &Some(numbers));
///     } else {
///         // otherwise, numbers contains some numbers, but we are not sure
///         // if the computation completely succeeded or not
///     }
///
///     // fallible parallel iterator
///     let results = inputs.par().map(|x| x.parse::<u32>().ok());
///     let result: Option<Vec<_>> = results.into_fallible_option().collect();
///     assert_eq!(&expected, &result);
/// }
/// ```
///
/// These differences are not specific to `collect`; all fallible iterator methods return an option.
/// The following demonstrate reduction examples, where the result is either the reduced value if the entire computation
/// succeeds, or None.
///
/// ```
/// use orx_parallel::*;
///
/// for will_fail in [false, true] {
///     let mut inputs: Vec<_> = (0..100).map(|x| x.to_string()).collect();
///     if will_fail {
///         inputs.insert(50, "x".to_string()); // plant an error case
///     }
///
///     // sum
///     let results = inputs.par().map(|x| x.parse::<u32>().ok());
///     let result: Option<u32> = results.into_fallible_option().sum();
///     match will_fail {
///         true => assert_eq!(result, None),
///         false => assert_eq!(result, Some(4950)),
///     }
///
///     // max
///     let results = inputs.par().map(|x| x.parse::<u32>().ok());
///     let result: Option<Option<u32>> = results.into_fallible_option().max();
///     match will_fail {
///         true => assert_eq!(result, None),
///         false => assert_eq!(result, Some(Some(99))),
///     }
/// }
/// ```
///
/// Finally, similar to regular iterators, a fallible parallel iterator can be tranformed using iterator methods.
/// However, the transformation is on the success path, the failure case of None always short circuits and returns None.
///
/// ```
/// use orx_parallel::*;
///
/// for will_fail in [false, true] {
///     let mut inputs: Vec<_> = (0..100).map(|x| x.to_string()).collect();
///     if will_fail {
///         inputs.insert(50, "x".to_string()); // plant an error case
///     }
///
///     // fallible iter
///     let results = inputs.par().map(|x| x.parse::<u32>().ok());
///     let fallible = results.into_fallible_option();
///
///     // transformations
///
///     let result: Option<usize> = fallible
///         .filter(|x| x % 2 == 1)                                 // Item: u32
///         .map(|x| 3 * x)                                         // Item: u32
///         .filter_map(|x| (x % 10 != 0).then_some(x))             // Item: u32
///         .flat_map(|x| [x.to_string(), (10 * x).to_string()])    // Item: String
///         .map(|x| x.len())                                       // Item: usize
///         .sum();
///
///     match will_fail {
///         true => assert_eq!(result, None),
///         false => assert_eq!(result, Some(312)),
///     }
/// }
/// ```
///
/// [`ParIter`]: crate::ParIter
pub trait ParIterOption<R = DefaultRunner>
where
    R: ParallelRunner,
{
    /// Type of the success element, to be received as the Some variant iff the entire computation succeeds.
    type Item;

    // params transformations

    /// Sets the number of threads to be used in the parallel execution.
    /// Integers can be used as the argument with the following mapping:
    ///
    /// * `0` -> `NumThreads::Auto`
    /// * `1` -> `NumThreads::sequential()`
    /// * `n > 0` -> `NumThreads::Max(n)`
    ///
    /// See [`NumThreads`] and [`crate::ParIter::num_threads`] for details.
    fn num_threads(self, num_threads: impl Into<NumThreads>) -> Self;

    /// Sets the number of elements to be pulled from the concurrent iterator during the
    /// parallel execution. When integers are used as argument, the following mapping applies:
    ///
    /// * `0` -> `ChunkSize::Auto`
    /// * `n > 0` -> `ChunkSize::Exact(n)`
    ///
    /// Please use the default enum constructor for creating `ChunkSize::Min` variant.
    ///
    /// See [`ChunkSize`] and [`crate::ParIter::chunk_size`] for details.
    fn chunk_size(self, chunk_size: impl Into<ChunkSize>) -> Self;

    /// Sets the iteration order of the parallel computation.
    ///
    /// See [`IterationOrder`] and [`crate::ParIter::iteration_order`] for details.
    fn iteration_order(self, order: IterationOrder) -> Self;

    /// Rather than the [`DefaultRunner`], uses the parallel runner `Q` which implements [`ParallelRunner`].
    ///
    /// See [`ParIter::with_runner`] for details.
    ///
    /// [`DefaultRunner`]: crate::DefaultRunner
    /// [`ParIter::with_runner`]: crate::ParIter::with_runner
    fn with_runner<Q: ParallelRunner>(
        self,
        orchestrator: Q,
    ) -> impl ParIterOption<Q, Item = Self::Item>;

    /// Rather than [`DefaultPool`], uses the parallel runner with the given `pool` implementing
    /// [`ParThreadPool`].
    ///
    /// See [`ParIter::with_pool`] for details.
    ///
    /// [`DefaultPool`]: crate::DefaultPool
    /// [`ParIter::with_pool`]: crate::ParIter::with_pool
    fn with_pool<P: ParThreadPool>(
        self,
        pool: P,
    ) -> impl ParIterOption<RunnerWithPool<P, R::Executor>, Item = Self::Item>
    where
        Self: Sized,
    {
        let runner = RunnerWithPool::from(pool).with_executor::<R::Executor>();
        self.with_runner(runner)
    }

    // computation transformations

    /// Takes a closure `map` and creates a parallel iterator which calls that closure on each element.
    ///
    /// Transformation is only for the success path where all elements are of the `Some` variant.
    /// Any observation of a `None` case short-circuits the computation and immediately returns None.
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// // all succeeds
    /// let a: Vec<Option<u32>> = vec![Some(1), Some(2), Some(3)];
    /// let iter = a.into_par().into_fallible_option().map(|x| 2 * x);
    ///
    /// let b: Option<Vec<_>> = iter.collect();
    /// assert_eq!(b, Some(vec![2, 4, 6]));
    ///
    /// // at least one fails
    /// let a = vec![Some(1), None, Some(3)];
    /// let iter = a.into_par().into_fallible_option().map(|x| 2 * x);
    ///
    /// let b: Option<Vec<_>> = iter.collect();
    /// assert_eq!(b, None);
    /// ```
    fn map<Out, Map>(self, map: Map) -> impl ParIterOption<R, Item = Out>
    where
        Self: Sized,
        Map: Fn(Self::Item) -> Out + Sync + Clone,
        Out: Send;

    /// Creates an iterator which uses a closure `filter` to determine if an element should be yielded.
    ///
    /// Transformation is only for the success path where all elements are of the `Some` variant.
    /// Any observation of a `None` case short-circuits the computation and immediately returns None.
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// // all succeeds
    /// let a: Vec<Option<i32>> = vec![Some(1), Some(2), Some(3)];
    /// let iter = a.into_par().into_fallible_option().filter(|x| x % 2 == 1);
    ///
    /// let b = iter.sum();
    /// assert_eq!(b, Some(1 + 3));
    ///
    /// // at least one fails
    /// let a = vec![Some(1), None, Some(3)];
    /// let iter = a.into_par().into_fallible_option().filter(|x| x % 2 == 1);
    ///
    /// let b = iter.sum();
    /// assert_eq!(b, None);
    /// ```
    fn filter<Filter>(self, filter: Filter) -> impl ParIterOption<R, Item = Self::Item>
    where
        Self: Sized,
        Filter: Fn(&Self::Item) -> bool + Sync + Clone,
        Self::Item: Send;

    /// Creates an iterator that works like map, but flattens nested structure.
    ///
    /// Transformation is only for the success path where all elements are of the `Some` variant.
    /// Any observation of a `None` case short-circuits the computation and immediately returns None.
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// // all succeeds
    /// let words: Vec<Option<&str>> = vec![Some("alpha"), Some("beta"), Some("gamma")];
    ///
    /// let all_chars: Option<Vec<_>> = words
    ///     .into_par()
    ///     .into_fallible_option()
    ///     .flat_map(|s| s.chars()) // chars() returns an iterator
    ///     .collect();
    ///
    /// let merged: Option<String> = all_chars.map(|chars| chars.iter().collect());
    /// assert_eq!(merged, Some("alphabetagamma".to_string()));
    ///
    /// // at least one fails
    /// let words: Vec<Option<&str>> = vec![Some("alpha"), Some("beta"), None, Some("gamma")];
    ///
    /// let all_chars: Option<Vec<_>> = words
    ///     .into_par()
    ///     .into_fallible_option()
    ///     .flat_map(|s| s.chars()) // chars() returns an iterator
    ///     .collect();
    ///
    /// let merged: Option<String> = all_chars.map(|chars| chars.iter().collect());
    /// assert_eq!(merged, None);
    /// ```
    fn flat_map<IOut, FlatMap>(self, flat_map: FlatMap) -> impl ParIterOption<R, Item = IOut::Item>
    where
        Self: Sized,
        IOut: IntoIterator,
        IOut::Item: Send,
        FlatMap: Fn(Self::Item) -> IOut + Sync + Clone;

    /// Creates an iterator that both filters and maps.
    ///
    /// The returned iterator yields only the values for which the supplied closure `filter_map` returns `Some(value)`.
    ///
    /// `filter_map` can be used to make chains of `filter` and `map` more concise.
    /// The example below shows how a `map().filter().map()` can be shortened to a single call to `filter_map`.
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// // all succeeds
    /// let a: Vec<Option<&str>> = vec![Some("1"), Some("two"), Some("NaN"), Some("four"), Some("5")];
    ///
    /// let numbers: Option<Vec<_>> = a
    ///     .into_par()
    ///     .into_fallible_option()
    ///     .filter_map(|s| s.parse::<usize>().ok())
    ///     .collect();
    ///
    /// assert_eq!(numbers, Some(vec![1, 5]));
    ///
    /// // at least one fails
    /// let a: Vec<Option<&str>> = vec![Some("1"), Some("two"), None, Some("four"), Some("5")];
    ///
    /// let numbers: Option<Vec<_>> = a
    ///     .into_par()
    ///     .into_fallible_option()
    ///     .filter_map(|s| s.parse::<usize>().ok())
    ///     .collect();
    ///
    /// assert_eq!(numbers, None);
    /// ```
    fn filter_map<Out, FilterMap>(self, filter_map: FilterMap) -> impl ParIterOption<R, Item = Out>
    where
        Self: Sized,
        FilterMap: Fn(Self::Item) -> Option<Out> + Sync + Clone,
        Out: Send;

    /// Does something with each successful element of an iterator, passing the value on, provided that all elements are of Some variant;
    /// short-circuits and returns None otherwise.
    ///
    /// When using iterators, you’ll often chain several of them together.
    /// While working on such code, you might want to check out what’s happening at various parts in the pipeline.
    /// To do that, insert a call to `inspect()`.
    ///
    /// It’s more common for `inspect()` to be used as a debugging tool than to exist in your final code,
    /// but applications may find it useful in certain situations when errors need to be logged before being discarded.
    ///
    /// It is often convenient to use thread-safe collections such as [`ConcurrentBag`] and
    /// [`ConcurrentVec`](https://crates.io/crates/orx-concurrent-vec) to
    /// collect some intermediate values during parallel execution for further inspection.
    /// The following example demonstrates such a use case.
    ///
    /// [`ConcurrentBag`]: orx_concurrent_bag::ConcurrentBag
    ///
    /// ```
    /// use orx_parallel::*;
    /// use orx_concurrent_bag::*;
    /// use std::num::ParseIntError;
    ///
    /// // all succeeds
    /// let a: Vec<Option<u32>> = ["1", "4", "2", "3"]
    ///     .into_iter()
    ///     .map(|x| x.parse::<u32>().ok())
    ///     .collect();
    ///
    /// // let's add some inspect() calls to investigate what's happening
    /// // - log some events
    /// // - use a concurrent bag to collect and investigate numbers contributing to the sum
    /// let bag = ConcurrentBag::new();
    ///
    /// let sum = a
    ///     .par()
    ///     .cloned()
    ///     .into_fallible_option()
    ///     .inspect(|x| println!("about to filter: {x}"))
    ///     .filter(|x| x % 2 == 0)
    ///     .inspect(|x| {
    ///         bag.push(*x);
    ///         println!("made it through filter: {x}");
    ///     })
    ///     .sum();
    /// assert_eq!(sum, Some(4 + 2));
    ///
    /// let mut values_made_through = bag.into_inner();
    /// values_made_through.sort();
    /// assert_eq!(values_made_through, [2, 4]);
    ///
    /// // at least one fails
    /// let a: Vec<Option<u32>> = ["1", "4", "x", "3"]
    ///     .into_iter()
    ///     .map(|x| x.parse::<u32>().ok())
    ///     .collect();
    ///
    /// // let's add some inspect() calls to investigate what's happening
    /// // - log some events
    /// // - use a concurrent bag to collect and investigate numbers contributing to the sum
    /// let bag = ConcurrentBag::new();
    ///
    /// let sum = a
    ///     .par()
    ///     .cloned()
    ///     .into_fallible_option()
    ///     .inspect(|x| println!("about to filter: {x}"))
    ///     .filter(|x| x % 2 == 0)
    ///     .inspect(|x| {
    ///         bag.push(*x);
    ///         println!("made it through filter: {x}");
    ///     })
    ///     .sum();
    /// assert_eq!(sum, None);
    /// ```
    fn inspect<Operation>(self, operation: Operation) -> impl ParIterOption<R, Item = Self::Item>
    where
        Self: Sized,
        Operation: Fn(&Self::Item) + Sync + Clone,
        Self::Item: Send;

    // collect

    /// Collects all the items from an iterator into a collection iff all elements are of Some variant.
    /// Early exits and returns None if any of the elements is None.
    ///
    /// This is useful when you already have a collection and want to add the iterator items to it.
    ///
    /// The collection is passed in as owned value, and returned back with the additional elements.
    ///
    /// All collections implementing [`ParCollectInto`] can be used to collect into.
    ///
    /// [`ParCollectInto`]: crate::ParCollectInto
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// let vec: Vec<i32> = vec![0, 1];
    ///
    /// // all succeeds
    /// let result = ["1", "2", "3"]
    ///     .into_par()
    ///     .map(|x| x.parse::<i32>().ok())
    ///     .into_fallible_option()
    ///     .map(|x| x * 10)
    ///     .collect_into(vec);
    /// assert_eq!(result, Some(vec![0, 1, 10, 20, 30]));
    ///
    /// let vec = result.unwrap();
    ///
    /// // at least one fails
    ///
    /// let result = ["1", "x!", "3"]
    ///     .into_par()
    ///     .map(|x| x.parse::<i32>().ok())
    ///     .into_fallible_option()
    ///     .map(|x| x * 10)
    ///     .collect_into(vec);
    /// assert_eq!(result, None);
    /// ```
    fn collect_into<C>(self, output: C) -> Option<C>
    where
        Self::Item: Send,
        C: ParCollectInto<Self::Item>;

    /// Transforms an iterator into a collection iff all elements are of Ok variant.
    /// Early exits and returns the error if any of the elements is an Err.
    ///
    /// Similar to [`Iterator::collect`], the type annotation on the left-hand-side determines
    /// the type of the result collection; or turbofish annotation can be used.
    ///
    /// All collections implementing [`ParCollectInto`] can be used to collect into.
    ///
    /// [`ParCollectInto`]: crate::ParCollectInto
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// // all succeeds
    ///
    /// let result_doubled: Option<Vec<i32>> = ["1", "2", "3"]
    ///     .into_par()
    ///     .map(|x| x.parse::<i32>().ok())
    ///     .into_fallible_option()
    ///     .map(|x| x * 2)
    ///     .collect();
    ///
    /// assert_eq!(result_doubled, Some(vec![2, 4, 6]));
    ///
    /// // at least one fails
    ///
    /// let result_doubled: Option<Vec<i32>> = ["1", "x!", "3"]
    ///     .into_par()
    ///     .map(|x| x.parse::<i32>().ok())
    ///     .into_fallible_option()
    ///     .map(|x| x * 2)
    ///     .collect();
    ///
    /// assert_eq!(result_doubled, None);
    /// ```
    fn collect<C>(self) -> Option<C>
    where
        Self::Item: Send,
        C: ParCollectInto<Self::Item>;

    // reduce

    /// Reduces the elements to a single one, by repeatedly applying a reducing operation.
    /// Early exits and returns None if any of the elements is None.
    ///
    /// If the iterator is empty, returns `Some(None)`; otherwise, returns `Some` of result of the reduction.
    ///
    /// The `reduce` function is a closure with two arguments: an ‘accumulator’, and an element.
    ///
    /// # Example
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// // all succeeds
    /// let reduced: Option<Option<u32>> = (1..10)
    ///     .par()
    ///     .map(|x| 100u32.checked_div(x as u32))
    ///     .into_fallible_option()
    ///     .reduce(|acc, e| acc + e);
    /// assert_eq!(reduced, Some(Some(281)));
    ///
    /// // all succeeds - empty iterator
    /// let reduced: Option<Option<u32>> = (1..1)
    ///     .par()
    ///     .map(|x| 100u32.checked_div(x as u32))
    ///     .into_fallible_option()
    ///     .reduce(|acc, e| acc + e);
    /// assert_eq!(reduced, Some(None));
    ///
    /// // at least one fails
    /// let reduced: Option<Option<u32>> = (0..10)
    ///     .par()
    ///     .map(|x| 100u32.checked_div(x as u32))
    ///     .into_fallible_option()
    ///     .reduce(|acc, e| acc + e);
    /// assert_eq!(reduced, None);
    /// ```
    fn reduce<Reduce>(self, reduce: Reduce) -> Option<Option<Self::Item>>
    where
        Self::Item: Send,
        Reduce: Fn(Self::Item, Self::Item) -> Self::Item + Sync;

    /// Tests if every element of the iterator matches a predicate.
    /// Early exits and returns None if any of the elements is None.
    ///
    /// `all` takes a `predicate` that returns true or false.
    /// It applies this closure to each Ok element of the iterator,
    /// and if they all return true, then so does `all`.
    /// If any of them returns false, it returns false.
    ///
    /// Note that `all` computation is itself also short-circuiting; in other words, it will stop processing as soon as it finds a false,
    /// given that no matter what else happens, the result will also be false.
    ///
    /// Therefore, in case the fallible iterator contains both a None element and a Some element which violates the `predicate`,
    /// the result is **not deterministic**. It might be the `None` if it is observed first; or `Some(false)` if the violation is observed first.
    ///
    /// On the other hand, when it returns `Some(true)`, we are certain that all elements are of `Some` variant and all satisfy the `predicate`.
    ///
    /// An empty iterator returns `Some(true)`.
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// // all Some
    /// let result = vec!["1", "2", "3"]
    ///     .into_par()
    ///     .map(|x| x.parse::<i32>().ok())
    ///     .into_fallible_option()
    ///     .all(|x| *x > 0);
    /// assert_eq!(result, Some(true));
    ///
    /// let result = vec!["1", "2", "3"]
    ///     .into_par()
    ///     .map(|x| x.parse::<i32>().ok())
    ///     .into_fallible_option()
    ///     .all(|x| *x > 1);
    /// assert_eq!(result, Some(false));
    ///
    /// let result = Vec::<&str>::new()
    ///     .into_par()
    ///     .map(|x| x.parse::<i32>().ok())
    ///     .into_fallible_option()
    ///     .all(|x| *x > 1);
    /// assert_eq!(result, Some(true)); // empty iterator
    ///
    /// // at least one None
    /// let result = vec!["1", "x!", "3"]
    ///     .into_par()
    ///     .map(|x| x.parse::<i32>().ok())
    ///     .into_fallible_option()
    ///     .all(|x| *x > 0);
    /// assert_eq!(result, None);
    /// ```
    fn all<Predicate>(self, predicate: Predicate) -> Option<bool>
    where
        Self: Sized,
        Self::Item: Send,
        Predicate: Fn(&Self::Item) -> bool + Sync,
    {
        let violates = |x: &Self::Item| !predicate(x);
        self.find(violates).map(|x| x.is_none())
    }

    /// Tests if any element of the iterator matches a predicate.
    /// Early exits and returns None if any of the elements is None.
    ///
    /// `any` takes a `predicate` that returns true or false.
    /// It applies this closure to each element of the iterator,
    /// and if any of the elements returns true, then so does `any`.
    /// If all of them return false, it returns false.
    ///
    /// Note that `any` computation is itself also short-circuiting; in other words, it will stop processing as soon as it finds a true,
    /// given that no matter what else happens, the result will also be true.
    ///
    /// Therefore, in case the fallible iterator contains both a None element and a Some element which satisfies the `predicate`,
    /// the result is **not deterministic**. It might be the `None` if it is observed first; or `Some(true)` if element satisfying the predicate
    /// is observed first.
    ///
    /// On the other hand, when it returns `Some(false)`, we are certain that all elements are of `Some` variant and none of them satisfies the `predicate`.
    ///
    /// An empty iterator returns `Some(false)`.
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// // all Some
    /// let result = vec!["1", "2", "3"]
    ///     .into_par()
    ///     .map(|x| x.parse::<i32>().ok())
    ///     .into_fallible_option()
    ///     .any(|x| *x > 1);
    /// assert_eq!(result, Some(true));
    ///
    /// let result = vec!["1", "2", "3"]
    ///     .into_par()
    ///     .map(|x| x.parse::<i32>().ok())
    ///     .into_fallible_option()
    ///     .any(|x| *x > 3);
    /// assert_eq!(result, Some(false));
    ///
    /// let result = Vec::<&str>::new()
    ///     .into_par()
    ///     .map(|x| x.parse::<i32>().ok())
    ///     .into_fallible_option()
    ///     .any(|x| *x > 1);
    /// assert_eq!(result, Some(false)); // empty iterator
    ///
    /// // at least one None
    /// let result = vec!["1", "x!", "3"]
    ///     .into_par()
    ///     .map(|x| x.parse::<i32>().ok())
    ///     .into_fallible_option()
    ///     .any(|x| *x > 5);
    /// assert_eq!(result, None);
    /// ```
    fn any<Predicate>(self, predicate: Predicate) -> Option<bool>
    where
        Self: Sized,
        Self::Item: Send,
        Predicate: Fn(&Self::Item) -> bool + Sync,
    {
        self.find(predicate).map(|x| x.is_some())
    }

    /// Consumes the iterator, counting the number of iterations and returning it.
    /// Early exits and returns None if any of the elements is None.
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// // all Some
    /// let result = vec!["1", "2", "3"]
    ///     .into_par()
    ///     .map(|x| x.parse::<i32>().ok())
    ///     .into_fallible_option()
    ///     .filter(|x| *x >= 2)
    ///     .count();
    /// assert_eq!(result, Some(2));
    ///
    /// // at least one None
    /// let result = vec!["x!", "2", "3"]
    ///     .into_par()
    ///     .map(|x| x.parse::<i32>().ok())
    ///     .into_fallible_option()
    ///     .filter(|x| *x >= 2)
    ///     .count();
    /// assert_eq!(result, None);
    /// ```
    fn count(self) -> Option<usize>
    where
        Self: Sized,
    {
        self.map(map_count)
            .reduce(reduce_sum)
            .map(|x| x.unwrap_or(0))
    }

    /// Calls a closure on each element of an iterator, and returns `Ok(())` if all elements succeed.
    /// Early exits and returns None if any of the elements is None.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use orx_parallel::*;
    /// use std::sync::mpsc::channel;
    ///
    /// // all Some
    /// let (tx, rx) = channel();
    /// let result = vec!["0", "1", "2", "3", "4"]
    ///     .into_par()
    ///     .map(|x| x.parse::<i32>().ok())
    ///     .into_fallible_option()
    ///     .map(|x| x * 2 + 1)
    ///     .for_each(move |x| tx.send(x).unwrap());
    ///
    /// assert_eq!(result, Some(()));
    ///
    /// let mut v: Vec<_> = rx.iter().collect();
    /// v.sort(); // order can be mixed, since messages will be sent in parallel
    /// assert_eq!(v, vec![1, 3, 5, 7, 9]);
    ///
    /// // at least one None
    /// let (tx, _rx) = channel();
    /// let result = vec!["0", "1", "2", "x!", "4"]
    ///     .into_par()
    ///     .map(|x| x.parse::<i32>().ok())
    ///     .into_fallible_option()
    ///     .map(|x| x * 2 + 1)
    ///     .for_each(move |x| tx.send(x).unwrap());
    ///
    /// assert_eq!(result, None);
    /// ```
    fn for_each<Operation>(self, operation: Operation) -> Option<()>
    where
        Self: Sized,
        Operation: Fn(Self::Item) + Sync,
    {
        let map = |x| operation(x);
        self.map(map).reduce(reduce_unit).map(|_| ())
    }

    /// Returns Some of maximum element of an iterator if all elements succeed.
    /// If the iterator is empty, `Some(None)` is returned.
    /// Early exits and returns None if any of the elements is None.
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// let a = vec![Some(1), Some(2), Some(3)];
    /// assert_eq!(a.par().copied().into_fallible_option().max(), Some(Some(3)));
    ///
    /// let b: Vec<Option<i32>> = vec![];
    /// assert_eq!(b.par().copied().into_fallible_option().max(), Some(None));
    ///
    /// let c = vec![Some(1), Some(2), None];
    /// assert_eq!(c.par().copied().into_fallible_option().max(), None);
    /// ```
    fn max(self) -> Option<Option<Self::Item>>
    where
        Self: Sized,
        Self::Item: Ord + Send,
    {
        self.reduce(Ord::max)
    }

    /// Returns the element that gives the maximum value with respect to the specified `compare` function.
    /// If the iterator is empty, `Some(None)` is returned.
    /// Early exits and returns None if any of the elements is None.
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// let a: Vec<Option<i32>> = vec![Some(1), Some(2), Some(3)];
    /// assert_eq!(
    ///     a.par()
    ///         .copied()
    ///         .into_fallible_option()
    ///         .max_by(|a, b| a.cmp(b)),
    ///     Some(Some(3))
    /// );
    ///
    /// let b: Vec<Option<i32>> = vec![];
    /// assert_eq!(
    ///     b.par()
    ///         .copied()
    ///         .into_fallible_option()
    ///         .max_by(|a, b| a.cmp(b)),
    ///     Some(None)
    /// );
    ///
    /// let c: Vec<Option<i32>> = vec![Some(1), Some(2), None];
    /// assert_eq!(
    ///     c.par()
    ///         .copied()
    ///         .into_fallible_option()
    ///         .max_by(|a, b| a.cmp(b)),
    ///     None
    /// );
    /// ```
    fn max_by<Compare>(self, compare: Compare) -> Option<Option<Self::Item>>
    where
        Self: Sized,
        Self::Item: Send,
        Compare: Fn(&Self::Item, &Self::Item) -> Ordering + Sync,
    {
        let reduce = |x, y| match compare(&x, &y) {
            Ordering::Greater | Ordering::Equal => x,
            Ordering::Less => y,
        };
        self.reduce(reduce)
    }

    /// Returns the element that gives the maximum value from the specified function.
    /// If the iterator is empty, `Some(None)` is returned.
    /// Early exits and returns None if any of the elements is None.
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// let a: Vec<Option<i32>> = vec![Some(-1), Some(2), Some(-3)];
    /// assert_eq!(
    ///     a.par()
    ///         .copied()
    ///         .into_fallible_option()
    ///         .max_by_key(|x| x.abs()),
    ///     Some(Some(-3))
    /// );
    ///
    /// let b: Vec<Option<i32>> = vec![];
    /// assert_eq!(
    ///     b.par()
    ///         .copied()
    ///         .into_fallible_option()
    ///         .max_by_key(|x| x.abs()),
    ///     Some(None)
    /// );
    ///
    /// let c: Vec<Option<i32>> = vec![Some(1), Some(2), None];
    /// assert_eq!(
    ///     c.par()
    ///         .copied()
    ///         .into_fallible_option()
    ///         .max_by_key(|x| x.abs()),
    ///     None
    /// );
    /// ```
    fn max_by_key<Key, GetKey>(self, key: GetKey) -> Option<Option<Self::Item>>
    where
        Self: Sized,
        Self::Item: Send,
        Key: Ord,
        GetKey: Fn(&Self::Item) -> Key + Sync,
    {
        let reduce = |x, y| match key(&x).cmp(&key(&y)) {
            Ordering::Greater | Ordering::Equal => x,
            Ordering::Less => y,
        };
        self.reduce(reduce)
    }

    /// Returns Some of minimum element of an iterator if all elements succeed.
    /// If the iterator is empty, `Some(None)` is returned.
    /// Early exits and returns None if any of the elements is None.
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// let a = vec![Some(1), Some(2), Some(3)];
    /// assert_eq!(a.par().copied().into_fallible_option().min(), Some(Some(1)));
    ///
    /// let b: Vec<Option<i32>> = vec![];
    /// assert_eq!(b.par().copied().into_fallible_option().min(), Some(None));
    ///
    /// let c = vec![Some(1), Some(2), None];
    /// assert_eq!(c.par().copied().into_fallible_option().min(), None);
    /// ```
    fn min(self) -> Option<Option<Self::Item>>
    where
        Self: Sized,
        Self::Item: Ord + Send,
    {
        self.reduce(Ord::min)
    }

    /// Returns the element that gives the minimum value with respect to the specified `compare` function.
    /// If the iterator is empty, `Some(None)` is returned.
    /// Early exits and returns None if any of the elements is None.
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// let a: Vec<Option<i32>> = vec![Some(1), Some(2), Some(3)];
    /// assert_eq!(
    ///     a.par()
    ///         .copied()
    ///         .into_fallible_option()
    ///         .min_by(|a, b| a.cmp(b)),
    ///     Some(Some(1))
    /// );
    ///
    /// let b: Vec<Option<i32>> = vec![];
    /// assert_eq!(
    ///     b.par()
    ///         .copied()
    ///         .into_fallible_option()
    ///         .min_by(|a, b| a.cmp(b)),
    ///     Some(None)
    /// );
    ///
    /// let c: Vec<Option<i32>> = vec![Some(1), Some(2), None];
    /// assert_eq!(
    ///     c.par()
    ///         .copied()
    ///         .into_fallible_option()
    ///         .min_by(|a, b| a.cmp(b)),
    ///     None
    /// );
    /// ```
    fn min_by<Compare>(self, compare: Compare) -> Option<Option<Self::Item>>
    where
        Self: Sized,
        Self::Item: Send,
        Compare: Fn(&Self::Item, &Self::Item) -> Ordering + Sync,
    {
        let reduce = |x, y| match compare(&x, &y) {
            Ordering::Less | Ordering::Equal => x,
            Ordering::Greater => y,
        };
        self.reduce(reduce)
    }

    /// Returns the element that gives the minimum value from the specified function.
    /// If the iterator is empty, `Some(None)` is returned.
    /// Early exits and returns None if any of the elements is None.
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// let a: Vec<Option<i32>> = vec![Some(-1), Some(2), Some(-3)];
    /// assert_eq!(
    ///     a.par()
    ///         .copied()
    ///         .into_fallible_option()
    ///         .min_by_key(|x| x.abs()),
    ///     Some(Some(-1))
    /// );
    ///
    /// let b: Vec<Option<i32>> = vec![];
    /// assert_eq!(
    ///     b.par()
    ///         .copied()
    ///         .into_fallible_option()
    ///         .min_by_key(|x| x.abs()),
    ///     Some(None)
    /// );
    ///
    /// let c: Vec<Option<i32>> = vec![Some(1), Some(2), None];
    /// assert_eq!(
    ///     c.par()
    ///         .copied()
    ///         .into_fallible_option()
    ///         .min_by_key(|x| x.abs()),
    ///     None
    /// );
    /// ```
    fn min_by_key<Key, GetKey>(self, get_key: GetKey) -> Option<Option<Self::Item>>
    where
        Self: Sized,
        Self::Item: Send,
        Key: Ord,
        GetKey: Fn(&Self::Item) -> Key + Sync,
    {
        let reduce = |x, y| match get_key(&x).cmp(&get_key(&y)) {
            Ordering::Less | Ordering::Equal => x,
            Ordering::Greater => y,
        };
        self.reduce(reduce)
    }

    /// Sums the elements of an iterator.
    /// Early exits and returns None if any of the elements is None.
    ///
    /// If the iterator is empty, returns zero; otherwise, returns `Some` of the sum.
    ///
    /// # Example
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// // all succeeds
    /// let reduced: Option<u32> = (1..10)
    ///     .par()
    ///     .map(|x| 100u32.checked_div(x as u32))
    ///     .into_fallible_option()
    ///     .sum();
    /// assert_eq!(reduced, Some(281));
    ///
    /// // all succeeds - empty iterator
    /// let reduced: Option<u32> = (1..1)
    ///     .par()
    ///     .map(|x| 100u32.checked_div(x as u32))
    ///     .into_fallible_option()
    ///     .sum();
    /// assert_eq!(reduced, Some(0));
    ///
    /// // at least one fails
    /// let reduced: Option<u32> = (0..10)
    ///     .par()
    ///     .map(|x| 100u32.checked_div(x as u32))
    ///     .into_fallible_option()
    ///     .sum();
    /// assert_eq!(reduced, None);
    /// ```
    fn sum<Out>(self) -> Option<Out>
    where
        Self: Sized,
        Self::Item: Sum<Out>,
        Out: Send,
    {
        self.map(Self::Item::map)
            .reduce(Self::Item::reduce)
            .map(|x| x.unwrap_or(Self::Item::zero()))
    }

    // early exit

    /// Returns the first (or any) element of the iterator.
    /// If the iterator is empty, `Some(None)` is returned.
    /// Early exits and returns None if a None element is observed first.
    ///
    /// * first element is returned if default iteration order `IterationOrder::Ordered` is used,
    /// * any element is returned if `IterationOrder::Arbitrary` is set.
    ///
    /// Note that `find` itself is short-circuiting in addition to fallible computation.
    /// Therefore, in case the fallible iterator contains both a None and a Some element,
    /// the result is **not deterministic**:
    /// * it might be the `None` if it is observed first;
    /// * or `Some(element)` if the Some element is observed first.
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// let a: Vec<Option<i32>> = vec![];
    /// assert_eq!(a.par().copied().into_fallible_option().first(), Some(None));
    ///
    /// let a: Vec<Option<i32>> = vec![Some(1), Some(2), Some(3)];
    /// assert_eq!(
    ///     a.par().copied().into_fallible_option().first(),
    ///     Some(Some(1))
    /// );
    ///
    /// let a: Vec<Option<i32>> = vec![Some(1), None, Some(3)];
    /// let result = a.par().copied().into_fallible_option().first();
    /// // depends on whichever is observed first in parallel execution
    /// assert!(result == Some(Some(1)) || result == None);
    /// ```
    fn first(self) -> Option<Option<Self::Item>>
    where
        Self::Item: Send;

    /// Returns the first (or any) element of the iterator that satisfies the `predicate`.
    /// If the iterator is empty, `Some(None)` is returned.
    /// Early exits and returns None if a None element is observed first.
    ///
    /// * first element is returned if default iteration order `IterationOrder::Ordered` is used,
    /// * any element is returned if `IterationOrder::Arbitrary` is set.
    ///
    /// Note that `find` itself is short-circuiting in addition to fallible computation.
    /// Therefore, in case the fallible iterator contains both a None and a Some element,
    /// the result is **not deterministic**:
    /// * it might be the `None` if it is observed first;
    /// * or `Some(element)` if the Some element satisfying the predicate is observed first.
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_parallel::*;
    ///
    /// let a: Vec<Option<i32>> = vec![];
    /// assert_eq!(
    ///     a.par().copied().into_fallible_option().find(|x| *x > 2),
    ///     Some(None)
    /// );
    ///
    /// let a: Vec<Option<i32>> = vec![Some(1), Some(2), Some(3)];
    /// assert_eq!(
    ///     a.par().copied().into_fallible_option().find(|x| *x > 2),
    ///     Some(Some(3))
    /// );
    ///
    /// let a: Vec<Option<i32>> = vec![Some(1), None, Some(3)];
    /// let result = a.par().copied().into_fallible_option().find(|x| *x > 2);
    /// // depends on whichever is observed first in parallel execution
    /// assert!(result == Some(Some(3)) || result == None);
    /// ```
    fn find<Predicate>(self, predicate: Predicate) -> Option<Option<Self::Item>>
    where
        Self: Sized,
        Self::Item: Send,
        Predicate: Fn(&Self::Item) -> bool + Sync,
    {
        self.filter(&predicate).first()
    }
}

pub trait IntoOption<T> {
    fn into_option(self) -> Option<T>;

    fn into_result_with_unit_err(self) -> Result<T, ()>;
}

impl<T> IntoOption<T> for Option<T> {
    #[inline(always)]
    fn into_option(self) -> Option<T> {
        self
    }

    #[inline(always)]
    fn into_result_with_unit_err(self) -> Result<T, ()> {
        match self {
            Some(x) => Ok(x),
            None => Err(()),
        }
    }
}

pub(crate) trait ResultIntoOption<T> {
    fn into_option(self) -> Option<T>;
}

impl<T> ResultIntoOption<T> for Result<T, ()> {
    #[inline(always)]
    fn into_option(self) -> Option<T> {
        self.ok()
    }
}