diff --git a/stdlib/option.affine b/stdlib/option.affine index 042a9608..ed7a2405 100644 --- a/stdlib/option.affine +++ b/stdlib/option.affine @@ -6,7 +6,10 @@ module option; // `Option`/`Result` types + constructors are owned by `prelude` (ADR-011). -use prelude::{Option, Some, None, Result, Ok, Err, map}; +// `map` is defined locally below as the Option map (f, Option); +// it is NOT imported from prelude (prelude's `map` is the list map +// with a different signature — importing both would conflict). +use prelude::{Option, Some, None, Result, Ok, Err}; // This module is the single canonical home for the Option *operations* // (is_some/is_none/unwrap/unwrap_or/map/filter/contains/…). #133 removed @@ -184,14 +187,20 @@ fn unzip(opt: Option<(A, B)>) -> (Option, Option) { /// Transpose Option of list to list of Option fn transpose(opt: Option<[T]>) -> [Option] { match opt { - Some(list) => map(fn(x) => Some(x), list), + Some(list) => { + let mut result = []; + for x in list { + result = result ++ [Some(x)]; + } + result + }, None => [] } } /// Collect list of Options into Option of list (None on any None) fn collect(opts: [Option]) -> Option<[T]> { - let values = []; + let mut values = []; for opt in opts { match opt { Some(value) => values = values ++ [value], @@ -203,10 +212,13 @@ fn collect(opts: [Option]) -> Option<[T]> { /// Filter out Nones from list fn cat_options(opts: [Option]) -> [T] { - let values = []; + let mut values = []; for opt in opts { match opt { - Some(value) => values = values ++ [value], + // Statement-block arm so the arm is Unit-typed and matches the + // `None => {}` arm (a bare assignment expression is typed as its + // RHS, which would clash Array vs Unit across the arms). + Some(value) => { values = values ++ [value]; }, None => {} } } @@ -215,7 +227,10 @@ fn cat_options(opts: [Option]) -> [T] { /// Map list with function returning Option, filtering Nones fn map_filter(f: T -> Option, list: [T]) -> [U] { - let results = map(f, list); + let mut results = []; + for x in list { + results = results ++ [f(x)]; + } cat_options(results) } @@ -317,14 +332,14 @@ fn replace_none(opt: Option, replacement: Option) -> Option { } /// Take value from Option, leaving None -fn take(opt: &mut Option) -> Option { +fn take(mut opt: Option) -> Option { let result = opt; opt = None; result } /// Insert value into Option if None -fn get_or_insert(opt: &mut Option, value: T) -> T { +fn get_or_insert(mut opt: Option, value: T) -> T { match opt { Some(x) => x, None => {