| 1 | fn test_sort_with_lambda_expr() { |
| 2 | a := [5, 2, 1, 9, 8] |
| 3 | dump(a) |
| 4 | |
| 5 | sorted01 := a.sorted(a < b) |
| 6 | sorted02 := a.sorted(a > b) |
| 7 | dump(sorted01) |
| 8 | dump(sorted02) |
| 9 | |
| 10 | sorted01_with_compare_fn := a.sorted_with_compare(fn (a &int, b &int) int { |
| 11 | return *a - *b |
| 12 | }) |
| 13 | sorted02_with_compare_fn := a.sorted_with_compare(fn (a &int, b &int) int { |
| 14 | return *b - *a |
| 15 | }) |
| 16 | dump(sorted01_with_compare_fn) |
| 17 | dump(sorted02_with_compare_fn) |
| 18 | |
| 19 | /////////////////////////////////////////// |
| 20 | |
| 21 | sorted01_lambda_expr := a.sorted(|ix, iy| ix < iy) |
| 22 | sorted02_lambda_expr := a.sorted(|ii, jj| ii > jj) |
| 23 | dump(sorted01_lambda_expr) |
| 24 | dump(sorted02_lambda_expr) |
| 25 | |
| 26 | sorted01_with_compare_lambda_expr := a.sorted_with_compare(|x, y| *x - *y) |
| 27 | sorted02_with_compare_lambda_expr := a.sorted_with_compare(|e1, e2| *e2 - *e1) |
| 28 | dump(sorted01_with_compare_lambda_expr) |
| 29 | dump(sorted02_with_compare_lambda_expr) |
| 30 | |
| 31 | assert sorted01 == sorted01_with_compare_fn |
| 32 | assert sorted02 == sorted02_with_compare_fn |
| 33 | assert sorted01 == sorted01_lambda_expr |
| 34 | assert sorted02 == sorted02_lambda_expr |
| 35 | assert sorted01 == sorted01_with_compare_lambda_expr |
| 36 | assert sorted02 == sorted02_with_compare_lambda_expr |
| 37 | } |
| 38 | |