Re: BigInt.toString

2022-08-03 Thread Ruby The Roobster via Digitalmars-d-learn

On Thursday, 4 August 2022 at 01:32:15 UTC, Salih Dincer wrote:


I guess I wrote the following anything like that you want.

```d
void main()
{
  import std.bigint,
 std.string : representation;

  BigInt i = 1001;
  auto val = i.to!(dchar[]);
  assert(val.representation == [49, 48, 48, 49]);
}
```

Is that what you want?

SDB@79


Not exactly.  Anyways, it has already been solved.


Re: BigInt.toString

2022-08-03 Thread Ruby The Roobster via Digitalmars-d-learn

On Thursday, 4 August 2022 at 01:05:31 UTC, H. S. Teoh wrote:


Don't call .toString directly. Instead, use std.format.format:

```d
import std;
void main() {
auto x = BigInt("123123123123123123123123123123123123");
	string s = format("%s", x); // this gives you the string 
representation

writeln(s); // prints "123123123123123123123123123123123123"

// If you need to convert to dchar[]:
dstring ds = s.to!(dchar[]);

	// Or if you want a direct formatting into dchar[] without 
going

// through a string intermediate:
auto app = appender!(dchar[]);
app.formattedWrite("%s", x");
dchar[] dcharArray = app.data; // now you have your dchar[]
}
```

Hope this is clear.


T


Thank you.  This worked.


Re: BigInt.toString

2022-08-03 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 4 August 2022 at 00:45:44 UTC, Ruby The Roobster 
wrote:
How exactly can one store the string representation of a 
BigInt?  The seemingly obvious:

dchar[] is necessary for my project.
Assume that val is a BigInt with
a value set earlier:
```d
val.toString(ret, "%d");
```
doesn't work.


I guess I wrote the following anything like that you want.

```d
void main()
{
  import std.bigint,
 std.string : representation;

  BigInt i = 1001;
  auto val = i.to!(dchar[]);
  assert(val.representation == [49, 48, 48, 49]);
}
```

Is that what you want?

SDB@79


Re: BigInt.toString

2022-08-03 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Aug 04, 2022 at 12:45:44AM +, Ruby The Roobster via 
Digitalmars-d-learn wrote:
> How exactly can one store the string representation of a BigInt?  The
> seemingly obvious
> 
> ```d
> //...
> dchar[] ret; //dchar[] is necessary for my project
> //Assume that val is a BigInt with a value set earlier:
> val.toString(ret, "%d");
> //...
> ```
> 
> doesn't work.  I am using x86_64 windows with -m64, and v2.100.1(LDC2
> 1.30.0).

Don't call .toString directly. Instead, use std.format.format:

```d
import std;
void main() {
auto x = BigInt("123123123123123123123123123123123123");
string s = format("%s", x); // this gives you the string representation
writeln(s); // prints "123123123123123123123123123123123123"

// If you need to convert to dchar[]:
dstring ds = s.to!(dchar[]);

// Or if you want a direct formatting into dchar[] without going
// through a string intermediate:
auto app = appender!(dchar[]);
app.formattedWrite("%s", x");
dchar[] dcharArray = app.data; // now you have your dchar[]
}
```

Hope this is clear.


T

-- 
Which is worse: ignorance or apathy? Who knows? Who cares? -- Erich Schubert


Re: MMFile usage

2022-08-03 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Aug 04, 2022 at 12:35:55AM +, Alex Burton via Digitalmars-d-learn 
wrote:
> Hi,
> I want to use MmFile to manage memory mapped files. I want multiple
> processes to be able to read in many files using the virtual memory
> system to page them in and reuse the memory.
> 
> If I read the contents of the file, I can cast it to immutable and
> then I get segfaults when accessing the data.

You need to keep the MmFile instance around as long as you're using the
memory, since it will unmap the memory when the GC collects it.


> Or I have to .dup the data, causing memory allocation in addition to
> the paging to get the data in the first place.

That would defeat the purpose of using MmFile in the first place.


> Is this special memory not managed by the GC ? Do I need to keep all
> the MmFile references around to avoid segfaults ?
[...]

Memory-mapped memory is not managed by the GC; it's managed by the OS.
The MmFile class, however, will unmap the memory upon destruction, so
you need to keep at least one reference to it live throughout the course
of your threads accessing the memory.  Otherwise you will get segfaults
after the MmFile is collected, because then it will unmap the memory and
all your references will become invalid.

