src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/eval/builtin_fn_stream_test.elvts (about)

     1  ///////
     2  # all #
     3  ///////
     4  
     5  ~> put foo bar | all
     6  ▶ foo
     7  ▶ bar
     8  ~> echo foobar | all
     9  ▶ foobar
    10  ~> all [foo bar]
    11  ▶ foo
    12  ▶ bar
    13  // bubbling output errors
    14  ~> all [foo bar] >&-
    15  Exception: port does not support value output
    16    [tty]:1:1-17: all [foo bar] >&-
    17  
    18  ///////
    19  # one #
    20  ///////
    21  ~> put foo | one
    22  ▶ foo
    23  ~> put | one
    24  Exception: arity mismatch: values must be 1 value, but is 0 values
    25    [tty]:1:7-9: put | one
    26  ~> put foo bar | one
    27  Exception: arity mismatch: values must be 1 value, but is 2 values
    28    [tty]:1:15-17: put foo bar | one
    29  ~> one [foo]
    30  ▶ foo
    31  ~> one []
    32  Exception: arity mismatch: values must be 1 value, but is 0 values
    33    [tty]:1:1-6: one []
    34  ~> one [foo bar]
    35  Exception: arity mismatch: values must be 1 value, but is 2 values
    36    [tty]:1:1-13: one [foo bar]
    37  // bubbling output errors
    38  ~> one [foo] >&-
    39  Exception: port does not support value output
    40    [tty]:1:1-13: one [foo] >&-
    41  
    42  ////////
    43  # take #
    44  ////////
    45  
    46  ~> range 100 | take 2
    47  ▶ (num 0)
    48  ▶ (num 1)
    49  // bubbling output errors
    50  ~> take 1 [foo bar] >&-
    51  Exception: port does not support value output
    52    [tty]:1:1-20: take 1 [foo bar] >&-
    53  
    54  ////////
    55  # drop #
    56  ////////
    57  
    58  ~> range 100 | drop 98
    59  ▶ (num 98)
    60  ▶ (num 99)
    61  // bubbling output errors
    62  ~> drop 1 [foo bar lorem] >&-
    63  Exception: port does not support value output
    64    [tty]:1:1-26: drop 1 [foo bar lorem] >&-
    65  
    66  ///////////
    67  # compact #
    68  ///////////
    69  
    70  ~> put a a b b c | compact
    71  ▶ a
    72  ▶ b
    73  ▶ c
    74  ~> put a b a | compact
    75  ▶ a
    76  ▶ b
    77  ▶ a
    78  // bubbling output errors
    79  ~> compact [a a] >&-
    80  Exception: port does not support value output
    81    [tty]:1:1-17: compact [a a] >&-
    82  
    83  /////////
    84  # count #
    85  /////////
    86  
    87  ~> range 100 | count
    88  ▶ (num 100)
    89  ~> count [(range 100)]
    90  ▶ (num 100)
    91  ~> count 123
    92  ▶ (num 3)
    93  ~> count 1 2 3
    94  Exception: arity mismatch: arguments must be 0 to 1 values, but is 3 values
    95    [tty]:1:1-11: count 1 2 3
    96  ~> count $true
    97  Exception: cannot get length of a bool
    98    [tty]:1:1-11: count $true
    99  
   100  /////////
   101  # order #
   102  /////////
   103  
   104  ## strings ##
   105  ~> put foo bar ipsum | order
   106  ▶ bar
   107  ▶ foo
   108  ▶ ipsum
   109  ~> put foo bar bar | order
   110  ▶ bar
   111  ▶ bar
   112  ▶ foo
   113  ~> put 10 1 5 2 | order
   114  ▶ 1
   115  ▶ 10
   116  ▶ 2
   117  ▶ 5
   118  
   119  ## booleans ##
   120  ~> put $true $false $true | order
   121  ▶ $false
   122  ▶ $true
   123  ▶ $true
   124  ~> put $false $true $false | order
   125  ▶ $false
   126  ▶ $false
   127  ▶ $true
   128  
   129  ## typed numbers ##
   130  // int
   131  ~> put 10 1 1 | each $num~ | order
   132  ▶ (num 1)
   133  ▶ (num 1)
   134  ▶ (num 10)
   135  ~> put 10 1 5 2 -1 | each $num~ | order
   136  ▶ (num -1)
   137  ▶ (num 1)
   138  ▶ (num 2)
   139  ▶ (num 5)
   140  ▶ (num 10)
   141  // int and *big.Int
   142  ~> put 1 100000000000000000000 2 100000000000000000000 | each $num~ | order
   143  ▶ (num 1)
   144  ▶ (num 2)
   145  ▶ (num 100000000000000000000)
   146  ▶ (num 100000000000000000000)
   147  // int and *big.Rat
   148  ~> put 1 2 3/2 3/2 | each $num~ | order
   149  ▶ (num 1)
   150  ▶ (num 3/2)
   151  ▶ (num 3/2)
   152  ▶ (num 2)
   153  // int and float64
   154  ~> put 1 1.5 2 1.5 | each $num~ | order
   155  ▶ (num 1)
   156  ▶ (num 1.5)
   157  ▶ (num 1.5)
   158  ▶ (num 2)
   159  // NaN's are considered smaller than other numbers for ordering
   160  ~> put NaN -1 NaN | each $num~ | order
   161  ▶ (num NaN)
   162  ▶ (num NaN)
   163  ▶ (num -1)
   164  
   165  ## lists ##
   166  ~> put [b] [a] | order
   167  ▶ [a]
   168  ▶ [b]
   169  ~> put [a] [b] [a] | order
   170  ▶ [a]
   171  ▶ [a]
   172  ▶ [b]
   173  ~> put [(num 10)] [(num 2)] | order
   174  ▶ [(num 2)]
   175  ▶ [(num 10)]
   176  ~> put [a b] [b b] [a c] | order
   177  ▶ [a b]
   178  ▶ [a c]
   179  ▶ [b b]
   180  ~> put [a] [] [a (num 2)] [a (num 1)] | order
   181  ▶ []
   182  ▶ [a]
   183  ▶ [a (num 1)]
   184  ▶ [a (num 2)]
   185  
   186  ## &reverse ##
   187  ~> put foo bar ipsum | order &reverse
   188  ▶ ipsum
   189  ▶ foo
   190  ▶ bar
   191  
   192  ## &key ##
   193  ~> put 10 1 5 2 | order &key={|v| num $v }
   194  ▶ 1
   195  ▶ 2
   196  ▶ 5
   197  ▶ 10
   198  
   199  ## &key and &reverse ##
   200  ~> put 10 1 5 2 | order &reverse &key={|v| num $v }
   201  ▶ 10
   202  ▶ 5
   203  ▶ 2
   204  ▶ 1
   205  
   206  ## different types without &total ##
   207  ~> put (num 1) 1 | order
   208  Exception: bad value: inputs to "compare" or "order" must be comparable values, but is uncomparable values
   209    [tty]:1:17-21: put (num 1) 1 | order
   210  ~> put 1 (num 1) | order
   211  Exception: bad value: inputs to "compare" or "order" must be comparable values, but is uncomparable values
   212    [tty]:1:17-21: put 1 (num 1) | order
   213  ~> put 1 (num 1) b | order
   214  Exception: bad value: inputs to "compare" or "order" must be comparable values, but is uncomparable values
   215    [tty]:1:19-23: put 1 (num 1) b | order
   216  ~> put [a] a | order
   217  Exception: bad value: inputs to "compare" or "order" must be comparable values, but is uncomparable values
   218    [tty]:1:13-17: put [a] a | order
   219  ~> put [a] [(num 1)] | order
   220  Exception: bad value: inputs to "compare" or "order" must be comparable values, but is uncomparable values
   221    [tty]:1:21-25: put [a] [(num 1)] | order
   222  
   223  ## different types with &total ##
   224  // &total orders the values into groups of different types, and sorts
   225  // the groups themselves. Test that without assuming the relative order
   226  // between numbers and strings.
   227  ~> put (num 3/2) (num 1) c (num 2) a | order &total | var li = [(all)]
   228     or (eq $li [a c (num 1) (num 3/2) (num 2)]) ^
   229        (eq $li [(num 1) (num 3/2) (num 2) a c])
   230  ▶ $true
   231  // &total keeps the order of unordered values as is.
   232  ~> put [&foo=bar] [&a=b] [&x=y] | order &total
   233  ▶ [&foo=bar]
   234  ▶ [&a=b]
   235  ▶ [&x=y]
   236  
   237  ## &less-than ##
   238  ~> put 1 10 2 5 | order &less-than={|a b| < $a $b }
   239  ▶ 1
   240  ▶ 2
   241  ▶ 5
   242  ▶ 10
   243  
   244  ## &less-than and &key ##
   245  ~> put [a 1] [b 10] [c 2] | order &key={|v| put $v[1]} &less-than=$'<~'
   246  ▶ [a 1]
   247  ▶ [c 2]
   248  ▶ [b 10]
   249  
   250  ## &less-than and &reverse ##
   251  ~> put 1 10 2 5 | order &reverse &less-than={|a b| < $a $b }
   252  ▶ 10
   253  ▶ 5
   254  ▶ 2
   255  ▶ 1
   256  
   257  ## &less-than writing more than one value ##
   258  ~> put 1 10 2 5 | order &less-than={|a b| put $true $false }
   259  Exception: arity mismatch: number of outputs of the &less-than callback must be 1 value, but is 2 values
   260    [tty]:1:16-57: put 1 10 2 5 | order &less-than={|a b| put $true $false }
   261  
   262  ## &less-than writing non-boolean value ##
   263  ~> put 1 10 2 5 | order &less-than={|a b| put x }
   264  Exception: bad value: output of the &less-than callback must be boolean, but is string
   265    [tty]:1:16-46: put 1 10 2 5 | order &less-than={|a b| put x }
   266  
   267  ## &less-than throwing an exception ##
   268  ~> put 1 10 2 5 | order &less-than={|a b| fail bad }
   269  Exception: bad
   270    [tty]:1:40-48: put 1 10 2 5 | order &less-than={|a b| fail bad }
   271    [tty]:1:16-49: put 1 10 2 5 | order &less-than={|a b| fail bad }
   272  
   273  ## all callback options support $nil for default behavior ##
   274  ~> put c b a | order &less-than=$nil &key=$nil
   275  ▶ a
   276  ▶ b
   277  ▶ c
   278  
   279  ## sort is stable ##
   280  // Test stability by pretending that all values but one are equal, and check
   281  // that the order among them has not changed.
   282  ~> put l x o x r x e x m | order &less-than={|a b| eq $a x }
   283  ▶ x
   284  ▶ x
   285  ▶ x
   286  ▶ x
   287  ▶ l
   288  ▶ o
   289  ▶ r
   290  ▶ e
   291  ▶ m
   292  
   293  ## &total and &less-than are mutually exclusive ##
   294  ~> put x | order &total &less-than={|a b| put $true }
   295  Exception: both &total and &less-than specified
   296    [tty]:1:9-50: put x | order &total &less-than={|a b| put $true }
   297  
   298  ## bubbling output errors ##
   299  ~> order [foo] >&-
   300  Exception: port does not support value output
   301    [tty]:1:1-15: order [foo] >&-