mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-05 03:17:50 +08:00
parent
181c4dde55
commit
acf470f104
|
@ -45,4 +45,8 @@ and compiled, even if it is not executed:
|
|||
- A new `path:join` command and `path:separator` and `path:list-separator`
|
||||
variables ([#1562](https://b.elv.sh/1562)).
|
||||
|
||||
- A new `runtime:elvish-path` variable ([#1423](https://b.elv.sh/1423)).
|
||||
- A new `runtime:` module that contains paths important for the Elvish runtime
|
||||
([#1385](https://b.elv.sh/1385), [#1423](https://b.elv.sh/1423)).
|
||||
|
||||
- A new `compact` command that replaces consecutive runs of equal values with
|
||||
a single copy, similar to the Unix `uniq` command.
|
||||
|
|
|
@ -17,6 +17,7 @@ func init() {
|
|||
|
||||
"take": take,
|
||||
"drop": drop,
|
||||
"compact": compact,
|
||||
|
||||
"count": count,
|
||||
|
||||
|
@ -240,6 +241,51 @@ func drop(fm *Frame, n int, inputs Inputs) error {
|
|||
return errOut
|
||||
}
|
||||
|
||||
//elvdoc:fn drop
|
||||
//
|
||||
// ```elvish
|
||||
// compact $inputs?
|
||||
// ```
|
||||
//
|
||||
// Replaces consecutive runs of equal values with a single copy. Similar to the
|
||||
// `uniq` command on Unix.
|
||||
//
|
||||
// Examples:
|
||||
//
|
||||
// ```elvish-transcript
|
||||
// ~> put a a b b c | compact
|
||||
// ▶ a
|
||||
// ▶ b
|
||||
// ▶ c
|
||||
// ~> compact [a a b b c]
|
||||
// ▶ a
|
||||
// ▶ b
|
||||
// ▶ c
|
||||
// ~> put a b a | compact
|
||||
// ▶ a
|
||||
// ▶ b
|
||||
// ▶ a
|
||||
// ```
|
||||
|
||||
func compact(fm *Frame, inputs Inputs) error {
|
||||
out := fm.ValueOutput()
|
||||
first := true
|
||||
var errOut error
|
||||
var prev any
|
||||
|
||||
inputs(func(v any) {
|
||||
if errOut != nil {
|
||||
return
|
||||
}
|
||||
if first || !vals.Equal(v, prev) {
|
||||
errOut = out.Put(v)
|
||||
first = false
|
||||
prev = v
|
||||
}
|
||||
})
|
||||
return errOut
|
||||
}
|
||||
|
||||
//elvdoc:fn count
|
||||
//
|
||||
// ```elvish
|
||||
|
|
|
@ -46,6 +46,14 @@ func TestDrop(t *testing.T) {
|
|||
)
|
||||
}
|
||||
|
||||
func TestCompact(t *testing.T) {
|
||||
Test(t,
|
||||
That(`put a a b b c | compact`).Puts("a", "b", "c"),
|
||||
That(`put a b a | compact`).Puts("a", "b", "a"),
|
||||
thatOutputErrorIsBubbled("compact [a a]"),
|
||||
)
|
||||
}
|
||||
|
||||
func TestCount(t *testing.T) {
|
||||
Test(t,
|
||||
That(`range 100 | count`).Puts(100),
|
||||
|
|
Loading…
Reference in New Issue
Block a user