Skip to content

Commit

Permalink
update itertools -- fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wbbradley committed Jul 31, 2020
1 parent b4a8bb3 commit 9e20311
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 192 deletions.
180 changes: 90 additions & 90 deletions lib/itertools.zion
Original file line number Diff line number Diff line change
@@ -1,121 +1,121 @@
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 {
let jx = Just(x)
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 () {
Expand Down
150 changes: 75 additions & 75 deletions lib/list.zion
Original file line number Diff line number Diff line change
@@ -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)
}
Loading

0 comments on commit 9e20311

Please sign in to comment.