Commit f93ef19f

f93ef19f8f26Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 2 files · +1 added · ~1 modified

Description

Add Array#sort, #min, #max, #sum, #reduce/#inject

  • sort → sp_IntArray_sort (qsort-based copy)
  • sort! → sp_IntArray_sort_bang (in-place qsort)
  • min/max → inline loop
  • sum → inline accumulator loop
  • reduce(init) { |acc, x| expr } → inline fold with temp var Block params registered in var table for proper type inference.

Test: examples/bm_sort_reduce.rb — all patterns match Ruby.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
f93ef19f8f26
Parent
32ab2ae8eabe

examples/bm_sort_reduce.rbaddedView file

1+# Test Array#sort, reduce, min, max, sum
2+
3+arr = (1..10).to_a
4+arr.reverse!
5+
6+# sort
7+sorted = arr.sort
8+puts sorted.first # 1
9+puts sorted.last # 10
10+
11+# min/max/sum
12+puts arr.min # 1
13+puts arr.max # 10
14+puts arr.sum # 55
15+
16+# reduce
17+puts arr.reduce(0) { |sum, x| sum + x } # 55
18+puts arr.reduce(1) { |prod, x| prod * x } # 3628800
19+
20+# inject (alias)
21+puts arr.inject(0) { |s, x| s + x } # 55