Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename trim functions #674

Merged
merged 8 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/explorer/backend/lazy_series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ defmodule Explorer.Backend.LazySeries do
covariance: 2,
# Strings
contains: 2,
trim_leading: 1,
trim_trailing: 1,
trim: 2,
lstrip: 2,
rstrip: 2,
strip: 2,
upcase: 1,
downcase: 1,
substring: 3,
Expand Down Expand Up @@ -874,21 +874,21 @@ defmodule Explorer.Backend.LazySeries do
end

@impl true
def trim(series, string) do
data = new(:trim, [lazy_series!(series), string])
def strip(series, string) do
data = new(:strip, [lazy_series!(series), string])
Backend.Series.new(data, :string)
end

@impl true
def trim_leading(series) do
data = new(:trim_leading, [lazy_series!(series)])
def lstrip(series, string) do
data = new(:lstrip, [lazy_series!(series), string])

Backend.Series.new(data, :string)
end

@impl true
def trim_trailing(series) do
data = new(:trim_trailing, [lazy_series!(series)])
def rstrip(series, string) do
data = new(:rstrip, [lazy_series!(series), string])

Backend.Series.new(data, :string)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/explorer/backend/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ defmodule Explorer.Backend.Series do
@callback contains(s, String.t()) :: s
@callback upcase(s) :: s
@callback downcase(s) :: s
@callback trim(s, String.t() | nil) :: s
@callback trim_leading(s) :: s
@callback trim_trailing(s) :: s
@callback strip(s, String.t() | nil) :: s
@callback lstrip(s, String.t() | nil) :: s
@callback rstrip(s, String.t() | nil) :: s
@callback substring(s, integer(), non_neg_integer() | nil) :: s

# Date / DateTime
Expand Down
6 changes: 3 additions & 3 deletions lib/explorer/polars_backend/expression.ex
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ defmodule Explorer.PolarsBackend.Expression do

# Strings
contains: 2,
trim: 2,
trim_leading: 1,
trim_trailing: 1,
strip: 2,
lstrip: 2,
rstrip: 2,
downcase: 1,
upcase: 1,
substring: 3
Expand Down
6 changes: 3 additions & 3 deletions lib/explorer/polars_backend/native.ex
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ defmodule Explorer.PolarsBackend.Native do
def s_is_null(_s), do: err()
def s_less(_s, _rhs), do: err()
def s_less_equal(_s, _rhs), do: err()
def s_trim_leading(_s), do: err()
def s_lstrip(_s, _string), do: err()
def s_mask(_s, _filter), do: err()
def s_max(_s), do: err()
def s_mean(_s), do: err()
Expand Down Expand Up @@ -311,7 +311,7 @@ defmodule Explorer.PolarsBackend.Native do
def s_round(_s, _decimals), do: err()
def s_floor(_s), do: err()
def s_ceil(_s), do: err()
def s_trim_trailing(_s), do: err()
def s_rstrip(_s, _string), do: err()
def s_rank(_s, _method, _descending, _seed), do: err()
def s_sample_n(_s, _n, _replace, _shuffle, _seed), do: err()
def s_sample_frac(_s, _frac, _replace, _shuffle, _seed), do: err()
Expand All @@ -322,7 +322,7 @@ defmodule Explorer.PolarsBackend.Native do
def s_slice_by_series(_s, _series), do: err()
def s_sort(_s, _descending?, _nils_last?), do: err()
def s_standard_deviation(_s), do: err()
def s_trim(_s, _string), do: err()
def s_strip(_s, _string), do: err()
def s_subtract(_s, _other), do: err()
def s_sum(_s), do: err()
def s_tail(_s, _length), do: err()
Expand Down
12 changes: 6 additions & 6 deletions lib/explorer/polars_backend/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -620,16 +620,16 @@ defmodule Explorer.PolarsBackend.Series do
do: Shared.apply_series(series, :s_downcase)

@impl true
def trim(series, str),
do: Shared.apply_series(series, :s_trim, [str])
def strip(series, str),
do: Shared.apply_series(series, :s_strip, [str])

@impl true
def trim_leading(series),
do: Shared.apply_series(series, :s_trim_leading)
def lstrip(series, str),
do: Shared.apply_series(series, :s_lstrip, [str])

@impl true
def trim_trailing(series),
do: Shared.apply_series(series, :s_trim_trailing)
def rstrip(series, str),
do: Shared.apply_series(series, :s_rstrip, [str])

