File tree Expand file tree Collapse file tree 1 file changed +14
-3
lines changed Expand file tree Collapse file tree 1 file changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -364,8 +364,10 @@ let init n f =
364
364
365
365
let rec rev_append l1 l2 =
366
366
match l1 with
367
- [] -> l2
368
- | a :: l -> rev_append l (a :: l2)
367
+ | [] -> l2
368
+ | [a0] -> a0::l2 (* single element is common *)
369
+ | [a0 ; a1] -> a1 :: a0 :: l2
370
+ | a0 ::a1 ::a2 ::rest -> rev_append rest (a2::a1::a0::l2)
369
371
370
372
let rev l = rev_append l []
371
373
@@ -442,7 +444,16 @@ let rec rev_map_append l1 l2 f =
442
444
let rec flat_map_aux f acc append lx =
443
445
match lx with
444
446
| [] -> rev_append acc append
445
- | a0 ::rest -> flat_map_aux f (rev_append (f a0) acc ) append rest
447
+ | a0 ::rest ->
448
+ let new_acc =
449
+ match f a0 with
450
+ | [] -> acc
451
+ | [a0] -> a0::acc
452
+ | [a0;a1] -> a1::a0::acc
453
+ | a0 ::a1 ::a2 ::rest ->
454
+ rev_append rest (a2::a1::a0::acc)
455
+ in
456
+ flat_map_aux f new_acc append rest
446
457
447
458
let flat_map lx f =
448
459
flat_map_aux f [] [] lx
You can’t perform that action at this time.
0 commit comments