digitalmars.D.learn - Comparing slice to an Array in assert
- madwebness (39/39) Jun 11 I was following the documentation here:
- madwebness (5/10) Jun 11 Correction here, I, of course, meant:
- Richard (Rikki) Andrew Cattermole (4/4) Jun 11 There are two more corrections to make.
- madwebness (52/57) Jun 11 While I do understand what you're saying, I'm not sure I
- Richard (Rikki) Andrew Cattermole (25/88) Jun 11 Yes, that is the second one.
- madwebness (15/17) Jun 11 With the following function definition what the function returns
- madwebness (16/17) Jun 11 Ran the code exactly as you posted. It works and I found the
- madwebness (5/22) Jun 11 Ah, my mistake. With the regular expression adjusted to
- Richard (Rikki) Andrew Cattermole (4/7) Jun 11 All good, happy to help!
I was following the documentation here: http://dlang.org.mirror/spec/arrays.html#slicing and I'm having a problem comparing the returned Slice from a function with an array. Here's the simplified code: ``` import std.stdio; import std.regex; import std.string; import std.array; auto sliceIt(array[string] words_arr) { int i = 1; string word; while(i < words_arr.length) { word = words_arr[i]; if(matchFirst(word, r"^[A-Z]+$")) { break; } i++; } return def_words_arr[1..i]; } unittest { auto words = ["HELLO", "world", "hi", "ENDOFTHERUNWAY"]; auto resulting_arr = sliceIt(["world", "hi"]); assert(resulting_arr == ["world", "hi"]); } ``` This produces a compile-time error: ``` Error: template `std.array.array(Range)(Range r) if (isIterable!Range && !isAutodecodableString!Range && !isInfinite!Range)` is used as a type Failed: ["~/.local/dlang/dmd-2.108.1/freebsd/bin64/dmd", "-debug", "-unittest", "-main", "-v", "-o-", "words.d", "-Idefinition"] ``` I was thinking of maybe assigning correct return type for the function, tried it, but got more errors produced by the compiler. Can someone point me to an obvious mistake or lack of understanding?
Jun 11
On Tuesday, 11 June 2024 at 08:29:06 UTC, madwebness wrote:unittest { auto words = ["HELLO", "world", "hi", "ENDOFTHERUNWAY"]; auto resulting_arr = sliceIt(["world", "hi"]); assert(resulting_arr == ["world", "hi"]); }Correction here, I, of course, meant: ``` auto resulting_arr = sliceIt(words); ```
Jun 11
There are two more corrections to make. Both in sliceIt. Note: array is a function not a type and is for ranges, slices are built in language concept. Associate Arrays (map) use the syntax ``Type[Type]``.
Jun 11
On Tuesday, 11 June 2024 at 08:44:25 UTC, Richard (Rikki) Andrew Cattermole wrote:There are two more corrections to make. Both in sliceIt. Note: array is a function not a type and is for ranges, slices are built in language concept. Associate Arrays (map) use the syntax ``Type[Type]``.While I do understand what you're saying, I'm not sure I understand how to fix the code. 1. Changing function's argument type to `Array(string)` results in the following error: `Error: function declaration without return type.` 2. Adding function type `Array(string) sliceIt(...)` results in the following error: `Error: function declaration without return type.` 3. Finally, changing function's argument type to `string[]`, so function definition looks like this: ``` auto extract_arg_names(string[] def_words_arr){...}; ``` results in, what I presume, is a runtime error: ``` core.exception.AssertError definition/sliceit.d(42): unittest failure ---------------- ??:? _d_unittestp [0x357a31] ??:? void arg.__unittest_L39_C1() [0x302495] ??:? void arg.__modtest() [0x34ba78] ??:? int core.runtime.runModuleUnitTests().__foreachbody6(object.ModuleInfo*) [0x37316a] ??:? int object.ModuleInfo.opApply(scope int delegate(object.ModuleInfo*)).__lambda2(immutable(object.ModuleInfo*)) [0x34dde3] ??:? int rt.minfo.moduleinfos_apply(scope int delegate(immutable(object.ModuleInfo*))).__foreachbody2(ref rt.sections_elf_shared.DSO) [0x380257] ??:? int rt.sections_elf_shared.DSO.opApply(scope int delegate(ref rt.sections_elf_shared.DSO)) [0x382dd1] ??:? int rt.minfo.moduleinfos_apply(scope int delegate(immutable(object.ModuleInfo*))) [0x3801e5] ??:? int object.ModuleInfo.opApply(scope int delegate(object.ModuleInfo*)) [0x34ddb5] ??:? runModuleUnitTests [0x372fa0] ??:? void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int function(char[][])*).runAll() [0x372728] ??:? void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x3726b5] ??:? _d_run_main2 [0x37261e] ??:? _d_run_main [0x3723bf] ??:? main [0x34ba8d] ??:? __libc_start1 [0x82289eaf9] /usr/src/lib/csu/amd64/crt1_s.S:83 _start [0x30213f] ??:? ??? [0xe83fb403007] 1/1 modules FAILED unittests ``` PS. Found more correction in the original code. The variable `def_words_arr` should just be `words_arr`. Was not paying attention when simplifying the code to post here.
Jun 11
On 11/06/2024 9:17 PM, madwebness wrote:On Tuesday, 11 June 2024 at 08:44:25 UTC, Richard (Rikki) Andrew Cattermole wrote:That is correct yes.There are two more corrections to make. Both in sliceIt. Note: array is a function not a type and is for ranges, slices are built in language concept. Associate Arrays (map) use the syntax ``Type[Type]``.While I do understand what you're saying, I'm not sure I understand how to fix the code. 1. Changing function's argument type to `Array(string)` results in the following error: `Error: function declaration without return type.` 2. Adding function type `Array(string) sliceIt(...)` results in the following error: `Error: function declaration without return type.` 3. Finally, changing function's argument type to `string[]`, so function definition looks like this: ``` auto extract_arg_names(string[] def_words_arr){...}; ```results in, what I presume, is a runtime error: ``` core.exception.AssertError definition/sliceit.d(42): unittest failure ---------------- ??:? _d_unittestp [0x357a31] ??:? void arg.__unittest_L39_C1() [0x302495] ??:? void arg.__modtest() [0x34ba78] ??:? int core.runtime.runModuleUnitTests().__foreachbody6(object.ModuleInfo*) [0x37316a] ??:? int object.ModuleInfo.opApply(scope int delegate(object.ModuleInfo*)).__lambda2(immutable(object.ModuleInfo*)) [0x34dde3] ??:? int rt.minfo.moduleinfos_apply(scope int delegate(immutable(object.ModuleInfo*))).__foreachbody2(ref rt.sections_elf_shared.DSO) [0x380257] ??:? int rt.sections_elf_shared.DSO.opApply(scope int delegate(ref rt.sections_elf_shared.DSO)) [0x382dd1] ??:? int rt.minfo.moduleinfos_apply(scope int delegate(immutable(object.ModuleInfo*))) [0x3801e5] ??:? int object.ModuleInfo.opApply(scope int delegate(object.ModuleInfo*)) [0x34ddb5] ??:? runModuleUnitTests [0x372fa0] ??:? void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int function(char[][])*).runAll() [0x372728] ??:? void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x3726b5] ??:? _d_run_main2 [0x37261e] ??:? _d_run_main [0x3723bf] ??:? main [0x34ba8d] ??:? __libc_start1 [0x82289eaf9] /usr/src/lib/csu/amd64/crt1_s.S:83 _start [0x30213f] ??:? ??? [0xe83fb403007] 1/1 modules FAILED unittests ``` PS. Found more correction in the original code. The variable `def_words_arr` should just be `words_arr`. Was not paying attention when simplifying the code to post here.Yes, that is the second one. My version that runs: ```d import std.stdio; import std.regex; import std.string; import std.array; auto sliceIt(string[] words_arr) { int i = 1; string word; while(i < words_arr.length) { word = words_arr[i]; if(matchFirst(word, r"^[A-Z]+$")) { break; } i++; } return words_arr[1..i]; } void main() { auto words = ["HELLO", "world", "hi", "ENDOFTHERUNWAY"]; auto resulting_arr = sliceIt(words); assert(resulting_arr == ["world", "hi"]); } ```
Jun 11
On Tuesday, 11 June 2024 at 09:17:21 UTC, madwebness wrote:While I do understand what you're saying, I'm not sure I understand how to fix the code.With the following function definition what the function returns is correct and the problem is in the unittest code. Note that `writeln()` prints what's expected here program fails only when it gets to `assert()`: ``` auto sliceIt(string[] words_arr) {...}; unittest { auto words = ["HELLO", "world", "hi", "ENDOFTHERUNWAY"]; auto resulting_arr = sliceIt(words); writeln(resulting_arr[0]); // => "world" assert(resulting_arr[0] == "world"); // => ERROR mentioned in (3) in the previous post. } ```
Jun 11
On Tuesday, 11 June 2024 at 09:29:05 UTC, madwebness wrote:My version that runs:Ran the code exactly as you posted. It works and I found the issue. Apparently, if I add =* to the end of the last element, the assertion fails with the error. ``` auto words = ["HELLO", "world", "hi", "ENDOFTHERUNWAY=*"]; ``` results in: ``` core.exception.AssertError ./code.d(23): Assertion failure ---------------- ??:? _d_assertp [0x360904] ??:? _Dmain [0x3073c3] ``` I don't understand why, isn't it supposed to be treated as a string?
Jun 11
On Tuesday, 11 June 2024 at 09:48:36 UTC, madwebness wrote:On Tuesday, 11 June 2024 at 09:29:05 UTC, madwebness wrote:Ah, my mistake. With the regular expression adjusted to `r"^[A-Z]+(=.*)?$"` it works just fine. Thank you very much for running the code for me. All very simple, I'm just new to the language.My version that runs:Ran the code exactly as you posted. It works and I found the issue. Apparently, if I add =* to the end of the last element, the assertion fails with the error. ``` auto words = ["HELLO", "world", "hi", "ENDOFTHERUNWAY=*"]; ``` results in: ``` core.exception.AssertError ./code.d(23): Assertion failure ---------------- ??:? _d_assertp [0x360904] ??:? _Dmain [0x3073c3] ``` I don't understand why, isn't it supposed to be treated as a string?
Jun 11
On 11/06/2024 9:51 PM, madwebness wrote:Ah, my mistake. With the regular expression adjusted to `r"^[A-Z]+(=.*)?$"` it works just fine. Thank you very much for running the code for me. All very simple, I'm just new to the language.All good, happy to help! We also have people on Discord and IRC if either of those mediums would be more helpful.
Jun 11