Comparing slice to an Array in assert

2024-06-11 Thread madwebness via Digitalmars-d-learn
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?


Re: Comparing slice to an Array in assert

2024-06-11 Thread madwebness via Digitalmars-d-learn

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);
```


Re: Comparing slice to an Array in assert

2024-06-11 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn

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]``.


Re: Comparing slice to an Array in assert

2024-06-11 Thread madwebness via Digitalmars-d-learn
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.


Re: Comparing slice to an Array in assert

2024-06-11 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn



On 11/06/2024 9:17 PM, madwebness wrote:
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){...};
```


That is correct yes.


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"]);
}
```


Re: Comparing slice to an Array in assert

2024-06-11 Thread madwebness via Digitalmars-d-learn

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.

}
```


Re: Comparing slice to an Array in assert

2024-06-11 Thread madwebness via Digitalmars-d-learn

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?


Re: Comparing slice to an Array in assert

2024-06-11 Thread madwebness via Digitalmars-d-learn

On Tuesday, 11 June 2024 at 09:48:36 UTC, madwebness wrote:

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?


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.


Re: Comparing slice to an Array in assert

2024-06-11 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn

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.


Re: How to use D without the GC ?

2024-06-11 Thread matheus via Digitalmars-d-learn

On Tuesday, 11 June 2024 at 13:00:50 UTC, Vinod K Chandran wrote:

...


Similar posts that may help:

https://forum.dlang.org/thread/hryadrwplyezihwag...@forum.dlang.org

https://forum.dlang.org/thread/dblfikgnzqfmmglwd...@forum.dlang.org

Matheus.


Re: How to use D without the GC ?

2024-06-11 Thread Vinod K Chandran via Digitalmars-d-learn

On Tuesday, 11 June 2024 at 13:35:19 UTC, matheus wrote:
On Tuesday, 11 June 2024 at 13:00:50 UTC, Vinod K Chandran 
wrote:

...


Similar posts that may help:

https://forum.dlang.org/thread/hryadrwplyezihwag...@forum.dlang.org

https://forum.dlang.org/thread/dblfikgnzqfmmglwd...@forum.dlang.org

Matheus.

Thank you Matheus, let me check that. :)



Re: How to use D without the GC ?

2024-06-11 Thread Kagamin via Digitalmars-d-learn
1) arena allocator makes memory manageable with occasional cache 
invalidation problem

2) no hashtable no problem
3) error handling depends on your code complexity, but even in 
complex C# code I found exceptions as boolean: you either have an 
exception or you don't

4) I occasionally use CTFE, where `@nogc` is a nuisance
5) polymorphism can be a little quirky


Re: How to use D without the GC ?

2024-06-11 Thread Vinod K Chandran via Digitalmars-d-learn

On Tuesday, 11 June 2024 at 14:59:24 UTC, Kagamin wrote:
1) arena allocator makes memory manageable with occasional 
cache invalidation problem

2) no hashtable no problem
3) error handling depends on your code complexity, but even in 
complex C# code I found exceptions as boolean: you either have 
an exception or you don't

4) I occasionally use CTFE, where `@nogc` is a nuisance
5) polymorphism can be a little quirky


Oh thank you @Kagamin. That's some valuable comments. I will take 
special care.


Re: How to use D without the GC ?

2024-06-11 Thread drug007 via Digitalmars-d-learn

On 11.06.2024 17:59, Kagamin wrote:
1) arena allocator makes memory manageable with occasional cache 
invalidation problem

2) no hashtable no problem


[OT] could you elaborate what problems they cause?

3) error handling depends on your code complexity, but even in complex 
C# code I found exceptions as boolean: you either have an exception or 
you don't

4) I occasionally use CTFE, where `@nogc` is a nuisance
5) polymorphism can be a little quirky




How to assign and compare arrays to SumType?

2024-06-11 Thread confuzzled via Digitalmars-d-learn
Comparison between a Variant and an array is straightforward. How 
does one accomplish the same between a SumType and an array?


```d
import std.variant;
import std.sumtype;
import std.stdio;

struct S
{
SumType!(double[]) data;  // {1}
}

void main()
{
Variant v = [1.7, 2.7, 3.7, 4.7, 5.7];
assert(v == [1.7, 2.7, 3.7, 4.7, 5.7]);

S s;
s.data = [1.7, 2.7, 3.7, 4.7, 5.7]; // {2}
assert(s.data == [1.7, 2.7, 3.7, 4.7, 5.7]);
}
```

Resulting Error:

var.d(17): Error: template \`opEquals\` is not callable using 
argument types `!()(double[])`

/Users/anju/dlang/dmd-2.109.0-beta.1/osx/bin/../../src/phobos/std/sumtype.d(712):
Candidate is: `opEquals(this This, Rhs)(auto ref Rhs rhs)`
 with `This = SumType!(double[]),
  Rhs = double[]`
 must satisfy the following constraint:
`   !is(CommonType!(This, Rhs) == void)`


Also, assuming that {1} read "SumType!(double)[] data;", what 
would be the proper way to accomplish the assignment at {2} and 
the subsequent comparison.


Thanks,
--confuzzled

P.S. Is news.digitalmars.com still operational? I'm unable to 
access it through Thunderbird.


Re: How to use D without the GC ?

2024-06-11 Thread Steven Schveighoffer via Digitalmars-d-learn

On Tuesday, 11 June 2024 at 13:00:50 UTC, Vinod K Chandran wrote:

Hi all,
I am planning to write some D code without GC. But I have no 
prior experience with it. I have experience using manual memory 
management languages. But D has so far been used with GC. So I 
want to know what pitfalls it has and what things I should 
watch out for. Also, I want to know what high level features I 
will be missing.

Thanks in advance.


I could answer the question directly, but it seems others have 
already done so.


I would instead ask the reason for wanting to write D code 
without the GC. In many cases, you can write code without 
*regularly* using the GC (i.e. preallocate, or reuse buffers), 
but still use the GC in the sense that it is there as your 
allocator.


A great example is exceptions. Something that has the code `throw 
new Exception(...)` is going to need the GC in order to build 
that exception. But if your code is written such that this never 
(normally) happens, then you aren't using the GC for that code.


So I would call this kind of style writing code that avoids 
creating garbage. To me, this is the most productive way to 
minimize GC usage, while still allowing one to use D as it was 
intended.


-Steve


Re: How to use D without the GC ?

2024-06-11 Thread Vinod K Chandran via Digitalmars-d-learn
On Tuesday, 11 June 2024 at 16:54:44 UTC, Steven Schveighoffer 
wrote:



I would instead ask the reason for wanting to write D code 
without the GC.


-Steve


Hi Steve,
Two reasons.
1. I am writting a dll to use in Python. So I am assuming that 
manual memory management is better for this project. It will give 
finer control to me.

2. To squeeze out the last bit of performance from D.





Re: How to assign and compare arrays to SumType?

2024-06-11 Thread confuzzled via Digitalmars-d-learn

On Tuesday, 11 June 2024 at 16:41:46 UTC, confuzzled wrote:


Also, assuming that {1} read "SumType!(double)[] data;", what 
would be the proper way to accomplish the assignment at {2} and 
the subsequent comparison.




Not sure how to do solve the fist part of the question yet but I 
was able to get this part done.


s.data = [SumType!double(1.7), SumType!double(2.7), 
SumType!double(3.7), SumType!double(4.7), SumType!double(5.7)];
assert(s.data == [SumType!double(1.7), SumType!double(2.7), 
SumType!double(3.7), SumType!double(4.7), SumType!double(5.7)]);


Wander if there is a more succinct way to do this?



P.S. Is news.digitalmars.com still operational? I'm unable to 
access it through Thunderbird.





Re: How to assign and compare arrays to SumType?

2024-06-11 Thread confuzzled via Digitalmars-d-learn

On Tuesday, 11 June 2024 at 16:41:46 UTC, confuzzled wrote:
Comparison between a Variant and an array is straightforward. 
How does one accomplish the same between a SumType and an array?




Okay, this is what I came up with. Just a sanity check please. 
Did I do this correctly? Is there something I'm overlooking?


```d
import std.variant;
import std.sumtype;
import std.traits: isArray;

struct S
{
SumType!(double[]) data;
bool opEquals(T)(auto ref const T s) const
if (isArray!T)
{
return data == typeof(data)(s);
}
void opAssign(T)(T value)
if (isArray!T)
{
data = typeof(data)(value);
}
this(T)(T value)
if (isArray!T)
{
opAssign(value);
}
}