Sharing the memory among threads can be done by just passing the memory
reference to the threads. It will be up to you, however, to manage
synchronization of access if you're planning to write to the memory. If
not, you can probably just cast the memory reference to immutable and
pass that around to threads.  Do NOT cast the MmFile object itself to
immutable, though. I'm almost certain that'll break it because it
probably needs to keep internal state in order to manage the memory
mapping with the OS.  Only cast the slice returned by Mmfile.opSlice,
not the MmFile object itself.


T

-- 
Why is it that all of the instruments seeking intelligent life in the universe 
are pointed away from Earth? -- Michael Beibl


BigInt.toString

2022-08-03 Thread Ruby The Roobster via Digitalmars-d-learn
How exactly can one store the string representation of a BigInt?  
The seemingly obvious


```d
//...
dchar[] ret; //dchar[] is necessary for my project
//Assume that val is a BigInt with a value set earlier:
val.toString(ret, "%d");
//...
```

doesn't work.  I am using x86_64 windows with -m64, and 
v2.100.1(LDC2 1.30.0).


MMFile usage

2022-08-03 Thread Alex Burton via Digitalmars-d-learn

Hi,
I want to use MmFile to manage memory mapped files. I want 
multiple processes to be able to read in many files using the 
virtual memory system to page them in and reuse the memory.


If I read the contents of the file, I can cast it to immutable 
and then I get segfaults when accessing the data.


Or I have to .dup the data, causing memory allocation in addition 
to the paging to get the data in the first place.


Is this special memory not managed by the GC ? Do I need to keep 
all the MmFile references around to avoid segfaults ?

Alex



Re: Obsecure problem 2

2022-08-03 Thread jfondren via Digitalmars-d-learn

On Wednesday, 3 August 2022 at 19:11:51 UTC, pascal111 wrote:

On Wednesday, 3 August 2022 at 18:53:35 UTC, jfondren wrote:

On Wednesday, 3 August 2022 at 18:33:37 UTC, pascal111 wrote:

I changed it to "x=notfunny(x);" and has the same result.


Now you are changing the value of the temporary loop variable 
that is still immediately discarded afterwards.


You should return a new range that has the values you want, 
not try to mutate the range you're given.


You should provide a code instead of describing my mistakes!!


I agree to the extent that I wanted to do so, and might still 
have if a delivery hadn't interrupted by post. But one way to 
encourage others to put in more effort is put in effort yourself. 
Here you have your first post, where you didn't use 'preview' to 
check that your formatting worked, or review when linked Markdown 
guide to see what the problem was when it didn't work. In that 
post you also didn't describe what the problem was or ask any 
question, or say what you wanted to accomplish. It wasn't until 
your second post that it was even clear to me that you wanted to 
reimplement `map`. I thought the addition was a placeholder. The 
thread title is also a placeholder title for a thread.


In your second and third posts, it didn't come across at all that 
you'd put much thought into your questions before asking them. 
The second post--just look at your code. There's a function call 
and data going nowhere. That should be pretty easy to see. For 
the third post, if you thought that 'foreach' aliased the members 
of a range to the degree that simple assignment would update the 
range, you could test that thought in an instant:


```
$ rdmd --eval 'auto a = [1, 2, 3]; foreach (n; a) n = 0; 
writeln(a)'

[1, 2, 3]
```

It seemed to me that with these quickly asked questions that 
you're treating the thread like an ongoing interpersonal dialogue 
where questions are cheap. That's fine, really. I didn't mind 
that you didn't put in a lot of effort because it also didn't 
require a lot of effort to help you. But if you're going to 
complain about it, at least put the effort into asking for what 
you want at the outset.


The last thing that discouraged me from writing a full example is 
that I wasn't sure that I'd really be helping you. If you're 
trying to do this, don't you have a range tutorial open? That 
should already be pretty well-written already. Now that I've 
helped you with some incidental problems you should get back to 
the tutorial and see where you got things mixed up. My guess was 
that you didn't realize you should be returning a new range, and 
not trying to modify the range you were given.


If you don't have a tutorial open, the first link at 
https://dlang.org/phobos/std_range.html is pretty good: 
http://ddili.org/ders/d.en/ranges.html You won't find the exact 
thing you want to do, but if you put that aside and thoroughly go 
through that chapter, the only way you won't be able to do what 
you want to do is if you get caught up again by some other more 
fundamental issue with D. The worst likely outcome is that you'll 
get something that works but that isn't a great way to do it. You 
should post that code here for feedback.


Re: Obsecure problem 2

2022-08-03 Thread Ruby The Roobster via Digitalmars-d-learn

On Wednesday, 3 August 2022 at 19:11:51 UTC, pascal111 wrote:

On Wednesday, 3 August 2022 at 18:53:35 UTC, jfondren wrote:

