mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-13 01:47:51 +08:00
Update docs in website/learn to use the new assignment syntax.
This commit is contained in:
parent
003557c22a
commit
2bedb5248b
|
@ -97,7 +97,7 @@ this is `put`:
|
|||
```elvish-transcript
|
||||
~> put foo
|
||||
▶ foo
|
||||
~> x = (put foo)
|
||||
~> var x = (put foo)
|
||||
~> put $x
|
||||
▶ foo
|
||||
```
|
||||
|
@ -109,7 +109,7 @@ old byte-based output as well. But let's try this:
|
|||
~> put "a\nb" [foo bar]
|
||||
▶ "a\nb"
|
||||
▶ [foo bar]
|
||||
~> s li = (put "a\nb" [foo bar])
|
||||
~> var s li = (put "a\nb" [foo bar])
|
||||
~> put $s
|
||||
▶ "a\nb"
|
||||
~> put $li[0]
|
||||
|
@ -129,7 +129,7 @@ write to structured output, like `str:split`:
|
|||
~> str:split , foo,bar
|
||||
▶ foo
|
||||
▶ bar
|
||||
~> words = [(str:split , foo,bar)]
|
||||
~> var words = [(str:split , foo,bar)]
|
||||
~> put $words
|
||||
▶ [foo bar]
|
||||
```
|
||||
|
@ -177,7 +177,7 @@ can write to both, and output capture will capture both:
|
|||
~> f
|
||||
bytes
|
||||
▶ value
|
||||
~> outs = [(f)]
|
||||
~> var outs = [(f)]
|
||||
~> put $outs
|
||||
▶ [bytes value]
|
||||
```
|
||||
|
@ -187,7 +187,7 @@ byte and value outputs, and it can recover the output sent to `echo`. When byte
|
|||
output contains multiple lines, each line becomes one value:
|
||||
|
||||
```elvish-transcript
|
||||
~> x = [(echo "lorem\nipsum")]
|
||||
~> var x = [(echo "lorem\nipsum")]
|
||||
~> put $x
|
||||
▶ [lorem ipsum]
|
||||
```
|
||||
|
@ -287,7 +287,7 @@ comma-separated value, reduplicate each value (using commas as separators), and
|
|||
rejoin them with semicolons, you can write:
|
||||
|
||||
```elvish-transcript
|
||||
~> csv = a,b,foo,bar
|
||||
~> var csv = a,b,foo,bar
|
||||
~> use str
|
||||
~> str:join ';' [(each {|x| put $x,$x } [(str:split , $csv)])]
|
||||
▶ 'a,a;b,b;foo,foo;bar,bar'
|
||||
|
@ -304,7 +304,7 @@ The answer to that particular question is in the next subsection, but for the
|
|||
program at hand, there is a much better way to write it:
|
||||
|
||||
```elvish-transcript
|
||||
~> csv = a,b,foo,bar
|
||||
~> var csv = a,b,foo,bar
|
||||
~> use str
|
||||
~> str:split , $csv | each {|x| put $x,$x } | str:join ';'
|
||||
▶ 'a,a;b,b;foo,foo;bar,bar'
|
||||
|
@ -421,7 +421,7 @@ list:
|
|||
▶ a
|
||||
▶ b
|
||||
▶ c
|
||||
~> li = (str:split , a,b,c)
|
||||
~> var li = (str:split , a,b,c)
|
||||
Exception: arity mismatch: assignment right-hand-side must be 1 value, but is 3 values
|
||||
[tty], line 1: li = (str:split , a,b,c)
|
||||
```
|
||||
|
@ -433,10 +433,10 @@ constructing a list or using rest variables:
|
|||
|
||||
```elvish-transcript
|
||||
~> use str
|
||||
~> li = [(str:split , a,b,c)]
|
||||
~> var li = [(str:split , a,b,c)]
|
||||
~> put $li
|
||||
▶ [a b c]
|
||||
~> @li = (str:split , a,b,c) # equivalent and slightly shorter
|
||||
~> var @li = (str:split , a,b,c) # equivalent and slightly shorter
|
||||
```
|
||||
|
||||
## Assigning Multiple Variables
|
||||
|
|
|
@ -168,7 +168,7 @@ Which works until you want a different message. One way to solve this is using
|
|||
**variables**:
|
||||
|
||||
```elvish-transcript
|
||||
~> name = John
|
||||
~> var name = John
|
||||
~> echo Hello, $name!
|
||||
Hello, John!
|
||||
```
|
||||
|
@ -178,7 +178,7 @@ the previous command. To greet a different person, you can just change the value
|
|||
of the variable, and the command doesn't need to change:
|
||||
|
||||
```elvish-transcript
|
||||
~> name = Jane
|
||||
~> var name = Jane
|
||||
~> echo Hello, $name!
|
||||
Hello, Jane!
|
||||
```
|
||||
|
@ -187,7 +187,7 @@ Using variables has another advantage: after defining a variable, you can use it
|
|||
as many times as you want:
|
||||
|
||||
```elvish-transcript
|
||||
~> name = Jane
|
||||
~> var name = Jane
|
||||
~> echo Hello, $name!
|
||||
Hello, Jane!
|
||||
~> echo Bye, $name!
|
||||
|
@ -221,7 +221,7 @@ store a **list** of values in one variable; a list can be written by surrounding
|
|||
some values with `[` and `]`. For example:
|
||||
|
||||
```elvish-transcript
|
||||
~> list = [linux bsd macos windows]
|
||||
~> var list = [linux bsd macos windows]
|
||||
~> echo $list
|
||||
[linux bsd macos windows]
|
||||
```
|
||||
|
@ -294,8 +294,8 @@ Since `$args` is a list, we can retrieve the individual elements with
|
|||
`$args[0]`, `$args[1]`, etc.. Let's rewrite our greet-and-bye script, taking the
|
||||
name as an argument. Put this in `greet-and-bye.elv`:
|
||||
|
||||
```
|
||||
name = $args[0]
|
||||
```elvish
|
||||
var name = $args[0]
|
||||
echo Hello, $name!
|
||||
echo Bye, $name!
|
||||
```
|
||||
|
@ -344,7 +344,7 @@ running this command directly, we can first **capture** its output in a
|
|||
variable:
|
||||
|
||||
```elvish-transcript
|
||||
~> os = (uname)
|
||||
~> var os = (uname)
|
||||
~> echo Hello, $E:USER, $os user!
|
||||
Hello, elf, Linux user!
|
||||
```
|
||||
|
|
|
@ -32,7 +32,7 @@ Elvish offers first-class support for data structures such as lists and maps.
|
|||
Here is an example that uses a list:
|
||||
|
||||
```elvish-transcript
|
||||
~> li = [foo bar 'lorem ipsum']
|
||||
~> var li = [foo bar 'lorem ipsum']
|
||||
~> kind-of $li # "kind" is like type
|
||||
▶ list
|
||||
~> count $li # count the number of elements in a list
|
||||
|
@ -51,7 +51,7 @@ capture to recover it:
|
|||
~> fn f {
|
||||
echo [foo bar 'lorem ipsum']
|
||||
}
|
||||
~> li = (f) # (...) is output capture, like $(...) in other shells
|
||||
~> var li = (f) # (...) is output capture, like $(...) in other shells
|
||||
~> kind-of $li
|
||||
▶ string
|
||||
~> count $li # count the number of bytes, since $li is now a string
|
||||
|
@ -68,7 +68,7 @@ Elvish provides a `put` command to output structured values as they are:
|
|||
~> fn f {
|
||||
put [foo bar 'lorem ipsum']
|
||||
}
|
||||
~> li = (f)
|
||||
~> var li = (f)
|
||||
~> kind-of $li
|
||||
▶ list
|
||||
~> count $li
|
||||
|
@ -364,9 +364,9 @@ underlying map, so `m['foo']` is also changed.
|
|||
This is not the case for Elvish:
|
||||
|
||||
```elvish-transcript
|
||||
~> m = [&foo=bar &lorem=ipsum]
|
||||
~> m2 = $m
|
||||
~> m2[foo] = quux
|
||||
~> var m = [&foo=bar &lorem=ipsum]
|
||||
~> var m2 = $m
|
||||
~> set m2[foo] = quux
|
||||
~> put $m[foo]
|
||||
▶ bar
|
||||
```
|
||||
|
@ -413,9 +413,9 @@ assign an element of `$m2`, Elvish turns that into an assignment of `$m2`
|
|||
itself:
|
||||
|
||||
```elvish
|
||||
m2[foo] = quux
|
||||
set m2[foo] = quux
|
||||
# is just syntax sugar for:
|
||||
m2 = (assoc $m2 foo quux)
|
||||
set m2 = (assoc $m2 foo quux)
|
||||
```
|
||||
|
||||
The sort of immutable data structures that support cheap creation of "slight
|
||||
|
|
Loading…
Reference in New Issue
Block a user