foo(x::Integer) = "Integer!"
foo (generic function with 1 method)
foo(5)
"Integer!"
foo(::Val{true}) = "TRUE"
foo(::Val{false}) = "FALSE"
foo (generic function with 3 methods)
foo(Val(true))
"TRUE"
foo(Val(false))
"FALSE"
foo(x::Bool) = foo(Val(x))
foo (generic function with 4 methods)
foo(true)
"TRUE"
foo(false)
"FALSE"
Val(true)
Val{true}()
Val(5)
Val{5}()
Val(UInt8(5))
Val{0x05}()
Val(5.0)
Val{5.0}()
Val(1+2im)
Val{1 + 2im}()
Val('A')
Val{'A'}()
Val("ABC")
TypeError: in Type, in parameter, expected Type, got String Stacktrace: [1] Val(::String) at ./essentials.jl:728 [2] top-level scope at In[15]:1
Val([1,2,3])
TypeError: in Type, in parameter, expected Type, got Array{Int64,1} Stacktrace: [1] Val(::Array{Int64,1}) at ./essentials.jl:728 [2] top-level scope at In[16]:1
isone(::Val{1}) = true
isone(::Val{N}) where {N} = false
isone(x::Int) = isone(Val(x))
isone (generic function with 3 methods)
isone(1)
true
isone(2)
false
isone(2.0)
MethodError: no method matching isone(::Float64) You may have intended to import Base.isone Closest candidates are: isone(!Matched::Int64) at In[17]:3 isone(!Matched::Val{1}) at In[17]:1 isone(!Matched::Val{N}) where N at In[17]:2 Stacktrace: [1] top-level scope at In[20]:1
abstract type LinkedList{T} end
mutable struct NullNode{T} <: LinkedList{T}
end
mutable struct Node{T} <: LinkedList{T}
data::T
next::LinkedList{T}
end
null = NullNode{Int}()
NullNode{Int64}()
n1 = Node(1, null)
Node{Int64}(1, NullNode{Int64}())
n2 = Node(2, n1)
Node{Int64}(2, Node{Int64}(1, NullNode{Int64}()))
n3 = Node(3, n2)
Node{Int64}(3, Node{Int64}(2, Node{Int64}(1, NullNode{Int64}())))
istwo(::Val{2}) = true
istwo(::Val{N}) where {N} = false
istwo(x::Int) = istwo(Val(x))
istwo (generic function with 3 methods)
findtwo(::NullNode) = false
findtwo(x::Node) = istwo(x.data) ? true : findtwo(x.next)
findtwo (generic function with 2 methods)
findtwo(n3)
true
replacetwo(::NullNode, n) = nothing
function replacetwo(x::Node, n)
if istwo(x.data)
x.data = n
return n
else
return replacetwo(x.next, n)
end
end
replacetwo (generic function with 2 methods)
replacetwo(n3, 4)
4
n3
Node{Int64}(3, Node{Int64}(4, Node{Int64}(1, NullNode{Int64}())))
using Match
bar(x) = @match x begin
1 => "one"
2 => "two"
_ => "Something else..."
end
bar (generic function with 1 method)
bar(1)
"one"
bar(2)
"two"
bar(3)
"Something else..."
bar(x) = @match x begin
n::Int => "integer"
str::String => "string"
d::Dict => "dictionary"
end
bar (generic function with 1 method)
bar(5)
"integer"
bar("5")
"string"
bar(Dict(2 => "2"))
"dictionary"
findtwo2(x::LinkedList) = @match x begin
Node(2, _) => 2
Node(_, next) => findtwo2(next)
x::NullNode => 0
end
findtwo2 (generic function with 1 method)
null = NullNode{Int}()
n1 = Node(1, null)
n2 = Node(2, n1)
n3 = Node(3, n2)
Node{Int64}(3, Node{Int64}(2, Node{Int64}(1, NullNode{Int64}())))
findtwo2(n3)
2
length
¶import Base.length
length(x::LinkedList, n) = @match x begin
Node(_, next) => length(next, n+1)
x::NullNode => n
end
length(x::LinkedList) = length(x, 0)
length (generic function with 87 methods)
length(n3)
3
find
¶find(x::LinkedList{T}, n::T) where {T} = @match x begin
Node(d, _), if d == n end => true
Node(_, next) => find(next, n)
x::NullNode => false
end
find (generic function with 1 method)
find(n3, 2)
true
find(n3, 0)
false
print
¶Base.show(io::IO, x::LinkedList{T}) where {T} = @match x begin
Node(d, next) => (print(d, ", "); Base.show(io, next))
x::NullNode => print()
end
print(n3)
3, 2, 1,