On Wednesday, 3 August 2022 at 18:33:37 UTC, pascal111 wrote:

I changed it to "x=notfunny(x);" and has the same result.


Now you are changing the value of the temporary loop variable 
that is still immediately discarded afterwards.


You should return a new range that has the values you want, 
not try to mutate the range you're given.


You should provide a code instead of describing my mistakes!!


No need for a code.  See, there is a keyword called `ref`, that 
can be used both in function parameters and in foreach loops, and 
it is the equivalent of a pointer to the type specified, but it 
is automatically dereferenced. As arrays are themselves reference 
types, you don't need to change the parameter from `Range range` 
to `ref Range range`.


On the other hand, `int` in it of itself isn't a reference type, 
meaning that to modify the actual value, you need to change the 
foreach loop to be:


```d
foreach(ref x; range)
x = notfunny(x);
```

Proof:  I tested it, and it works.


Re: Obsecure problem 2

2022-08-03 Thread pascal111 via Digitalmars-d-learn

On Wednesday, 3 August 2022 at 18:53:35 UTC, jfondren wrote:

On Wednesday, 3 August 2022 at 18:33:37 UTC, pascal111 wrote:

I changed it to "x=notfunny(x);" and has the same result.


Now you are changing the value of the temporary loop variable 
that is still immediately discarded afterwards.


You should return a new range that has the values you want, not 
try to mutate the range you're given.


You should provide a code instead of describing my mistakes!!


Re: Obsecure problem 2

2022-08-03 Thread jfondren via Digitalmars-d-learn

On Wednesday, 3 August 2022 at 18:33:37 UTC, pascal111 wrote:

I changed it to "x=notfunny(x);" and has the same result.


Now you are changing the value of the temporary loop variable 
that is still immediately discarded afterwards.


You should return a new range that has the values you want, not 
try to mutate the range you're given.


Re: Obsecure problem 2

2022-08-03 Thread pascal111 via Digitalmars-d-learn

On Wednesday, 3 August 2022 at 18:25:50 UTC, jfondren wrote:

On Wednesday, 3 August 2022 at 17:33:40 UTC, pascal111 wrote:

On Wednesday, 3 August 2022 at 17:09:11 UTC, jfondren wrote:

On Wednesday, 3 August 2022 at 16:59:53 UTC, pascal111 wrote:
I tried to make a template that receive lambda expression to 
apply it on a given range the user specifies, but I found 
non-understood problem:


Compare with:

```D
auto foo(Range)(Range range) { // remove isInputRange!T test
...
 lolo.foo!(a=>a+2); // call foo!(f)() rather than 
foo!()(f)

```


Ok! the program ran and gives "[12, 66, 654, -98, 54]" without 
adding "2" as I requested!! why?!


You call notfunny(x) in a loop, discarding the result of the 
addition.


I changed it to "x=notfunny(x);" and has the same result.


Re: Obsecure problem 2

2022-08-03 Thread jfondren via Digitalmars-d-learn

On Wednesday, 3 August 2022 at 17:33:40 UTC, pascal111 wrote:

On Wednesday, 3 August 2022 at 17:09:11 UTC, jfondren wrote:

On Wednesday, 3 August 2022 at 16:59:53 UTC, pascal111 wrote:
I tried to make a template that receive lambda expression to 
apply it on a given range the user specifies, but I found 
non-understood problem:


Compare with:

```D
auto foo(Range)(Range range) { // remove isInputRange!T test
...
 lolo.foo!(a=>a+2); // call foo!(f)() rather than 
foo!()(f)

```


Ok! the program ran and gives "[12, 66, 654, -98, 54]" without 
adding "2" as I requested!! why?!


You call notfunny(x) in a loop, discarding the result of the 
addition.


Re: Obsecure problem 2

2022-08-03 Thread pascal111 via Digitalmars-d-learn

On Wednesday, 3 August 2022 at 17:09:11 UTC, jfondren wrote:

On Wednesday, 3 August 2022 at 16:59:53 UTC, pascal111 wrote:
I tried to make a template that receive lambda expression to 
apply it on a given range the user specifies, but I found 
non-understood problem:


Compare with:

```D
auto foo(Range)(Range range) { // remove isInputRange!T test
...
 lolo.foo!(a=>a+2); // call foo!(f)() rather than 
foo!()(f)

```


Ok! the program ran and gives "[12, 66, 654, -98, 54]" without 
adding "2" as I requested!! why?!


Re: Obsecure problem 2

2022-08-03 Thread jfondren via Digitalmars-d-learn

On Wednesday, 3 August 2022 at 16:59:53 UTC, pascal111 wrote:
I tried to make a template that receive lambda expression to 
apply it on a given range the user specifies, but I found 
non-understood problem:


