New "compact" command.

This fixes #1453.
This commit is contained in:
Qi Xiao 2022-08-28 22:57:37 +01:00
parent 181c4dde55
commit acf470f104
3 changed files with 61 additions and 3 deletions

View File

@ -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.

View File

@ -15,8 +15,9 @@ func init() {
"all": all,
"one": one,
"take": take,
"drop": drop,
"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

View File

@ -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),