diff --git a/lib/itertools.zion b/lib/itertools.zion index 7fd39801..7de6c5df 100644 --- a/lib/itertools.zion +++ b/lib/itertools.zion @@ -1,14 +1,14 @@ fn zip(xs, ys) { - let iter_xs = iter(xs) - let iter_ys = iter(ys) - return fn () { - if iter_xs() is Just(x) { - if iter_ys() is Just(y) { - return Just((x, y)) - } - } - return Nothing + let iter_xs = iter(xs) + let iter_ys = iter(ys) + return fn () { + if iter_xs() is Just(x) { + if iter_ys() is Just(y) { + return Just((x, y)) + } } + return Nothing + } } fn repeat(x) fn () Maybe a { @@ -16,106 +16,106 @@ fn repeat(x) fn () Maybe a { return fn () => jx } -fn take(n, iterable) { - let iterator = iter(iterable) - var i = n - return fn() { - if i <= 0 { - return Nothing - } - let val = iterator() - match val { - Just(x) { - i = i - 1 - return val - } - Nothing { - return Nothing - } - } +fn take(iterable, n) { + let iterator = iter(iterable) + var i = n + return fn() { + if i <= 0 { + return Nothing } + let val = iterator() + match val { + Just(x) { + i = i - 1 + return val + } + Nothing { + return Nothing + } + } + } } -fn takewhile(predicate, iterable) { - let iterator = iter(iterable) - return fn() { - let val = iterator() - match val { - Just(x) { - if predicate(x) { - return val - } else { - return Nothing - } - } - Nothing { - return Nothing - } +fn takewhile(iterable, predicate) { + let iterator = iter(iterable) + return fn() { + let val = iterator() + match val { + Just(x) { + if predicate(x) { + return val + } else { + return Nothing } + } + Nothing { + return Nothing + } } + } } -fn dropwhile(predicate, iterable) { - let iterator = iter(iterable) - var dropping = True - return fn () { - while dropping { - match iterator() { - val@Just(x) { - if predicate(x) { - continue - } else { - dropping = False - return val - } - } - Nothing { - return Nothing - } - } +fn dropwhile(iterable, predicate) { + let iterator = iter(iterable) + var dropping = True + return fn () { + while dropping { + match iterator() { + val@Just(x) { + if predicate(x) { + continue + } else { + dropping = False + return val + } + } + Nothing { + return Nothing } - return iterator() + } } + return iterator() + } } fn cycle(xs_input) { - var xs = Just(iter(xs_input)) - let recording = [] - var index = 0 - return fn () { - match! xs { - Just(iterator) { - match iterator() { - step@Just(x) { - append(recording, step) - return step - } - Nothing { - xs = Nothing - } - } - } - } - let num_recordings = len(recording) - if index < num_recordings { - let ret = recording[index] - index = index + 1 - return ret - } else if num_recordings == 0 { - return Nothing - } else { - assert(index == num_recordings) - index = 1 - return recording[0] + var xs = Just(iter(xs_input)) + let recording = [] + var index = 0 + return fn () { + match! xs { + Just(iterator) { + match iterator() { + step@Just(x) { + append(recording, step) + return step + } + Nothing { + xs = Nothing + } } + } + } + let num_recordings = len(recording) + if index < num_recordings { + let ret = recording[index] + index = index + 1 + return ret + } else if num_recordings == 0 { + return Nothing + } else { + assert(index == num_recordings) + index = 1 + return recording[0] } + } } -fn chain2(a, b) fn () Maybe a => chain2_iters(iter(a), iter(b)) +fn chain2(a, b) fn () Maybe a => _chain2_iters(iter(a), iter(b)) -fn chain2_iters(as fn () Maybe a, bs fn () Maybe a) fn () Maybe a { +fn _chain2_iters(as fn () Maybe a, bs fn () Maybe a) fn () Maybe a { var outer_iter = iter([as, bs]) var cur_iterable = outer_iter() return fn () { diff --git a/lib/list.zion b/lib/list.zion index cca21d9e..cac08b20 100644 --- a/lib/list.zion +++ b/lib/list.zion @@ -1,101 +1,101 @@ data List x { - Cons(x, var (List x)) - Nil + Cons(x, var (List x)) + Nil } instance HasLength (List a) { - fn len(list) { - var p = list - var i = 0 - while match p { - Cons(_, var next) { - i += 1 - p = next - } - Nil { - break - } - } - return i + fn len(list) { + var p = list + var i = 0 + while match p { + Cons(_, var next) { + i += 1 + p = next + } + Nil { + break + } } + return i + } } fn nth(l List x, index Int) Maybe x { - var rl = l - var i = 0 - while match rl { - Cons(x, var next) { - if i == index { - return Just(x) - } else { - rl = next - i += 1 - } - } - Nil { - break - } + var rl = l + var i = 0 + while match rl { + Cons(x, var next) { + if i == index { + return Just(x) + } else { + rl = next + i += 1 + } } - return Nothing + Nil { + break + } + } + return Nothing } instance Iterable (List a) a { - fn iter(list List a) { - var list = list - return fn () Maybe a { - match list { - Cons(x, var next) { - list = next - return Just(x) - } - Nil { - return Nothing - } - } + fn iter(list List a) { + var list = list + return fn () Maybe a { + match list { + Cons(x, var next) { + list = next + return Just(x) + } + Nil { + return Nothing } + } } + } } instance Str (List a) { - fn str(list) { - return join(", ", list) - } + fn str(list) { + return join(", ", list) + } } # Removes items where pred(item) returns True. # Returns the new list, as well as the number of items removed. fn remove_if(list List a, pred fn (a) Bool) (List a, Int) { - var removed = 0 - var starting_list = Nil - var cur_node = list - var prev_node = Nil - while match cur_node { - Cons(a, var next) { - if pred(a) { - removed += 1 - match prev_node { - Cons(_, var prev_next) { - # Remove this node from the list - prev_next = next - } - Nil {} - } - cur_node = next - continue - } else { - match! starting_list { - Nil { - starting_list = cur_node - } - } - prev_node = cur_node - cur_node = next - continue - } + var removed = 0 + var starting_list = Nil + var cur_node = list + var prev_node = Nil + while match cur_node { + Cons(a, var next) { + if pred(a) { + removed += 1 + match prev_node { + Cons(_, var prev_next) { + # Remove this node from the list + prev_next = next + } + Nil {} } - Nil { - return (starting_list, removed) + cur_node = next + continue + } else { + match! starting_list { + Nil { + starting_list = cur_node + } } + prev_node = cur_node + cur_node = next + continue + } + } + Nil { + return (starting_list, removed) } - return (Nil, 0) + } + return (Nil, 0) } diff --git a/lib/map.zion b/lib/map.zion index 7a312496..bd90fbb2 100644 --- a/lib/map.zion +++ b/lib/map.zion @@ -11,11 +11,11 @@ fn map_render(map, tuple_show) { let Map(MapStorage(storage), _) = map let results = [] for match Cons(MapKeyValueCell(_, key, var value), var next) in storage { - append(results, tuple_show(key, value)) + results.append(tuple_show(key, value)) var next_list = next while match next_list { Cons(MapKeyValueCell(_, key, var value), var next_next) { - append(results, tuple_show(key, value)) + results.append(tuple_show(key, value)) next_list = next_next } Nil { @@ -23,7 +23,7 @@ fn map_render(map, tuple_show) { } } } - return "{${join(", ", results)}}" + return "{${", ".join(results)}}" } instance Str (Map key value) { @@ -94,7 +94,7 @@ instance HasRemovableItems (Map key value) key { if size == 0 { return } - let hash_key = abs(hash(key)) + let hash_key = key.hash.abs let index = hash_key % len(storage) let (new_list, removed_count) = remove_if(storage[index], |key_value_cell| { let MapKeyValueCell(existing_key_hash, existing_key, _) = key_value_cell diff --git a/src/parser.cpp b/src/parser.cpp index 09c663ec..1527d85b 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1977,6 +1977,7 @@ std::pair parse_lambda_param_core(ParseState &ps) { throw user_error(first_token.location, "parameter names cannot begin with capital letters"); } + ps.mutable_vars.erase(first_token.text); ps.locals.insert(first_token.text); return {iid(first_token), (token_begins_type(ps.token)) diff --git a/tests/test_ferber.zion b/tests/test_ferber.zion index 1774da50..3df901bf 100644 --- a/tests/test_ferber.zion +++ b/tests/test_ferber.zion @@ -10,8 +10,8 @@ fn wait(minutes Int) { } fn main() { - let wait_times = chain2([7, 12], cycle([15])) - for i in take(15, wait_times) { - wait(i) + let wait_times = chain2([7, 12], [15].cycle) + for i in wait_times.take(15) { + i.wait } } diff --git a/tests/test_itertools_dropwhile.zion b/tests/test_itertools_dropwhile.zion index 58d09da2..abae80f1 100644 --- a/tests/test_itertools_dropwhile.zion +++ b/tests/test_itertools_dropwhile.zion @@ -5,5 +5,5 @@ import itertools {dropwhile} import math {sum} fn main() { - print(sum(dropwhile(|x| => x < 6, [0..10]))) + print([0..10].dropwhile(|x| => x < 6).sum) } diff --git a/tests/test_itertools_take.zion b/tests/test_itertools_take.zion index c7e1d5dc..b1fd0953 100644 --- a/tests/test_itertools_take.zion +++ b/tests/test_itertools_take.zion @@ -5,5 +5,5 @@ import itertools {take} import math {sum} fn main() { - print(sum(take(5, [0..]))) + print([0..].take(5).sum) } diff --git a/tests/test_itertools_takewhile.zion b/tests/test_itertools_takewhile.zion index e550f35d..04637ab9 100644 --- a/tests/test_itertools_takewhile.zion +++ b/tests/test_itertools_takewhile.zion @@ -5,5 +5,5 @@ import itertools {takewhile} import math {sum} fn main() { - print(sum(takewhile(|x| => x < 6, [0..]))) + print([0..].takewhile(|x| => x < 6).sum) } diff --git a/tests/test_readline.zion b/tests/test_readline.zion index 069c8603..0822ec1d 100644 --- a/tests/test_readline.zion +++ b/tests/test_readline.zion @@ -4,10 +4,10 @@ import itertools {take} fn main() { - assert(chomp("\n") == "") - with! let fd = open(__filename__) { - for line in take(5, readlines(fd)) { - print(chomp(line)) - } + assert("\n".chomp == "") + with! let fd = __filename__.open { + for line in readlines(fd).take(5) { + print(line.chomp) } + } } diff --git a/tests/test_struct_member_vars.zion b/tests/test_struct_member_vars.zion index a8ccd1e3..d5840544 100644 --- a/tests/test_struct_member_vars.zion +++ b/tests/test_struct_member_vars.zion @@ -1,22 +1,22 @@ # test: pass struct MutableVector { - mx var Float - my var Float + mx var Float + my var Float } instance Str MutableVector { - fn str(mv) { - return "<${mv.mx}, ${mv.my}>" - } + fn str(mv) { + return "<${mv.mx}, ${mv.my}>" + } } fn main() { - var x = 1.2 - var y = 2.3 - let mv = MutableVector(var x, var y) - mv.mx = 2.09 - print(mv.mx) - print(mv) - print("PASS") + var x = 1.2 + var y = 2.3 + let mv = MutableVector(var x, var y) + mv.mx = 2.09 + print(mv.mx) + print(mv) + print("PASS") }