core:slice in Odin
max, contains, and an in-place sort from the slice utility package.
package main
import "core:fmt"
import "core:slice"
main :: proc() {
hauls := []f32{12.5, 8.0, 15.25, 6.75, 11.0}
fmt.println("best haul:", slice.max(hauls))
fmt.println("has 8.0?", slice.contains(hauls, 8.0))
// sort works in place on any ordered element type.
weights := [5]f32{12.5, 8.0, 15.25, 6.75, 11.0}
slice.sort(weights[:])
fmt.println("sorted:", weights)
fmt.println("lightest two:", weights[0:2])
}
How it works
slice.maxandslice.containsreduce and search without loops.slice.sort(weights[:])orders the array through a slice of it.- Slicing
[0:2]after the sort reads off the two lightest hauls.
Keywords and builtins used here
f32importmaxpackageproc
The run, in numbers
- Lines
- 17
- Characters to type
- 412
- Tokens
- 117
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 108 seconds.
Step 2 of 3 in The core library, step 23 of 27 in Language basics.