Repository navigation
Riffle: insert a non-list separator only between elements - #2017
Conversation
Fixes #2003. `Riffle[{a}, x]` gave `{a, x}`; WMA gives `{a}`. `Riffle.eval` wrapped a non-list separator as `[sep]` and passed it to `riffle_lists`, which appends a trailing separator when the item and separator lists have the same length. With one element that rule fired and `x` was appended. A non-list separator is now placed only between elements. The list-separator forms still go through `riffle_lists`, so `Riffle[{a}, {x}]` is `{a, x}` and `Riffle[{a, b}, {x, y}]` is `{a, x, b, y}`, as in WMA. Tests: new `Riffle` cases in `test/builtin/list/test_list.py`.
Add one more assert failure message
Co-authored-by: R. Bernstein <rocky@users.noreply.github.com>
tombouctou
left a comment
There was a problem hiding this comment.
The Riffle change looks right to me. I checked these against WMA 15, and the branch gives the same result for all of them: Riffle[{a}, x], Riffle[{a, b}, x], Riffle[{}, x], Riffle[{a}, {x}], Riffle[{a, b}, {x, y}], Riffle[{{1}, {2}}, x], Riffle[{a, b, c}, {x}], Riffle[{a}, {x, y}], Riffle[{a, b}, {}].
But it breaks one doctest, and that is why the build jobs are red: ExportString in mathics/builtin/import_export/importexport.py. The CSV exporter (SystemFiles/Formats/CSV/Export.wl) builds each row with Riffle[#, sep]. A one-field row like {3} used to come out as 3,, and the doctest recorded that trailing comma. With this fix, the output is the same as WMA's, so only the expected output needs updating:
>> ExportString[{{1,2,3,4},{3},{2},{4}}, "CSV"]
= 1,2,3,4
. 3
. 2
. 4
>> ExportString[{1,2,3,4}, "CSV"]
= 1
. 2
. 3
. 4
(WMA also ends the string with a newline, but that is a separate, existing difference.)
A small nit: in test_list.py, the failure message "Issue #2017" should be #2003. 2017 is this PR.
With the doctest updated, LGTM.
|
Thanks @TheNetherWatcher and @tombouctou. LGTM |
Fixes #2003.
Riffle[{a}, x]returned{a, x}; WMA returns{a}.Riffle.evalwrapped a non-list separator as[sep]and passed it toriffle_lists, which appends a trailing separator when the item and separator lists have the same length. For a one-element list that rule fired, soxwas appended.A non-list separator is now placed only between elements. List separators still go through
riffle_listsunchanged, soRiffle[{a}, {x}]is{a, x}andRiffle[{a, b}, {x, y}]is{a, x, b, y}, as in WMA.Tests: added
Rifflecases totest/builtin/list/test_list.py({a},{a, b},{}withx;{a}with{x};{a, b}with{x, y}). The list tests and the existingRiffledoctests pass. Added a CHANGES.md entry.