Skip to content

Commit

Permalink
Merge pull request #257 from rgwood/math-mode
Browse files Browse the repository at this point in the history
Remove math mode mentions, move short hand syntax documentation
  • Loading branch information
sophiajt committed Mar 7, 2022
2 parents d9839a5 + c151cac commit 3226654
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
20 changes: 0 additions & 20 deletions book/math.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,3 @@ Math operations are evaluated in the follow order (from highest precedence to lo
> 3 * (1 + 2)
9
```

## Short-hand math mode

A variation of math mode that Nushell supports is called "short-hand" math mode. This is because it gives you a way of accessing columns using a simple short-hand.

You may have already used this functionality before. If, for example, we wanted to only see rows from `ls` where the entry is at least ten kilobytes, we can write:

```
> ls | where size > 10kb
```

The `where size > 10kb` is a command with two parts: the command name `where` and the short-hand math expression `size > 10kb`. We say short-hand because `size` here is the shortened version of writing `$it.size`. If we look at the fully expanded form, we would see:

```
> ls | where {|$it| $it.size > 10kb }
```

Rather than having to type all this out every time a command needs to work with column data, we use this short-hand mode to access column data.

For the expansion to work, the column name must appear on the left-hand side of the operation. Above, `size` appears on the left-hand side of the comparison, which allows the expression to expand into the full math mode block.
18 changes: 18 additions & 0 deletions book/variables_and_subexpressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,21 @@ We can do a very similar action in a single step using a subexpression path:
```

It depends on the needs of the code and your particular style which form works best for you.

## Short-hand subexpressions (row conditions)

Nushell supports accessing columns in a subexpression using a simple short-hand. You may have already used this functionality before. If, for example, we wanted to only see rows from `ls` where the entry is at least ten kilobytes we can write:

```
> ls | where size > 10kb
```

The `where size > 10kb` is a command with two parts: the command name `where` and the short-hand expression `size > 10kb`. We say short-hand because `size` here is the shortened version of writing `$it.size`. This could also be written in any of the following ways:

```
> ls | where $it.size > 10kb
> ls | where ($it.size > 10kb)
> ls | where {|$it| $it.size > 10kb }
```

For short-hand syntax to work, the column name must appear on the left-hand side of the operation (like `size` in `size > 10kb`).
5 changes: 1 addition & 4 deletions book/working_with_lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ let colors = []
= $colors | empty? # true
```
The `in` and `not in` operators are used to test whether a value is in a list.
Operators can only be used in "math mode".
One way to enter math mode is to begin an expression with `=`.
For example:
The `in` and `not-in` operators are used to test whether a value is in a list. For example:
```bash
let colors = [red green blue]
Expand Down

0 comments on commit 3226654

Please sign in to comment.