mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 14:40:06 +02:00
add iter
module to standard library (#8899)
# Description this pr introduces an `iter` module to the standard library. the module is aimed at extending the filter commands.
This commit is contained in:
50
crates/nu-std/tests/test_iter.nu
Normal file
50
crates/nu-std/tests/test_iter.nu
Normal file
@ -0,0 +1,50 @@
|
||||
use std *
|
||||
|
||||
export def test_iter_find [] {
|
||||
let hastack1 = [1 2 3 4 5 6 7]
|
||||
let hastack2 = [nushell rust shell iter std]
|
||||
let hastack3 = [nu 69 2023-04-20 "std"]
|
||||
|
||||
let res = ($hastack1 | iter find {|it| $it mod 2 == 0})
|
||||
assert equal $res 2
|
||||
|
||||
let res = ($hastack2 | iter find {|it| $it starts-with 's'})
|
||||
assert equal $res 'shell'
|
||||
|
||||
let res = ($hastack2 | iter find {|it| ($it | length) == 50})
|
||||
assert equal $res null
|
||||
|
||||
let res = ($hastack3 | iter find {|it| (it | describe) == filesize})
|
||||
assert equal $res null
|
||||
}
|
||||
|
||||
export def test_iter_intersperse [] {
|
||||
let res = ([1 2 3 4] | iter intersperse 0)
|
||||
assert equal $res [1 0 2 0 3 0 4]
|
||||
|
||||
let res = ([] | iter intersperse x)
|
||||
assert equal $res []
|
||||
|
||||
let res = ([1] | iter intersperse 5)
|
||||
assert equal $res [1]
|
||||
|
||||
let res = ([a b c d e] | iter intersperse 5)
|
||||
assert equal $res [a 5 b 5 c 5 d 5 e]
|
||||
|
||||
let res = (1..4 | iter intersperse 0)
|
||||
assert equal $res [1 0 2 0 3 0 4]
|
||||
|
||||
let res = (4 | iter intersperse 1)
|
||||
assert equal $res [4]
|
||||
}
|
||||
|
||||
export def test_iter_scan [] {
|
||||
let scanned = ([1 2 3] | iter scan 0 {|x, y| $x + $y} -n)
|
||||
assert equal $scanned [1, 3, 6]
|
||||
|
||||
let scanned = ([1 2 3] | iter scan 0 {|x, y| $x + $y})
|
||||
assert equal $scanned [0, 1, 3, 6]
|
||||
|
||||
let scanned = ([a b c d] | iter scan "" {|x, y| [$x, $y] | str join} -n)
|
||||
assert equal $scanned ["a" "ab" "abc" "abcd"]
|
||||
}
|
Reference in New Issue
Block a user