Skip to content

Commit

Permalink
deprecate nil and cons
Browse files Browse the repository at this point in the history
  • Loading branch information
oxinabox committed Aug 31, 2020
1 parent 9658145 commit 1fd1b1a
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 40 deletions.
4 changes: 2 additions & 2 deletions docs/src/linked_list.md
Expand Up @@ -4,8 +4,8 @@ A list of sequentially linked nodes. This allows efficient insertion of
nodes to the front of the list:

```julia
julia> l1 = nil()
nil()
julia> l1 = Nil()
Nil()

julia> l2 = cons(1, l1)
list(1)
Expand Down
3 changes: 1 addition & 2 deletions src/DataStructures.jl
Expand Up @@ -30,8 +30,7 @@ module DataStructures

export Trie, subtrie, keys_with_prefix, partial_path

export LinkedList, Nil, Cons, nil, cons, head, tail, list, filter, cat,
reverse
export LinkedList, Nil, Cons, head, tail, list
export MutableLinkedList
export SortedDict, SortedMultiDict, SortedSet
export SDToken, SDSemiToken, SMDToken, SMDSemiToken
Expand Down
4 changes: 4 additions & 0 deletions src/deprecations.jl
@@ -1,3 +1,7 @@
# 0.18 deprecations. Remove before releasing 0.19
@deprecate path(t::Trie, str::AbstractString) partial_path(t::Trie, str::AbstractString)
@deprecate find_root find_root!

@deprecate nil() Nil()
@deprecate nil(T) Nil{T}()
@deprecate cons Cons
42 changes: 19 additions & 23 deletions src/list.jl
Expand Up @@ -4,17 +4,13 @@ Base.eltype(::Type{<:LinkedList{T}}) where T = T

mutable struct Nil{T} <: LinkedList{T}
end
Nil() = Nil{Any}()

mutable struct Cons{T} <: LinkedList{T}
head::T
tail::LinkedList{T}
end

cons(h, t::LinkedList{T}) where {T} = Cons{T}(h, t)

nil(T) = Nil{T}()
nil() = nil(Any)

head(x::Cons) = x.head
tail(x::Cons) = x.tail

Expand All @@ -24,9 +20,9 @@ Base.:(==)(x::Cons, y::Cons) = (x.head == y.head) && (x.tail == y.tail)
function Base.show(io::IO, l::LinkedList{T}) where T
if isa(l,Nil)
if T === Any
print(io, "nil()")
print(io, "Nil()")
else
print(io, "nil(", T, ")")
print(io, "Nil{", T, "}()")
end
else
print(io, "list(")
Expand All @@ -39,20 +35,20 @@ function Base.show(io::IO, l::LinkedList{T}) where T
end
end

list() = nil()
list() = Nil()

function list(elts...)
l = nil()
for i=length(elts):-1:1
l = cons(elts[i],l)
l = Nil()
for i in length(elts):-1:1
l = Cons(elts[i],l)
end
return l
end

function list(elts::T...) where T
l = nil(T)
for i=length(elts):-1:1
l = cons(elts[i],l)
l = Nil{T}()
for i in length(elts):-1:1
l = Cons(elts[i],l)
end
return l
end
Expand All @@ -71,27 +67,27 @@ Base.map(f::Base.Callable, l::Nil) = l

function Base.map(f::Base.Callable, l::Cons{T}) where T
first = f(l.head)
l2 = cons(first, nil(typeof(first) <: T ? T : typeof(first)))
l2 = Cons(first, Nil{typeof(first) <: T ? T : typeof(first)}())
for h in l.tail
l2 = cons(f(h), l2)
l2 = Cons(f(h), l2)
end
reverse(l2)
end

function Base.filter(f::Function, l::LinkedList{T}) where T
l2 = nil(T)
l2 = Nil{T}()
for h in l
if f(h)
l2 = cons(h, l2)
l2 = Cons(h, l2)
end
end
reverse(l2)
end

function Base.reverse(l::LinkedList{T}) where T
l2 = nil(T)
l2 = Nil{T}()
for h in l
l2 = cons(h, l2)
l2 = Cons(h, l2)
end
return l2
end
Expand All @@ -112,14 +108,14 @@ function Base.cat(lst::LinkedList, lsts::LinkedList...)
T = typejoin(T, T2)
end

l2 = nil(T)
l2 = Nil{T}()
for h in lst
l2 = cons(h, l2)
l2 = Cons(h, l2)
end

for i = 1:n
for h in lsts[i]
l2 = cons(h, l2)
l2 = Cons(h, l2)
end
end

Expand Down
10 changes: 10 additions & 0 deletions test/test_deprecations.jl
Expand Up @@ -14,4 +14,14 @@
@test collect(path(t, "robb")) == [t0, t1, t2, t3]
@test collect(path(t, "ro")) == [t0, t1, t2]
@test collect(path(t, "roa")) == [t0, t1, t2]
end


@testset "List nil and cons" begin
@test nil() == Nil()
@test nil(Char) == Nil{Char}()

l1 = Nil()
l2 = cons(1, l1) # deprecated to Cons(1, l1)
@test length(l2) == 1
end
26 changes: 13 additions & 13 deletions test/test_list.jl
Expand Up @@ -8,30 +8,30 @@
end

@testset "l0" begin
l0 = nil(Char)
l0 = Nil{Char}()
@test length(l0) == 0
@test l0 == nil(Char)
@test l0 == nil()
@test sprint(show,l0) == "nil(Char)"
@test l0 == Nil{Char}()
@test l0 == Nil()
@test sprint(show,l0) == "Nil{Char}()"
end

@testset "l1" begin
l1 = nil()
l1 = Nil()
@test length(l1) == 0
@test l1 == nil()
@test l1 == nil(Int)
@test sprint(show,l1) == "nil()"
@test l1 == Nil()
@test l1 == Nil{Int}()
@test sprint(show,l1) == "Nil()"
@test typeof(list()) === typeof(l1)
@test copy(l1) == l1
@test map((x) -> x*2,l1) == l1
end

@testset "l2" begin
l1 = nil()
l2 = cons(1, l1)
l1 = Nil()
l2 = Cons(1, l1)
@test length(l2) == 1
@test head(l2) == 1
@test l2 == cons(1, l1)
@test l2 == Cons(1, l1)
@test l2 == list(1)
@test sprint(show,l2) == "list(1)"
@test cat(l2) == l2
Expand All @@ -50,8 +50,8 @@
end

@testset "l4" begin
l1 = nil()
l2 = cons(1, l1)
l1 = Nil()
l2 = Cons(1, l1)
l3 = list(2, 3)
l4 = cat(l1, l2, l3)
@test length(l4) == 3
Expand Down

0 comments on commit 1fd1b1a

Please sign in to comment.