36 lines
857 B
Plaintext
36 lines
857 B
Plaintext
ns std::core
|
|
|
|
pub int Array<T> : Monad + Default + Empty {
|
|
const default = array::empty<Self>
|
|
const empty = array::empty<Self>
|
|
|
|
length: Int
|
|
}
|
|
|
|
pub mod array {
|
|
|
|
// Usage:
|
|
// let int_array = array::of(1, 2, 3)
|
|
// assert_eq(3, int_array.length)
|
|
pub extern fn of<T>(ts: ...T): Array<T>
|
|
|
|
pub extern fn of_length<T>(length: Int, init_value: T): Array<T>
|
|
|
|
pub extern fn empty<T>(): Array<T>
|
|
|
|
pub fn flatten<T>(ats: Array<Array<T>>): Array<T> {
|
|
let total_ts = ats.map { ts -> ts.length }
|
|
.reduce { acc, current -> acc + current }
|
|
let result: mut Array<T> = of_length(total_ts, std::reflect::default_or_empty<T>)
|
|
let i: mut Int = 0
|
|
for a in ats {
|
|
for element in a {
|
|
result[i] = element
|
|
i++
|
|
}
|
|
}
|
|
result
|
|
}
|
|
|
|
}
|