diff --git a/stdlib/collections.affine b/stdlib/collections.affine index 10062528..a20ec927 100644 --- a/stdlib/collections.affine +++ b/stdlib/collections.affine @@ -3,6 +3,12 @@ // // Collections - Advanced list, array, and data structure operations +module collections; + +// `Option` + constructors and the generic list utilities are owned by +// `prelude` (ADR-011); collections is a consumer module (#135 slice 8). +use prelude::{Option, Some, None, filter, map, range, any}; + // ============================================================================ // List Operations // ============================================================================ diff --git a/stdlib/option.affine b/stdlib/option.affine index 001827a5..042a9608 100644 --- a/stdlib/option.affine +++ b/stdlib/option.affine @@ -6,7 +6,7 @@ module option; // `Option`/`Result` types + constructors are owned by `prelude` (ADR-011). -use prelude::{Option, Some, None, Result, Ok, Err}; +use prelude::{Option, Some, None, Result, Ok, Err, map}; // This module is the single canonical home for the Option *operations* // (is_some/is_none/unwrap/unwrap_or/map/filter/contains/…). #133 removed diff --git a/stdlib/prelude.affine b/stdlib/prelude.affine index e3705a76..5962c625 100644 --- a/stdlib/prelude.affine +++ b/stdlib/prelude.affine @@ -16,15 +16,15 @@ module prelude; // the single canonical bindings for those. // ============================================================================ -type Option = Some(T) | None +pub type Option = Some(T) | None -type Result = Ok(T) | Err(E) +pub type Result = Ok(T) | Err(E) // ============================================================================ // List utilities // ============================================================================ -fn map(arr: [T], f: T -> U) -> [U] { +pub fn map(arr: [T], f: T -> U) -> [U] { let mut result = []; for x in arr { result = result ++ [f(x)]; @@ -32,7 +32,7 @@ fn map(arr: [T], f: T -> U) -> [U] { result } -fn filter(arr: [T], predicate: T -> Bool) -> [T] { +pub fn filter(arr: [T], predicate: T -> Bool) -> [T] { let mut result = []; for x in arr { if predicate(x) { @@ -42,7 +42,7 @@ fn filter(arr: [T], predicate: T -> Bool) -> [T] { result } -fn fold(arr: [T], init: U, f: (U, T) -> U) -> U { +pub fn fold(arr: [T], init: U, f: (U, T) -> U) -> U { let mut acc = init; for x in arr { acc = f(acc, x); @@ -51,7 +51,7 @@ fn fold(arr: [T], init: U, f: (U, T) -> U) -> U { } /// Conforms to aLib collection/contains spec v1.0 -fn contains(arr: [T], element: T) -> Bool { +pub fn contains(arr: [T], element: T) -> Bool { for x in arr { if x == element { return true; @@ -60,11 +60,11 @@ fn contains(arr: [T], element: T) -> Bool { false } -fn sum(arr: [Int]) -> Int { +pub fn sum(arr: [Int]) -> Int { fold(arr, 0, |acc, x| acc + x) } -fn product(arr: [Int]) -> Int { +pub fn product(arr: [Int]) -> Int { fold(arr, 1, |acc, x| acc * x) } @@ -72,15 +72,15 @@ fn product(arr: [Int]) -> Int { // Comparison and ordering // ============================================================================ -fn min(a: Int, b: Int) -> Int { +pub fn min(a: Int, b: Int) -> Int { if a < b { a } else { b } } -fn max(a: Int, b: Int) -> Int { +pub fn max(a: Int, b: Int) -> Int { if a > b { a } else { b } } -fn clamp(value: Int, min_val: Int, max_val: Int) -> Int { +pub fn clamp(value: Int, min_val: Int, max_val: Int) -> Int { if value < min_val { min_val } else if value > max_val { @@ -94,11 +94,11 @@ fn clamp(value: Int, min_val: Int, max_val: Int) -> Int { // Boolean utilities // ============================================================================ -fn not(b: Bool) -> Bool { +pub fn not(b: Bool) -> Bool { if b { false } else { true } } -fn all(arr: [Bool]) -> Bool { +pub fn all(arr: [Bool]) -> Bool { for b in arr { if not(b) { return false; @@ -107,7 +107,7 @@ fn all(arr: [Bool]) -> Bool { true } -fn any(arr: [Bool]) -> Bool { +pub fn any(arr: [Bool]) -> Bool { for b in arr { if b { return true; @@ -120,7 +120,7 @@ fn any(arr: [Bool]) -> Bool { // Range and iteration utilities // ============================================================================ -fn range(start: Int, end: Int) -> [Int] { +pub fn range(start: Int, end: Int) -> [Int] { let mut result = []; let mut i = start; while i < end { @@ -130,7 +130,7 @@ fn range(start: Int, end: Int) -> [Int] { result } -fn repeat(value: T, n: Int) -> [T] { +pub fn repeat(value: T, n: Int) -> [T] { let mut result = []; let mut i = 0; while i < n { diff --git a/stdlib/result.affine b/stdlib/result.affine index 17078644..0945ed04 100644 --- a/stdlib/result.affine +++ b/stdlib/result.affine @@ -6,7 +6,7 @@ module result; // `Option`/`Result` types + constructors are owned by `prelude` (ADR-011). -use prelude::{Result, Ok, Err, Option, Some, None}; +use prelude::{Result, Ok, Err, Option, Some, None, map}; // This module is the single canonical home for the Result *operations* // (is_ok/is_err/unwrap/unwrap_or/map_ok/map_err/…). #133 removed the @@ -187,7 +187,7 @@ fn partition_results(results: [Result]) -> ([T], [E]) { /// Try to apply function to all elements, collecting errors fn try_map(f: T -> Result, list: [T]) -> Result<[U], E> { - let results = map(f, list); + let results = map(list, f); collect(results) }