Skip to content

Commit b4c2027

Browse files
committed
Test: implement reverse
(cherry picked from commit c4b47cb)
1 parent 9e94cd6 commit b4c2027

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

test/Inputs/Swiftskell.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public protocol Generator: ~Copyable {
5757

5858
// MARK: Tuples
5959
public enum Pair<L: ~Copyable, R: ~Copyable>: ~Copyable {
60-
case elms(L, R)
60+
case pair(L, R)
6161
}
6262
extension Pair: Copyable where L: Copyable, R: Copyable {}
6363

@@ -244,14 +244,24 @@ extension List where Element: ~Copyable {
244244
public consuming func pop() -> Optional<Pair<Element, List<Element>>> {
245245
switch consume self {
246246
case .empty: .none
247-
case let .cons(elm, tail): .elms(elm, tail.take())
247+
case let .cons(elm, tail): .pair(elm, tail.take())
248248
}
249249
}
250250

251251
/// Push an element onto the list.
252252
public consuming func push(_ newHead: consuming Element) -> List<Element> {
253253
return List(newHead, self)
254254
}
255+
256+
/// Produces a new list that is the reverse of this list.
257+
public consuming func reverse() -> List<Element> {
258+
var new = List<Element>()
259+
while case let .pair(head, tail) = pop() {
260+
new = new.push(head)
261+
self = tail
262+
}
263+
return new
264+
}
255265
}
256266

257267
extension List: Show where Element: Show & ~Copyable {

test/Interpreter/moveonly_swiftskell.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@ func testListBasic() {
5353
check(items.length() == 5)
5454
check(!items.empty())
5555

56+
items = items.reverse()
57+
check(items.length() == 5)
58+
print(items.show()) // CHECK: [4, 3, 2, 1, 0, ]
59+
5660
items = .empty
5761
check(items.length() == 0)
5862
check(items.empty())
5963

6064
let nums = List<Int>().push(7).push(7).push(3)
6165
print(nums.show()) // CHECK: [7, 7, 3, ]
6266

63-
67+
6468
}

0 commit comments

Comments
 (0)