Skip to content

Commit

Permalink
Remove hacks to support old versions
Browse files Browse the repository at this point in the history
  • Loading branch information
sabiwara committed Aug 28, 2024
1 parent e403f0e commit 06994ef
Show file tree
Hide file tree
Showing 5 changed files with 611 additions and 642 deletions.
70 changes: 30 additions & 40 deletions lib/aja.ex
Original file line number Diff line number Diff line change
Expand Up @@ -209,26 +209,21 @@ defmodule Aja do
end
end

# TODO remove when dropping support for Elixir < 1.12
stepped_range_available? = Version.compare(System.version(), "1.12.0-rc.0") != :lt

if stepped_range_available? do
defmacro vec({:"..//", _, [first, last, step]} = call) do
case Enum.map([first, last, step], &Macro.expand(&1, __CALLER__)) do
[first, last, step] when is_integer(first) and is_integer(last) and is_integer(step) ->
Range.new(first, last, step)
|> Enum.to_list()
|> ast_from_list(__CALLER__)

_ ->
raise ArgumentError, ~s"""
Incorrect use of `Aja.vec/1`:
vec(#{Macro.to_string(call)}).
The `vec(a..b//c)` syntax can only be used with constants:
vec(1..100//5)
"""
end
defmacro vec({:"..//", _, [first, last, step]} = call) do
case Enum.map([first, last, step], &Macro.expand(&1, __CALLER__)) do
[first, last, step] when is_integer(first) and is_integer(last) and is_integer(step) ->
Range.new(first, last, step)
|> Enum.to_list()
|> ast_from_list(__CALLER__)

_ ->
raise ArgumentError, ~s"""
Incorrect use of `Aja.vec/1`:
vec(#{Macro.to_string(call)}).
The `vec(a..b//c)` syntax can only be used with constants:
vec(1..100//5)
"""
end
end

Expand Down Expand Up @@ -348,30 +343,25 @@ defmodule Aja do
end
end

plus_enabled? = Version.compare(System.version(), "1.11.0") != :lt

if plus_enabled? do
@doc """
Convenience operator to concatenate an enumerable `right` to a vector `left`.
@doc """
Convenience operator to concatenate an enumerable `right` to a vector `left`.
`left` has to be an `Aja.Vector`, `right` can be any `Enumerable`.
`left` has to be an `Aja.Vector`, `right` can be any `Enumerable`.
It is just an alias for `Aja.Vector.concat/2`.
It is just an alias for `Aja.Vector.concat/2`.
Only available on Elixir versions >= 1.11.
Only available on Elixir versions >= 1.11.
## Examples
## Examples
iex> import Aja
iex> vec(5..1//-1) +++ vec([:boom, nil])
vec([5, 4, 3, 2, 1, :boom, nil])
iex> vec(5..1//-1) +++ 0..3
vec([5, 4, 3, 2, 1, 0, 1, 2, 3])
iex> import Aja
iex> vec(5..1//-1) +++ vec([:boom, nil])
vec([5, 4, 3, 2, 1, :boom, nil])
iex> vec(5..1//-1) +++ 0..3
vec([5, 4, 3, 2, 1, 0, 1, 2, 3])
"""
# TODO remove hack to support 1.10
defdelegate unquote(if(plus_enabled?, do: String.to_atom("+++"), else: :++))(left, right),
to: Aja.Vector,
as: :concat
end
"""
defdelegate left +++ right,
to: Aja.Vector,
as: :concat
end
27 changes: 10 additions & 17 deletions lib/vector.ex
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
defmodule Aja.Vector do
# TODO remove doc hack when stop supporting 1.10
plusplusplus_doc = ~S"""
## Convenience [`+++/2`](`Aja.+++/2`) operator
The `Aja.+++/2` operator can make appending to a vector more compact by aliasing `Aja.Vector.concat/2`:
iex> import Aja
iex> vec([1, 2, 3]) +++ vec([4, 5])
vec([1, 2, 3, 4, 5])
"""

@moduledoc ~s"""
Fast persistent vector with efficient appends and random access.
Expand Down Expand Up @@ -88,10 +77,13 @@ defmodule Aja.Vector do
iex> match?(v when vec_size(v) > 99, Aja.Vector.new(1..100))
true
#{if Version.compare(System.version(), "1.11.0") != :lt do
plusplusplus_doc
end}
## Convenience [`+++/2`](`Aja.+++/2`) operator
The `Aja.+++/2` operator can make appending to a vector more compact by aliasing `Aja.Vector.concat/2`:
iex> import Aja
iex> vec([1, 2, 3]) +++ vec([4, 5])
vec([1, 2, 3, 4, 5])
## Pattern-matching and opaque type
Expand Down Expand Up @@ -200,9 +192,10 @@ defmodule Aja.Vector do
**DO**
Aja.Vector.concat(vector, enumerable)
#{if Version.compare(System.version(), "1.11.0") != :lt do
"vector +++ enumerable"
end}
or
vector +++ enumerable
### Prefer `Aja.Enum` and `Aja.Vector` to `Enum` for vectors
Expand Down
Loading

0 comments on commit 06994ef

Please sign in to comment.