Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to implement F#'s forward pipe operator in R? [duplicate]

Tags:

r

f#

How can you implement F#'s forward pipe operator in R? The operator makes it possible to easily chain a sequence of calculations. For example, when you have an input data and want to call functions foo and bar in sequence, you can write:

data |> foo |> bar

Instead of writing bar(foo(data)). The benefits are that you avoid some parentheses and the computations are written in the same order in which they are executed (left-to-right). In F#, the operator is defined as follows:

let (|>) a f = f a

It would appear that %...% can be used for binary operators, but how would this work?

like image 651
user4 Avatar asked Jan 17 '12 14:01

user4


People also ask

How do we implement a logical expression?

The functions AND and OR may be used to test two or more logical expressions, while the NOT function is used to reverse the truth value of a logical expression. The XOR function returns TRUE when one, and only one, of the logical expressions is true.

How do you implement a multiplexer?

There are certain steps involved in it: Step 1: Draw the truth table for the given number of variable function. Step 2: Consider one variable as input and remaining variables as select lines. Step 3: Form a matrix where input lines of MUX are columns and input variable and its compliment are rows.

How do you implement a function using a decoder?

Thus, a Decoder can be used to implement a function of n variables simply by connecting the outputs of the Decoder that correspond to the minterms of a function to a multi-input OR gate. Consider the Decoder circuit shown in Figure 7 which implements two separate functions of three variables.

How do you implement NAND gates?

To implement NAND operation using OR gate, we first complement the inputs and then perform OR on the complemented inputs. We get A' + B'. A' + B' is equivalent to (AB)' which can be shown to be true by using de morgan's law.


1 Answers

I don't know how well it would hold up to any real use, but this seems (?) to do what you want, at least for single-argument functions ...

> "%>%" <- function(x,f) do.call(f,list(x))
> pi %>% sin
[1] 1.224606e-16
> pi %>% sin %>% cos
[1] 1
> cos(sin(pi))
[1] 1

For what it's worth, as of now (3 December 2021), in addition to the magrittr/tidyverse pipe (%>%), there also a native pipe |> in R (and an experimental => operator that can be enabled in the development version): see here, for example.

like image 101
Ben Bolker Avatar answered Sep 23 '22 20:09

Ben Bolker