@impl true
def substring(series, offset, length),
Expand Down
114 changes: 86 additions & 28 deletions lib/explorer/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4312,88 +4312,146 @@ defmodule Explorer.Series do
def downcase(%Series{dtype: dtype}), do: dtype_error("downcase/1", dtype, [:string])

@doc """
Returns a string where all leading and trailing Unicode whitespaces have been removed.
Returns a string series where all leading and trailing Unicode whitespaces
have been removed.

## Examples

iex> s = Explorer.Series.from_list([" abc", "def ", " bcd "])
iex> Explorer.Series.trim(s)
iex> Explorer.Series.strip(s)
#Explorer.Series<
Polars[3]
string ["abc", "def", "bcd"]
>

"""
@doc type: :string_wise
@spec trim(Series.t()) :: Series.t()
def trim(%Series{dtype: :string} = series),
do: apply_series(series, :trim, [nil])
@spec strip(Series.t()) :: Series.t()
def strip(%Series{dtype: :string} = series),
do: apply_series(series, :strip, [nil])

@doc """
Returns a string where all leading and trailing examples of the provided string
Returns a string series where all leading and trailing examples of the provided string
have been removed.

Where multiple characters are provided, all combinations of this set of characters will be stripped

## Examples

iex> s = Explorer.Series.from_list(["abc", "adefa", "bcda"])
iex> Explorer.Series.trim(s, "a")
iex> s = Explorer.Series.from_list(["£123", "1.00£", "£1.00£"])
iex> Explorer.Series.strip(s, "£")
#Explorer.Series<
Polars[3]
string ["bc", "def", "bcd"]
string ["123", "1.00", "1.00"]
>

iex> s = Explorer.Series.from_list(["£123", "1.00£", "£1.00£"])
iex> Explorer.Series.trim(s, "£")
iex> s = Explorer.Series.from_list(["abc", "adefa", "bcda"])
iex> Explorer.Series.strip(s, "ab")
#Explorer.Series<
Polars[3]
string ["123", "1.00", "1.00"]
string ["c", "def", "cd"]
>

"""
@doc type: :string_wise
@spec trim(Series.t(), String.t()) :: Series.t()
def trim(%Series{dtype: :string} = series, string) when is_binary(string),
do: apply_series(series, :trim, [string])
@spec strip(Series.t(), String.t()) :: Series.t()
def strip(%Series{dtype: :string} = series, string) when is_binary(string),
do: apply_series(series, :strip, [string])

def trim(%Series{dtype: dtype}, _string), do: dtype_error("trim/2", dtype, [:string])
def strip(%Series{dtype: dtype}, _string), do: dtype_error("strip/2", dtype, [:string])

@doc """
Returns a string where all leading Unicode whitespaces have been removed.
Returns a string series where all leading Unicode whitespaces have been removed.

## Examples

iex> s = Explorer.Series.from_list([" abc", "def ", " bcd"])
iex> Explorer.Series.trim_leading(s)
iex> Explorer.Series.lstrip(s)
#Explorer.Series<
Polars[3]
string ["abc", "def ", "bcd"]
>
"""
@doc type: :string_wise
@spec trim_leading(Series.t()) :: Series.t()
def trim_leading(%Series{dtype: :string} = series),
do: apply_series(series, :trim_leading)
@spec lstrip(Series.t()) :: Series.t()
def lstrip(%Series{dtype: :string} = series),
do: apply_series(series, :lstrip, [nil])

@doc """
Returns a string series where all leading examples of the provided string
have been removed.

Where multiple characters are provided, all combinations of this set of characters will be stripped
## Examples

iex> s = Explorer.Series.from_list(["$1", "$$200$$", "$$$3000$"])
iex> Explorer.Series.lstrip(s, "$")
#Explorer.Series<
Polars[3]
string ["1", "200$$", "3000$"]
>

iex> s = Explorer.Series.from_list(["abc", "adefa", "bcda"])
iex> Explorer.Series.lstrip(s, "ab")
#Explorer.Series<
Polars[3]
string ["c", "defa", "cda"]
>
"""
@doc type: :string_wise
@spec lstrip(Series.t(), String.t()) :: Series.t()
def lstrip(%Series{dtype: :string} = series, string) when is_binary(string),
do: apply_series(series, :lstrip, [string])

