From 79adbcfde0ff87be61bfb5a03ffc0a54156adea9 Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Mon, 5 Mar 2018 09:23:08 -0800 Subject: [PATCH 1/2] Add range with keyword arguments --- README.md | 4 ++++ src/Compat.jl | 29 +++++++++++++++++++++++++++++ test/runtests.jl | 10 ++++++++++ 3 files changed, 43 insertions(+) diff --git a/README.md b/README.md index 3c114cdfa..e564e075e 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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]) @@ -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 diff --git a/src/Compat.jl b/src/Compat.jl index b3cdb1498..f169dfb9c 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index c88320bc4..f69444726 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 From 43dff9f452f05c855cca0c2a8205d731a6549e71 Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Mon, 5 Mar 2018 11:44:58 -0800 Subject: [PATCH 2/2] Corrections --- README.md | 2 +- src/Compat.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e564e075e..fe6c90108 100644 --- a/README.md +++ b/README.md @@ -278,7 +278,7 @@ Currently, the `@compat` macro supports the following syntaxes: * `Compat.IOBuffer` supporting keyword arguments ([#25873]). -* `Compat.range` supporting keyword arguments ([25896]). +* `Compat.range` supporting keyword arguments ([#25896]). ## Renaming diff --git a/src/Compat.jl b/src/Compat.jl index f169dfb9c..970283cba 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -1638,7 +1638,7 @@ end end if have_stop && !have_length - return have_step ? start:step:stop : start:stop + 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