Skip to content

Commit

Permalink
Add range with keyword arguments (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan authored Mar 5, 2018
1 parent 98db2e5 commit ffdfa1e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ Currently, the `@compat` macro supports the following syntaxes:

* `Compat.IOBuffer` supporting keyword arguments ([#25873]).

* `Compat.range` supporting keyword arguments ([#25896]).

## Renaming

Expand Down Expand Up @@ -370,6 +371,8 @@ Currently, the `@compat` macro supports the following syntaxes:
* `DevNull`, `STDIN`, `STDOUT` and `STDERR` are now `devnull`, `stdin`, `stdout` and
`stderr` respectively ([#25959]).

* `LinSpace` is now `LinRange` ([#25896]).

## New macros

* `@__DIR__` has been added ([#18380])
Expand Down Expand Up @@ -574,5 +577,6 @@ includes this fix. Find the minimum version from there.
[#25780]: https://github.com/JuliaLang/julia/issues/25780
[#25819]: https://github.com/JuliaLang/julia/issues/25819
[#25873]: https://github.com/JuliaLang/julia/issues/25873
[#25896]: https://github.com/JuliaLang/julia/issues/25896
[#25990]: https://github.com/JuliaLang/julia/issues/25990
[#26089]: https://github.com/JuliaLang/julia/issues/26089
29 changes: 29 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,35 @@ if VERSION < v"0.7.0-DEV.3734"
end
end

@static if VERSION < v"0.7.0-DEV.3986"
const LinRange = Base.LinSpace
export LinRange

function range(start; step=nothing, stop=nothing, length=nothing)
have_step = step !== nothing
have_stop = stop !== nothing
have_length = length !== nothing

if !(have_stop || have_length)
throw(ArgumentError("At least one of `length` or `stop` must be specified"))
elseif have_step && have_stop && have_length
throw(ArgumentError("Too many arguments specified; try passing only one of `stop` or `length`"))
elseif start === nothing
throw(ArgumentError("Can't start a range at `nothing`"))
end

if have_stop && !have_length
return have_step ? (start:step:stop) : (start:stop)
elseif have_length && !have_stop
return have_step ? Base.range(start, step, length) : Base.range(start, length)
elseif !have_step
return linspace(start, stop, length)
end
end
else
import Base: range, LinRange
end

include("deprecated.jl")

end # module Compat
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1450,4 +1450,14 @@ let buf = Compat.IOBuffer(sizehint=20)
@test String(take!(buf)) == "Hello world.\n"
end

# 0.7.0-DEV.3986
@test_throws ArgumentError Compat.range(1)
@test_throws ArgumentError Compat.range(nothing)
@test_throws ArgumentError Compat.range(1, step=1)
@test_throws ArgumentError Compat.range(1, step=1, stop=4, length=3)
@test Compat.range(2, step=2, stop=8) == 2:2:8
@test Compat.range(2, stop=8) == 2:8
@test Compat.range(2, step=2, length=8) == 2:2:16
@test Compat.range(1.0, stop=2.0, length=3) == 1.0:0.5:2.0

nothing

0 comments on commit ffdfa1e

Please sign in to comment.