Compare with:

```D
auto foo(Range)(Range range) { // remove isInputRange!T test
...
 lolo.foo!(a=>a+2); // call foo!(f)() rather than 
foo!()(f)

```



Obsecure problem 2

2022-08-03 Thread pascal111 via Digitalmars-d-learn
I tried to make a template that receive lambda expression to 
apply it on a given range the user specifies, but I found 
non-understood problem:



'''D

module main;

import std.stdio;
import std.functional;


template foo(alias predicate)
if (is(typeof(unaryFun!predicate)))
{
alias notfunny=unaryFun!predicate;

auto foo(Range)(Range range) if (isInputRange!(Unqual!Range)){

foreach(x; range)
notfunny(x);

return range;}


}


int main()
{

int[] lolo = [12, 66, 654, -98, 54];

lolo.foo(a=>a+2);

lolo.writeln;

return 0;
}
'''


Re: Choosing the correct compiler version

2022-08-03 Thread Alain De Vos via Digitalmars-d-learn

I don't know if this helps but i have to do,
export CC=/usr/local/bin/gcc
with ldc2.
I don't know why it works with gcc but not with llvm/clang.



Re: How to cast away shared?

2022-08-03 Thread Ruby The Roobster via Digitalmars-d-learn

On Wednesday, 3 August 2022 at 13:00:05 UTC, Paul Backus wrote:
On Wednesday, 3 August 2022 at 12:50:17 UTC, Ruby The Roobster 
wrote:

Any way to 'cast away' shared for an unknown type T?


There's actually an `Unshared` template for this in 
`std.traits`, but for some reason it's `private`, so you can't 
use it directly. Fortunately, it's only two lines:


```d
alias Unshared(T) = T;
alias Unshared(T: shared U, U) = U;
```

Once you have this, you can write `cast(Unshared!T)`.


Thank you.  This really helped.


Re: How to cast away shared?

2022-08-03 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 3 August 2022 at 12:50:17 UTC, Ruby The Roobster 
wrote:

Any way to 'cast away' shared for an unknown type T?


There's actually an `Unshared` template for this in `std.traits`, 
but for some reason it's `private`, so you can't use it directly. 
Fortunately, it's only two lines:


```d
alias Unshared(T) = T;
alias Unshared(T: shared U, U) = U;
```

Once you have this, you can write `cast(Unshared!T)`.


How to cast away shared?

2022-08-03 Thread Ruby The Roobster via Digitalmars-d-learn

Any way to 'cast away' shared for an unknown type T?


Re: How to find all modules in a package?

2022-08-03 Thread frame via Digitalmars-d-learn

On Wednesday, 3 August 2022 at 03:36:55 UTC, Domain wrote:
I want to find out all public functions in all modules in a 
package. Can I do that at compile time?


You can do something like that:

```d
static foreach (sym; __traits(allMembers, mixin("std.string")))
{
pragma(msg, sym.stringof);
}
```

Then you would have to check if `sym` is a template or function 
or something else.


Re: How to find all modules in a package?

2022-08-03 Thread Adam D Ruppe via Digitalmars-d-learn

On Wednesday, 3 August 2022 at 03:36:55 UTC, Domain wrote:
I want to find out all public functions in all modules in a 
package. Can I do that at compile time?


No, D packages are not closed; anyone can add new modules to them 
at any time.


Re: How to find all modules in a package?

2022-08-03 Thread Piotr Mitana via Digitalmars-d-learn

On Wednesday, 3 August 2022 at 03:36:55 UTC, Domain wrote:
I want to find out all public functions in all modules in a 
package. Can I do that at compile time?


I think it's not possible.


Re: Request assistance resolving linker error: Undefined symbol(s) for architecture x86_64

2022-08-03 Thread anonymouse via Digitalmars-d-learn

On Wednesday, 3 August 2022 at 09:39:36 UTC, ryuukk_ wrote:

Does adding ```-m64``` work


I'm using macOS so I don't think that applies. But no, it doesn't 
do anything for me.


Thanks,
--anonymouse


Re: Request assistance resolving linker error: Undefined symbol(s) for architecture x86_64

2022-08-03 Thread ryuukk_ via Digitalmars-d-learn

Does adding ```-m64``` work


Re: A look inside "filter" function defintion

2022-08-03 Thread novice2 via Digitalmars-d-learn

leyts try very rough simlified concept:

template itself - is compile-time program (parameterizable), it 
can generate some code for you.


template instantiation (like "calling") with "!" - instruct 
compiler to "start this compile-time program here with this 
parameters".