What Does Using a Single Pipe '|' in a Function Argument Do

What Does Using A Single Pipe '|' In A Function Argument Do?

Bitwise operators

Bitwise operators modify the bits of the values involved. A bitwise OR basically ORs together each bit of both the left and right argument. For example:

5 | 2

Would translate to bits/binary as:

101 | 10

Which would result in:

111

Because:

1 || 0 = 1
0 || 1 = 1
1 || 0 = 1

And as an Integer that is the representation of 7 which is exactly what you get if you:

echo 5 | 2;


In the words of Eddie Izzard... Flag!

As Ignacio states, this is most often used in PHP (and other langauges) as a way to combine multiple flags. Each flag is usually defined as a constant whose value is normally set to an integer that represents just one bit at a different offset:

define('FLAG_A', 1); /// 0001
define('FLAG_B', 2); /// 0010
define('FLAG_C', 4); /// 0100
define('FLAG_D', 8); /// 1000

Then when you OR these together they operate each on their own bit offset and will never collide:

FLAG_A | FLAG_C

Translates to:

1 | 100

So you end up turning on:

101

Which represents the integer 5.

Then all the code has to do—the code that will be reacting to the different flags being set—is the following (using a bitwise AND):

$combined_flags = FLAG_A | FLAG_C;

if ( $combined_flags & FLAG_A ) {
/// do something when FLAG_A is set
}

if ( $combined_flags & FLAG_B ) {
/// this wont be reached with the current value of $combined_flags
}

if ( $combined_flags & FLAG_C ) {
/// do something when FLAG_C is set
}

At the end of the day it just makes things easier to read by having named constants, and generally more optimal by relying on integer values rather than strings or arrays. Another benefit of using constants is that if they are ever mistyped when used, the compiler is in a better situation to tell and to throw a warning... if a string value is used it has no way of knowing that anything is wrong.

define('MY_FLAG_WITH_EASY_TYPO', 1);

my_function_that_expects_a_flag( MY_FLAG_WITH_EASY_TPYO );

/// if you have strict errors on the above will trigger an error

my_function_that_expects_a_flag( 'my_string_with_easy_tpyo' );

/// the above is just a string, the compiler knows nowt with
/// regard to it's correctness, so instead you'd have to
/// code your own checks.

What does pipe ( | ) mean in ML Programming?

This is pattern matching. From the link:

Functions may be composed of one or more rules. A rule consists of its function-name, an argument pattern and its expression. When the function is called the argument values will be matched to the patterns in top down order. Functions using pattern-matching are very similar to case expressions

This means the pipe separates cases for pattern matching. Pattern matching matches special patterns and executes specific expressions based on that pattern. They follow the syntax:

fun <fun-name> <pattern>
| <fun-name> <pattern>
| <fun-name> <pattern>;

Where <pattern> is:

<args> = <expressions>

In your first example, it is declaring function fac for factorial calculation. The first pattern is when the argument, int is 0. If int is 0, the expression of that case will execute, and in this case if the passed argument is 0, the result will be 1 (because 0 factorial is 1). If the passed argument is not 0 then it follows the next pattern (as it did not match the first pattern). If the passed argument is, say 2, it will perform recursion and find the factorial accordingly.


Consider the snippet below:

fun past "run"  = "ran"
| past "swim" = "swam"
| past x = x ^ "ed";

We define function named past that takes an argument and finds the passed tense of the argument. The first pattern is equivalent to - in plain English:

if the word (or passed argument) is "run", the passed tense is "ran".

The second pattern is equivalent to:

if the word (or passed argument) is "swim", the passed tense is "swam".

If the word is neither "swim" or "run" (and thus the two patterns are not matched) continue with the last pattern, which just adds "ed" to the end of the word.

With the example you can see that | (pipe) symbols separate patterns. You can think of patterns like this pseudocode:

if argument is "run", then passed tense is "ran"
else if argument is "swim", then passed tense is "swam"
else add "ed" to end of word

Pattern matching is used to cover irregular cases. Since "ran" and "swam" are irregular passed tenses, we cover those cases with patterns. The exact same principle applies to the first example - 0! is a special case, it is 1. Because of this, we can use pattern matching to match the specific case where the argument is 0 and execute according to that specific case.

What does the pipe(|) mean in typescript?

This is called union type in typescript.

A union type describes a value that can be one of several types.

Pipe (|) is used to separate each type, so for example number | string | boolean is the type of a value that can be a number, a string, or a boolean.

let something: number | string | boolean;

something = 1; // ok
something = '1'; // ok
something = true; // ok
something = {}; // Error: Type '{}' is not assignable to type 'string | number | boolean'

Playground


And here's an example similar to one in the question:

class Test1 {
public a: string
}

class Test2 {
public b: string
}

class Test3 {
}

let x: (typeof Test1 | typeof Test2)[];

x = [Test1]; //ok
x = [Test1, Test2]; //ok
x = [Test3]; //compilation error

x is an array containing constructors of Test1 or Test2.

Pipe character in Python

It is a bitwise OR of integers. For example, if one or both of ax or bx are 1, this evaluates to 1, otherwise to 0. It also works on other integers, for example 15 | 128 = 143, i.e. 00001111 | 10000000 = 10001111 in binary.

What does the vertical pipe ( | ) mean in C++?

Bitwise OR operator. It will set all bits true that are true in either of both values provided.

For example CS_HREDRAW could be 1 and CS_VREDRAW could be 2. Then it's very simple to check if they are set by using the bitwise AND operator &:

#define CS_HREDRAW 1
#define CS_VREDRAW 2
#define CS_ANOTHERSTYLE 4

unsigned int style = CS_HREDRAW | CS_VREDRAW;
if(style & CS_HREDRAW){
/* CS_HREDRAW set */
}

if(style & CS_VREDRAW){
/* CS_VREDRAW set */
}

if(style & CS_ANOTHERSTYLE){
/* CS_ANOTHERSTYLE set */
}

See also:

  • Wikipedia: Bitwise operation (Section OR)
  • Wikipedia: Mask (computing) Section( Common bitmask functions)

R: Using piping to pass a single argument to multiple locations in a function

You can reference piped data in a different position in the function by using the . In your case:

myDF2 <- babynames %>%
group_by(year) %>%
summarize(totalBirthsPerYear = sum(n)) %>%
slice(seq(1, nrow(.), by = 20))


Related Topics



Leave a reply



Submit