Skip to content

Commit

Permalink
rework all bianry operators
Browse files Browse the repository at this point in the history
  • Loading branch information
sorhawell committed Aug 4, 2023
1 parent 26d54db commit 2d44903
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 57 deletions.
51 changes: 29 additions & 22 deletions R/expr__expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ wrap_e_legacy = function(e, str_to_lit = TRUE) {
}
}

#' DEPRECATED wrap as literal
#' wrap as literal
#' @description use robj_to!(Expr) on rust side or rarely wrap_e on R-side
#' This function is only kept for reference
#' @param e an Expr(polars) or any R expression
Expand Down Expand Up @@ -188,7 +188,7 @@ wrap_elist_result = function(elist, str_to_lit = TRUE) {
#' pl$lit(5)$add(pl$lit(10))
#' +pl$lit(5) # unary use resolves to same as pl$lit(5)
Expr_add = function(other) {
.pr$Expr$add(self, wrap_e(other) |> result() |> unwrap("in $add()"))
.pr$Expr$add(self, other) |> unwrap("in $add()")
}
#' @export
#' @rdname Expr_add
Expand All @@ -210,7 +210,7 @@ Expr_add = function(other) {
#' pl$lit(5) / pl$lit(10)
#' pl$lit(5)$div(pl$lit(10))
Expr_div = function(other) {
.pr$Expr$div(self, wrap_e(other) |> result() |> unwrap("in $div()"))
.pr$Expr$div(self, other) |> unwrap("in $div()")
}
#' @export
#' @rdname Expr_div
Expand All @@ -230,7 +230,7 @@ Expr_div = function(other) {
#' pl$lit(5)$sub(pl$lit(10))
#' -pl$lit(5)
Expr_sub = function(other) {
.pr$Expr$sub(self, wrap_e(other) |> result() |> unwrap("in $sub"))
.pr$Expr$sub(self, other) |> unwrap("in $sub()")
}
#' @export
#' @rdname Expr_sub
Expand All @@ -253,7 +253,7 @@ Expr_sub = function(other) {
#' pl$lit(5) * pl$lit(10)
#' pl$lit(5)$mul(pl$lit(10))
Expr_mul = Expr_mul = function(other) {
.pr$Expr$mul(self, wrap_e(other) |> result() |> unwrap("in $mul"))
.pr$Expr$mul(self, other) |> unwrap("in $mul()")
}

#' @export
Expand Down Expand Up @@ -292,7 +292,7 @@ Expr_is_not = "use_extendr_wrapper"
#' pl$lit(5) < pl$lit(10)
#' pl$lit(5)$lt(pl$lit(10))
Expr_lt = function(other) {
.pr$Expr$lt(self, wrap_e(other) |> result() |> unwrap("in $lt()"))
.pr$Expr$lt(self, other) |> unwrap("in $lt()")
}
#' @export
#' @details
Expand All @@ -313,7 +313,7 @@ Expr_lt = function(other) {
#' pl$lit(2) > pl$lit(1)
#' pl$lit(2)$gt(pl$lit(1))
Expr_gt = function(other) {
.pr$Expr$gt(self, wrap_e(other) |> result() |> unwrap("in $gt()"))
.pr$Expr$gt(self, other) |> unwrap("in $gt()")
}
#' @export
#' @details
Expand All @@ -334,7 +334,7 @@ Expr_gt = function(other) {
#' pl$lit(2) == pl$lit(2)
#' pl$lit(2)$eq(pl$lit(2))
Expr_eq = function(other) {
.pr$Expr$eq(self, wrap_e(other) |> result() |> unwrap("in $eq()"))
.pr$Expr$eq(self, other) |> unwrap("in $eq()")
}
#' @export
#' @details
Expand All @@ -356,7 +356,7 @@ Expr_eq = function(other) {
#' pl$lit(1) != pl$lit(2)
#' pl$lit(1)$neq(pl$lit(2))
Expr_neq = function(other) {
.pr$Expr$neq(self, wrap_e(other) |> result() |> unwrap("in $neq()" ))
.pr$Expr$neq(self, other) |> unwrap("in $neq()" )
}
#' @export
#' @details
Expand All @@ -377,7 +377,7 @@ Expr_neq = function(other) {
#' pl$lit(2) <= pl$lit(2)
#' pl$lit(2)$lt_eq(pl$lit(2))
Expr_lt_eq = function(other) {
.pr$Expr$lt_eq(self, wrap_e(other) |> result() |> unwrap("in $lt_eq()" ))
.pr$Expr$lt_eq(self, other) |> unwrap("in $lt_eq()" )
}
#' @export
#' @details
Expand All @@ -399,7 +399,7 @@ Expr_lt_eq = function(other) {
#' pl$lit(2) >= pl$lit(2)
#' pl$lit(2)$gt_eq(pl$lit(2))
Expr_gt_eq = function(other) {
.pr$Expr$gt_eq(self, wrap_e(other) |> result() |> unwrap("in $gt_eq()" ))
.pr$Expr$gt_eq(self, other) |> unwrap("in $gt_eq()" )
}
#' @export
#' @details
Expand Down Expand Up @@ -900,9 +900,11 @@ Expr_reverse = function() {
#' @examples
#' pl$lit(TRUE) & TRUE
#' pl$lit(TRUE)$and(pl$lit(TRUE))
Expr_and = "use_extendr_wrapper"
Expr_and = function(other) {
.pr$Expr$and(self, other) |> unwrap("in $and()")
}
#' @export
"&.Expr" = function(e1, e2) wrap_e(e1)$and(wrap_e(e2))
"&.Expr" = function(e1, e2) result(wrap_e(e1)$and(e2)) |> unwrap("using the '&'-operator")


#' Or
Expand All @@ -918,9 +920,11 @@ Expr_and = "use_extendr_wrapper"
#' @examples
#' pl$lit(TRUE) | FALSE
#' pl$lit(TRUE)$or(pl$lit(TRUE))
Expr_or = "use_extendr_wrapper"
Expr_or = function(other) {
.pr$Expr$or(self, other) |> unwrap("in $or()")
}
#' @export
"|.Expr" = function(e1, e2) wrap_e(e1)$or(wrap_e(e2))
"|.Expr" = function(e1, e2) result(wrap_e(e1)$or(e2)) |> unwrap("using the '|'-operator")


#' Xor
Expand All @@ -934,7 +938,9 @@ Expr_or = "use_extendr_wrapper"
#' @usage Expr_xor(other)
#' @examples
#' pl$lit(TRUE)$xor(pl$lit(FALSE))
Expr_xor = "use_extendr_wrapper"
Expr_xor = function(other) {
.pr$Expr$xor(self, other) |> unwrap("in $xor()")
}



Expand Down Expand Up @@ -1011,8 +1017,7 @@ Expr_cast = function(dtype, strict = TRUE) {
#' pl$lit(2) %**% (pl$col("a"))
#' )$get_column("a")$to_r() == (-1:3)^2
Expr_rpow = function(base) {
if (!inherits(base, "Expr")) base <- pl$lit(base)
expr = .pr$Expr$pow(base, self)
result(wrap_e(base)$pow(self)) |> unwrap("in $rpow()")
}

#' @rdname Expr_rpow
Expand All @@ -1023,7 +1028,7 @@ Expr_rpow = function(base) {

#' @rdname Expr_rpow
#' @export
"%**%.Expr" = function(e1, e2) wrap_e(e1)$rpow(e2)
"%**%.Expr" = function(e1, e2) result(wrap_e(e1)$rpow(e2))|> unwrap("using the '**'-operator")


#' Square root
Expand Down Expand Up @@ -2446,10 +2451,10 @@ Expr_limit = function(n = 10) {
#' pl$lit(2)^(pl$col("a"))
#' )$get_column("literal")$to_r() == 2^(-1:3)
Expr_pow = function(exponent) {
.pr$Expr$pow(self, wrap_e(exponent))
.pr$Expr$pow(self, exponent) |> unwrap("in $pow()")
}
#' @export
"^.Expr" = function(e1, e2) wrap_e(e1)$pow(e2)
"^.Expr" = function(e1, e2) result(wrap_e(e1)$pow(e2)) |> unwrap("using '^'-operator")


#' is_in
Expand All @@ -2468,7 +2473,9 @@ Expr_pow = function(exponent) {
#' pl$col("a")$is_in(pl$lit(NA_real_))
#' )$to_data_frame()[[1L]]
#'
Expr_is_in = "use_extendr_wrapper"
Expr_is_in = function(other) {
.pr$Expr$is_in(self, other) |> unwrap("in $is_in()")
}

## TODO contribute polars, do not panic on by pointing to non positive values
#' Repeat by
Expand Down
8 changes: 4 additions & 4 deletions man/nanoarrow.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/wrap_e.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 30 additions & 30 deletions src/rust/src/lazy/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,45 +146,45 @@ impl Expr {
}

//expr binary comparisons
pub fn gt(&self, other: &Expr) -> Self {
self.0.clone().gt(other.0.clone()).into()
pub fn gt(&self, other: Robj) -> RResult<Self> {
Ok(self.0.clone().gt(robj_to!(PLExpr, other)?).into())
}

pub fn gt_eq(&self, other: &Expr) -> Self {
self.0.clone().gt_eq(other.0.clone()).into()
pub fn gt_eq(&self, other: Robj) -> RResult<Self> {
Ok(self.0.clone().gt_eq(robj_to!(PLExpr, other)?).into())
}

pub fn lt(&self, other: &Expr) -> Self {
self.0.clone().lt(other.0.clone()).into()
pub fn lt(&self, other: Robj) -> RResult<Self> {
Ok(self.0.clone().lt(robj_to!(PLExpr, other)?).into())
}

pub fn lt_eq(&self, other: &Expr) -> Self {
self.0.clone().lt_eq(other.0.clone()).into()
pub fn lt_eq(&self, other: Robj) -> RResult<Self> {
Ok(self.0.clone().lt_eq(robj_to!(PLExpr, other)?).into())
}

pub fn neq(&self, other: &Expr) -> Self {
self.0.clone().neq(other.0.clone()).into()
pub fn neq(&self, other: Robj) -> RResult<Self> {
Ok(self.0.clone().neq(robj_to!(PLExpr, other)?).into())
}

pub fn eq(&self, other: &Expr) -> Self {
self.0.clone().eq(other.0.clone()).into()
pub fn eq(&self, other: Robj) -> RResult<Self> {
Ok(self.0.clone().eq(robj_to!(PLExpr, other)?).into())
}

//logical operators
fn and(&self, other: &Expr) -> Self {
self.0.clone().and(other.0.clone()).into()
fn and(&self, other: Robj) -> RResult<Self> {
Ok(self.0.clone().and(robj_to!(PLExpr, other)?).into())
}

fn or(&self, other: &Expr) -> Self {
self.0.clone().or(other.0.clone()).into()
fn or(&self, other: Robj) -> RResult<Self> {
Ok(self.0.clone().or(robj_to!(PLExpr, other)?).into())
}

fn xor(&self, other: &Expr) -> Self {
self.0.clone().xor(other.0.clone()).into()
fn xor(&self, other: Robj) -> RResult<Self> {
Ok(self.0.clone().xor(robj_to!(PLExpr, other)?).into())
}

fn is_in(&self, other: &Expr) -> Self {
self.0.clone().is_in(other.0.clone()).into()
fn is_in(&self, other: Robj) -> RResult<Self> {
Ok(self.0.clone().is_in(robj_to!(PLExpr, other)?).into())
}

//any not translated expr from expr/expr.py
Expand Down Expand Up @@ -1410,8 +1410,8 @@ impl Expr {
self.clone().0.dt().offset_by(by).into()
}

pub fn pow(&self, exponent: &Expr) -> Self {
self.0.clone().pow(exponent.0.clone()).into()
pub fn pow(&self, exponent: Robj) -> RResult<Self> {
Ok(self.0.clone().pow(robj_to!(PLExpr, exponent)?).into())
}

pub fn repeat_by(&self, by: &Expr) -> Self {
Expand Down Expand Up @@ -1590,21 +1590,21 @@ impl Expr {
}

//binary arithmetic expressions
pub fn add(&self, other: &Expr) -> Self {
self.0.clone().add(other.0.clone()).into()
pub fn add(&self, other: Robj) -> RResult<Self> {
Ok(self.0.clone().add(robj_to!(PLExpr, other)?).into())
}

//binary arithmetic expressions
pub fn sub(&self, other: &Expr) -> Self {
self.0.clone().sub(other.0.clone()).into()
pub fn sub(&self, other: Robj) -> RResult<Self> {
Ok(self.0.clone().sub(robj_to!(PLExpr, other)?).into())
}

pub fn mul(&self, other: &Expr) -> Self {
self.0.clone().mul(other.0.clone()).into()
pub fn mul(&self, other: Robj) -> RResult<Self> {
Ok(self.0.clone().mul(robj_to!(PLExpr, other)?).into())
}

pub fn div(&self, other: &Expr) -> Self {
self.0.clone().div(other.0.clone()).into()
pub fn div(&self, other: Robj) -> RResult<Self> {
Ok(self.0.clone().div(robj_to!(PLExpr, other)?).into())
}

//unary
Expand Down

0 comments on commit 2d44903

Please sign in to comment.