Skip to content

Commit

Permalink
rename Always to Returns
Browse files Browse the repository at this point in the history
  • Loading branch information
jw3126 committed Feb 27, 2021
1 parent 115333b commit 2496bb0
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1810,7 +1810,7 @@ function hvcat(nbc::Integer, as...)
mod(n,nbc) != 0 &&
throw(ArgumentError("number of arrays $n is not a multiple of the requested number of block columns $nbc"))
nbr = div(n,nbc)
hvcat(ntuple(Always(nbc), nbr), as...)
hvcat(ntuple(Returns(nbc), nbr), as...)
end

"""
Expand Down
2 changes: 1 addition & 1 deletion base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ function iterate(d::ImmutableDict{K,V}, t=d) where {K, V}
!isdefined(t, :parent) && return nothing
(Pair{K,V}(t.key, t.value), t.parent)
end
length(t::ImmutableDict) = count(Always(true), t)
length(t::ImmutableDict) = count(Returns(true), t)
isempty(t::ImmutableDict) = !isdefined(t, :parent)
empty(::ImmutableDict, ::Type{K}, ::Type{V}) where {K, V} = ImmutableDict{K,V}()

Expand Down
2 changes: 1 addition & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export
AbstractUnitRange,
AbstractVector,
AbstractVecOrMat,
Always,
Array,
AbstractMatch,
AbstractPattern,
Expand Down Expand Up @@ -71,6 +70,7 @@ export
Rational,
Regex,
RegexMatch,
Returns,
RoundFromZero,
RoundDown,
RoundingMode,
Expand Down
14 changes: 7 additions & 7 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -917,14 +917,14 @@ julia> [1:5;] |> x->x.^2 |> sum |> inv

# always
"""
f = Always(value)
f = Returns(value)
Create a callable `f` such that `f(args...; kw...) === value` holds.
# Examples
```jldoctest
julia> f = Always(42);
julia> f = Returns(42);
julia> f(1)
42
Expand All @@ -934,15 +934,15 @@ julia> f("hello", x=32)
```
!!! compat "Julia 1.7"
Always requires at least Julia 1.7.
Returns requires at least Julia 1.7.
"""
struct Always{V} <: Function
struct Returns{V} <: Function
value::V
end

(obj::Always)(args...; kw...) = obj.value
function show(io::IO, obj::Always)
show(io, Always)
(obj::Returns)(args...; kw...) = obj.value
function show(io::IO, obj::Returns)
show(io, Returns)
print(io, "(")
show(io, obj.value)
print(io, ")")
Expand Down
18 changes: 9 additions & 9 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1153,17 +1153,17 @@ end
# issue #5177

c = fill(1,2,3,4)
m1 = mapslices(Always(fill(1,2,3)), c, dims=[1,2])
m2 = mapslices(Always(fill(1,2,4)), c, dims=[1,3])
m3 = mapslices(Always(fill(1,3,4)), c, dims=[2,3])
m1 = mapslices(Returns(fill(1,2,3)), c, dims=[1,2])
m2 = mapslices(Returns(fill(1,2,4)), c, dims=[1,3])
m3 = mapslices(Returns(fill(1,3,4)), c, dims=[2,3])
@test size(m1) == size(m2) == size(m3) == size(c)

n1 = mapslices(Always(fill(1,6) ), c, dims=[1,2])
n2 = mapslices(Always(fill(1,6) ), c, dims=[1,3])
n3 = mapslices(Always(fill(1,6) ), c, dims=[2,3])
n1a = mapslices(Always(fill(1,1,6)), c, dims=[1,2])
n2a = mapslices(Always(fill(1,1,6)), c, dims=[1,3])
n3a = mapslices(Always(fill(1,1,6)), c, dims=[2,3])
n1 = mapslices(Returns(fill(1,6) ), c, dims=[1,2])
n2 = mapslices(Returns(fill(1,6) ), c, dims=[1,3])
n3 = mapslices(Returns(fill(1,6) ), c, dims=[2,3])
n1a = mapslices(Returns(fill(1,1,6)), c, dims=[1,2])
n2a = mapslices(Returns(fill(1,1,6)), c, dims=[1,3])
n3a = mapslices(Returns(fill(1,1,6)), c, dims=[2,3])
@test size(n1a) == (1,6,4) && size(n2a) == (1,3,6) && size(n3a) == (2,1,6)
@test size(n1) == (6,1,4) && size(n2) == (6,3,1) && size(n3) == (2,6,1)

Expand Down
6 changes: 3 additions & 3 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ end
@test collect(takewhile(<(4),1:10)) == [1,2,3]
@test collect(takewhile(<(4),Iterators.countfrom(1))) == [1,2,3]
@test collect(takewhile(<(4),5:10)) == []
@test collect(takewhile(Always(true),5:10)) == 5:10
@test collect(takewhile(Returns(true),5:10)) == 5:10
@test collect(takewhile(isodd,[1,1,2,3])) == [1,1]
@test collect(takewhile(<(2), takewhile(<(3), [1,1,2,3]))) == [1,1]
end
Expand All @@ -209,8 +209,8 @@ end
@test collect(dropwhile(<(4), 1:10)) == 4:10
@test collect(dropwhile(<(4), 1:10)) isa Vector{Int}
@test isempty(dropwhile(<(4), []))
@test collect(dropwhile(Always(false),1:3)) == 1:3
@test isempty(dropwhile(Always(true), 1:3))
@test collect(dropwhile(Returns(false),1:3)) == 1:3
@test isempty(dropwhile(Returns(true), 1:3))
@test collect(dropwhile(isodd,[1,1,2,3])) == [2,3]
@test collect(dropwhile(iseven,dropwhile(isodd,[1,1,2,3]))) == [3]
end
Expand Down
14 changes: 7 additions & 7 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@ a = rand(3, 3)

@test [Base.afoldl(+, 1:i...) for i = 1:40] == [i * (i + 1) ÷ 2 for i = 1:40]

@testset "Always" begin
@test @inferred(Always(1)() ) === 1
@test @inferred(Always(1)(23) ) === 1
@test @inferred(Always("a")(2,3)) == "a"
@test @inferred(Always(1)(x=1, y=2)) === 1
@testset "Returns" begin
@test @inferred(Returns(1)() ) === 1
@test @inferred(Returns(1)(23) ) === 1
@test @inferred(Returns("a")(2,3)) == "a"
@test @inferred(Returns(1)(x=1, y=2)) === 1
val = [1,2,3]
@test Always(val)(1) === val
@test sprint(show, Always(1)) == "Always(1)"
@test Returns(val)(1) === val
@test sprint(show, Returns(1)) == "Returns(1)"
end

0 comments on commit 2496bb0

Please sign in to comment.