void main()
{
Variant v = [1.7, 2.7, 3.7, 4.7, 5.7];
assert(v == [1.7, 2.7, 3.7, 4.7, 5.7]);

S s = [1.7, 2.7, 3.7, 4.7, 5.7];
assert(s == [1.7, 2.7, 3.7, 4.7, 5.7]);
}
```

Thanks,
--confuzzled




Re: How to assign and compare arrays to SumType?

2024-06-11 Thread Anonymouse via Digitalmars-d-learn

On Tuesday, 11 June 2024 at 18:26:50 UTC, confuzzled wrote:

On Tuesday, 11 June 2024 at 16:41:46 UTC, confuzzled wrote:
Comparison between a Variant and an array is straightforward. 
How does one accomplish the same between a SumType and an 
array?




Okay, this is what I came up with. Just a sanity check please. 
Did I do this correctly? Is there something I'm overlooking?


It's enough to just make the whole array another 
`SumType!(double[])`.


```d
void main()
{
Variant v = [1.7, 2.7, 3.7, 4.7, 5.7];
assert(v == [1.7, 2.7, 3.7, 4.7, 5.7]);

S s;
s.data = [1.7, 2.7, 3.7, 4.7, 5.7]; // {2}
assert(s.data == SumType!(double[])([1.7, 2.7, 3.7, 4.7, 
5.7]));

}
```


Or better yet, to avoid redundantly spelling out the type;

```d
assert(s.data == typeof(s.data)([1.7, 2.7, 3.7, 4.7, 5.7]));
```


Re: How to assign and compare arrays to SumType?

2024-06-11 Thread Steven Schveighoffer via Digitalmars-d-learn

On Tuesday, 11 June 2024 at 16:41:46 UTC, confuzzled wrote:
Comparison between a Variant and an array is straightforward. 
How does one accomplish the same between a SumType and an array?


```d
import std.variant;
import std.sumtype;
import std.stdio;

struct S
{
SumType!(double[]) data;  // {1}
}

void main()
{
Variant v = [1.7, 2.7, 3.7, 4.7, 5.7];
assert(v == [1.7, 2.7, 3.7, 4.7, 5.7]);

S s;
s.data = [1.7, 2.7, 3.7, 4.7, 5.7]; // {2}
assert(s.data == [1.7, 2.7, 3.7, 4.7, 5.7]);
}
```

Resulting Error:

var.d(17): Error: template \`opEquals\` is not callable using 
argument types `!()(double[])`

/Users/anju/dlang/dmd-2.109.0-beta.1/osx/bin/../../src/phobos/std/sumtype.d(712):
Candidate is: `opEquals(this This, Rhs)(auto ref Rhs rhs)`
 with `This = SumType!(double[]),
  Rhs = double[]`
 must satisfy the following constraint:
`   !is(CommonType!(This, Rhs) == void)`


Also, assuming that {1} read "SumType!(double)[] data;", what 
would be the proper way to accomplish the assignment at {2} and 
the subsequent comparison.


`SumType` requires you unwrap to the correct type in order to use 
it.


The function that unwraps a `SumType` is called `match`. The 
`match` function accepts a sumtype and a list of processors. Each 
processor is tried in order, based on the actual stored type, and 
the first one that compiles will be used.


Your `SumType` has only one type, but just to illustrate how it 
would work:


```d
assert(s.data.match!((double[] darr) => darr == [1.7, 2.7, 3.7, 
4.7, 5.7]));

```

What this is doing is, if the actual type is a `double[]`, then 
call the first function, and return it's value (a boolean).


Because there are no other types, this will work. If you have 
more than one type, your list of lambdas must be exhaustive. That 
is, at least one of the functions must match for each type. And 
also, functions that won't ever be used are rejected.


Let's see how it works if you want to both check that the value 
is a `double[]` and that it matches your value:


```d
SumType!(double[], int) s = [1.7, 2.7, 3.7, 4.7, 5.7];
assert(s.data.match!(
(double[] darr) => darr == [1.7, 2.7, 3.7, 4.7, 5.7]),
_ => false // any other type
)
);
s = 5; // switch to int
assert(!s.data.match!(
(double[] darr) => darr == [1.7, 2.7, 3.7, 4.7, 5.7]),
_ => false // any other type
)
);
```

`SumTypes` take some getting used to. It can get pretty verbose 
to use `match`.


-Steve


Re: How to assign and compare arrays to SumType?

2024-06-11 Thread Steven Schveighoffer via Digitalmars-d-learn
On Tuesday, 11 June 2024 at 22:54:50 UTC, Steven Schveighoffer 
wrote:


Let's see how it works if you want to both check that the value 
is a `double[]` and that it matches your value:


```d
SumType!(double[], int) s = [1.7, 2.7, 3.7, 4.7, 5.7];
assert(s.data.match!(
```


This should be `s.match!(...`, I just was copy-pasting.

-Steve


Re: How to use D without the GC ?

2024-06-11 Thread monkyyy via Digitalmars-d-learn

On Tuesday, 11 June 2024 at 17:15:07 UTC, Vinod K Chandran wrote:
On Tuesday, 11 June 2024 at 16:54:44 UTC, Steven Schveighoffer 
wrote:



I would instead ask the reason for wanting to write D code 
without the GC.


-Steve


Hi Steve,
Two reasons.
1. I am writting a dll to use in Python. So I am assuming that 
manual memory management is better for this project. It will 
give finer control to me.

2. To squeeze out the last bit of performance from D.


rather then worring about the gc, just have 95% of data on the 
stack