infix-operator

Are binary operators / infix functions in R generic? And how to make use of?

百般思念 提交于 2019-12-13 06:38:48
问题 From http://adv-r.had.co.nz/Functions.html or R: What are operators like %in% called and how can I learn about them? I learned that it is possible to write own "binary operators" or "infix functioncs" using the % -sign. One example would be '%+%' <- function(a, b) a*b x <- 2 y <- 3 x %+% y # gives 6 But is it possible to use them in a generic way if they are from a pre-defined class (so that in some cases I don't have to use the % -sign)? For exampple x + y shall give 6 if they are from the

Scala match decomposition on infix operator

那年仲夏 提交于 2019-12-05 03:59:34
I'm trying to understand the implementation of List s in Scala. In particular I'm trying to get my head around how you can write match expressions using an infix operator, for example: a match { case Nil => "An empty list" case x :: Nil => "A list without a tail" case x :: xs => "A list with a tail" } How is the match expression allowed to be x :: xs rather than List(x, xs) ? Daniel Spiewak Jay Conrad's answer is almost right. The important thing is that somewhere there is an object named :: which implements the unapply method, returning type Option[(A, List[A])] . Thusly: object :: { def

Assigning a value to a list item using `assign()`

让人想犯罪 __ 提交于 2019-12-03 05:03:23
A little bit of context first... I've written an infix function that in essence replaces the idiom x[[length(x) +1]] <- y ..or simply x <- append(x, y) for vectors. Here it is: `%+=%` <- function(x, y) { xcall <- substitute(x) xobjname <- setdiff(all.names(xcall), c("[[", "[", ":", "$")) # if the object doesn't exist, create it if (!exists(xobjname, parent.frame(), mode = "list") && !exists(xobjname, parent.frame(), mode = "numeric") && !exists(xobjname, parent.frame(), mode = "character")) { xobj <- subset(y, FALSE) } else { xobj <- eval(xcall, envir = parent.frame()) } if (is.atomic(xobj)) {

How to use `package::function()` inside a package when `function` is an infix operator?

孤人 提交于 2019-12-02 06:09:51
问题 According to H. Wickham's book R Packages , in the Package Metadata chapter, on how to add a package dependency, Hadley points out good reasons to explicitly refer to external functions using the syntax package::function() . Adding a package dependency here ensures that it’ll be installed. However, it does not mean that it will be attached along with your package (i.e., library(x)). The best practice is to explicitly refer to external functions using the syntax package::function() . This

Scala - defining own infix operators

Deadly 提交于 2019-12-01 20:29:57
Methods taking a single argument can be written as an infix operators in Scal. I.e. adding *(other:C) = foo(this, other) to class C, will allow us to write c1 * c2 instead of foo(c1,c2). But is there a way to define infix operators on existing classes that you cannot modify? E.g. if I wanted to write c1 + c2 instead of xor(c1,c2) , where c1,c2:Array[Byte] , I obviously cannot modify the Array-Class. I found this and tried implicit class Bytearray(a1:Array[Byte]) extends Anyval { def +(a2:Array[Byte]) = xor(a1,a2) } But that doesn't seem to work ( c1 + c2 ). Type mismatch, expected:String,

Haskell: Why aren't infix type constructors allowed?

狂风中的少年 提交于 2019-12-01 15:16:08
问题 In the Haskell 98 report, I found this: The syntax for Haskell type expressions is given above. Just as data values are built using data constructors, type values are built from type constructors. As with data constructors, the names of type constructors start with uppercase letters. Unlike data constructors, infix type constructors are not allowed (other than (->)). No reasons as to why infix type constructors aren't allowed are given. In Agda and the like, infix type constructors are

Why is the unary minus operator problematic in this expression: (- 2) 1? [duplicate]

天大地大妈咪最大 提交于 2019-11-28 08:30:05
问题 This question already has answers here : Prefix form of unary operator in Haskell (4 answers) Closed 4 years ago . All of the following expressions get evaluated without mishap: (+2) 1 -- 3 (*2) 1 -- 2 ((-)2) 1 -- 1 (2-) 1 -- 1 (/2) 1 -- 0.5 (2/) 1 -- 2.0 but not this one: (-2) 1 -- the inferred type is ambiguous GHC throws some error about the inferred type being ambiguous. Why? 回答1: Each of these parenthesized expressions but (-2) (edit: and ((-) 2) ) are sections , i.e. functions that take