def trim_leading(%Series{dtype: dtype}), do: dtype_error("trim_leading/1", dtype, [:string])
def lstrip(%Series{dtype: dtype}, _string),
do: dtype_error("lstrip/2", dtype, [:string])

@doc """
Returns a string where all trailing Unicode whitespaces have been removed.
Returns a string series where all trailing Unicode whitespaces have been removed.

## Examples

iex> s = Explorer.Series.from_list([" abc", "def ", " bcd"])
iex> Explorer.Series.trim_trailing(s)
iex> Explorer.Series.rstrip(s)
#Explorer.Series<
Polars[3]
string [" abc", "def", " bcd"]
>
"""
@doc type: :string_wise
@spec trim_trailing(Series.t()) :: Series.t()
def trim_trailing(%Series{dtype: :string} = series),
do: apply_series(series, :trim_trailing)
@spec rstrip(Series.t()) :: Series.t()
def rstrip(%Series{dtype: :string} = series),
do: apply_series(series, :rstrip, [nil])

@doc """
Returns a string series where all trailing examples of the provided string
have been removed.

Where multiple characters are provided, all combinations of this set of characters will be stripped

## Examples

iex> s = Explorer.Series.from_list(["__abc__", "def_", "__bcd_"])
iex> Explorer.Series.rstrip(s, "_")
#Explorer.Series<
Polars[3]
string ["__abc", "def", "__bcd"]
>

iex> s = Explorer.Series.from_list(["abc", "adefa", "bcdabaaa"])
iex> Explorer.Series.rstrip(s, "ab")
#Explorer.Series<
Polars[3]
string ["abc", "adef", "bcd"]
>
"""
@doc type: :string_wise
@spec rstrip(Series.t(), String.t()) :: Series.t()
def rstrip(%Series{dtype: :string} = series, string) when is_binary(string),
do: apply_series(series, :rstrip, [string])

def trim_trailing(%Series{dtype: dtype}), do: dtype_error("trim_trailing/1", dtype, [:string])
def rstrip(%Series{dtype: dtype}, _string),
do: dtype_error("rstrip/2", dtype, [:string])

@doc """
Returns a string sliced from the offset to the end of the string, supporting
Expand Down
10 changes: 5 additions & 5 deletions native/explorer/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,21 +748,21 @@ pub fn expr_downcase(expr: ExExpr) -> ExExpr {
}

#[rustler::nif]
pub fn expr_trim(expr: ExExpr, string: Option<String>) -> ExExpr {
pub fn expr_strip(expr: ExExpr, string: Option<String>) -> ExExpr {
let expr = expr.clone_inner();
ExExpr::new(expr.str().strip(string))
}

#[rustler::nif]
pub fn expr_trim_leading(expr: ExExpr) -> ExExpr {
pub fn expr_lstrip(expr: ExExpr, string: Option<String>) -> ExExpr {
let expr = expr.clone_inner();
ExExpr::new(expr.str().lstrip(None))
ExExpr::new(expr.str().lstrip(string))
}

#[rustler::nif]
pub fn expr_trim_trailing(expr: ExExpr) -> ExExpr {
pub fn expr_rstrip(expr: ExExpr, string: Option<String>) -> ExExpr {
let expr = expr.clone_inner();
ExExpr::new(expr.str().rstrip(None))
ExExpr::new(expr.str().rstrip(string))
}

#[rustler::nif]
Expand Down
12 changes: 6 additions & 6 deletions native/explorer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ rustler::init!(
expr_contains,
expr_upcase,
expr_downcase,
expr_trim,
expr_trim_leading,
expr_trim_trailing,
expr_strip,
expr_lstrip,
expr_rstrip,
expr_substring,
// float round expressions
expr_round,
Expand Down Expand Up @@ -343,7 +343,7 @@ rustler::init!(
s_is_nan,
s_less,
s_less_equal,
s_trim_leading,
s_lstrip,
s_mask,
s_max,
s_mean,
Expand Down Expand Up @@ -390,7 +390,7 @@ rustler::init!(
s_remainder,
s_rename,
s_reverse,
s_trim_trailing,
s_rstrip,
s_sample_n,
s_sample_frac,
s_series_equal,
Expand All @@ -403,7 +403,7 @@ rustler::init!(
s_sort,
s_standard_deviation,
s_tan,
s_trim,
s_strip,
s_substring,
s_subtract,
s_sum,
Expand Down
Loading