Re: Mimicking a shell

2019-12-29 Thread angel via Digitalmars-d-learn

On Sunday, 29 December 2019 at 17:03:14 UTC, Jan wrote:

Hi,

Is there a way to forward all input and output from a shell? 
This implies that e.g. pressing the left arrow on the keyboard 
is immediately being forwarded to the shell and that the output 
from a shell would be *exactly* the same as output from my D 
program (that would include the prompt and VGA colouring).


Kind regards,
Jan


IMHO, this is kinda what you need:
https://github.com/greenduck/shell-tunnel/blob/master/shell-tunnel.c

Sorry, this code is in C, but converting it to D would be fairly 
straight forward.


Re: What is the point of a synchronized lock on a single return statement?

2019-11-25 Thread angel via Digitalmars-d-learn
On Monday, 25 November 2019 at 08:22:17 UTC, Andrej Mitrovic 
wrote:
From: 
https://github.com/dlang/phobos/blob/10b9174ddcadac52f6a1ea532deab3310d3a8c03/std/concurrency.d#L1913-L1916:


-
///
final @property bool isClosed() @safe @nogc pure
{
synchronized (m_lock)
{
return m_closed;
}
}
-

I don't understand the purpose of this lock. The lock will be 
released as soon as the function returns, and it returns a copy 
of a boolean anyway. Am I missing something here?


I think this code can be rewritten as
---
final @property bool isClosed() @safe @nogc pure
{
bool ret;

synchronized (m_lock)
{
ret = m_closed;
}

return ret;
}
---

Normally, if the memory location of m_closed is aligned, the 
assignment to 'ret' should be atomic, however if you cannot make 
assumptions about alignment, the access should be protected.


Re: gtkDcoding Facelift

2019-06-23 Thread angel via Digitalmars-d-learn

On Sunday, 23 June 2019 at 10:55:52 UTC, Ron Tarrant wrote:
Stage 1 is now complete. Blog entries are color-associated in 
an effort to make things more visual. Each topic also has its 
own avatar. Points to anyone who can figure out why each avatar 
is associated with its topic.


https://gtkdcoding.com/


Great work,
thank you !


Re: Singleton in Action?

2019-02-02 Thread angel via Digitalmars-d-learn

On Saturday, 2 February 2019 at 19:23:58 UTC, Ron Tarrant wrote:
Thanks for the replies, fellow programmers. (generic, unisex, 
PC, and all-encompassing)


If I could trouble someone for a complete working example so I 
have something to study, that would be excellent.



I think that's what you really need:
[1] https://davesdprogramming.wordpress.com/
[2] https://www.youtube.com/watch?v=ZHmIAdlNtiM



Re: Reverse and sort array elements

2018-12-18 Thread angel via Digitalmars-d-learn

On Tuesday, 18 December 2018 at 12:07:37 UTC, Andrey wrote:

Hi,
Have array:

enum array = ["qwerty", "a", "baz"];

Need to reverse and sort array elements to get this result:

[a, ytrewq, zab]

Did this:

enum result = array.map!(value => value.retro()).sort();

Got:
Error: template std.algorithm.sorting.sort cannot deduce 
function from argument types !()(MapResult!(__lambda1, 
string[])), candidates are:
/usr/include/dmd/phobos/std/algorithm/sorting.d(1849,1):
std.algorithm.sorting.sort(alias less = "a < b", SwapStrategy 
ss = SwapStrategy.unstable, Range)(Range r) if ((ss == 
SwapStrategy.unstable && (hasSwappableElements!Range || 
hasAssignableElements!Range) || ss != SwapStrategy.unstable && 
hasAssignableElements!Range) && isRandomAccessRange!Range && 
hasSlicing!Range && hasLength!Range)


How to solve the problem?


Did you try this ?

  import std.stdio;
  import std.algorithm;
  import std.range;
  import std.conv;

  void main()
  {
enum input = ["qwerty", "a", "baz"];
  	enum output = input.map!(value => 
value.retro().to!string()).array.sort();


writeln(input);
writeln(output);
  }



Re: How to list all process directories under /proc/

2017-09-22 Thread angel via Digitalmars-d-learn

On Sunday, 17 September 2017 at 08:15:58 UTC, Ky-Anh Huynh wrote:

Hi,

I want to list all processes by scanning /proc/. The following 
code doesn't work


[code]
  foreach (string fstatm; dirEntries("/proc/", "[0-9]*", 
SpanMode.shallow)) {

writefln("pid %s", fstatm);
  }
[/code]

as it only list a few entries before exiting

[code]
pid /proc/9
pid /proc/935
pid /proc/9146
pid /proc/9149
pid /proc/9150
pid /proc/9151
pid /proc/9756
pid /proc/9759
pid /proc/9760
pid /proc/9761
[/code]

I don't want to use `SpanMode.depth` or `SpanMode.breadth` 
because it will scan so deeply and there would be a permission 
problem.


Any ideas?

Thanks a lot



Are you familiar with libprocps ?
Maybe you had better make use of this library, or, at least, peek 
into its code, for reference.


Re: real simple delegate question.

2017-08-20 Thread angel via Digitalmars-d-learn

On Saturday, 19 August 2017 at 18:33:37 UTC, WhatMeWorry wrote:

On Friday, 18 August 2017 at 20:39:38 UTC, angel wrote:

On Friday, 18 August 2017 at 02:38:15 UTC, WhatMeForget wrote:

[...]


This actually appears correct ...
The 1-st example:
Each call to makeCalculator() increments a static (i.e. shared 
among all makeCalculator() instances) variable - context.

In addition, makeCalculator() generates a random variable.
Whereas the delegate merely captures these variables, and the 
displayed results reflect this.


The 2-nd example:
There is a single call to makeCalculator().
After this call, context == 1, randy == _apparently 2_.
Now the delegate, as has already been said, merely captures 
these values, so consecutive calls do not change the result.


Thanks. So,
auto calculator = makeCalculator();
is the actual call of the delegate? "Delegate is function 
pointer with context"

But what is
...calculator(0));

Or maybe another approach would be to ask, what type is the 
compiler replacing auto with.


No !
The actual call to the delegate is calculator(0).
But this delegate does not induce change on its context 
variables, so it is expectable that consecutive calls to 
calculator(0) produce the same results, isn't it ?
makeCalculator(), while not a delegate, also has a context 
variable - "static int context" - this is an "old-school" context 
variable implemented by the means of static variable.
Consecutive calls to makeCalculator() return delegates having 
different contexts, so each call to calculator(0) produces 
different results.


Re: real simple delegate question.

2017-08-18 Thread angel via Digitalmars-d-learn

On Friday, 18 August 2017 at 02:38:15 UTC, WhatMeForget wrote:


Can someone explain what is the difference between the two? 
Thanks.


module gates;
import std.stdio;
import std.random;

alias Calculator = int delegate(int);

Calculator makeCalculator()
{
static int context = 0;
int randy = uniform(1, 7);
context++;
writeln("context = ", context);
writeln("randy = ", randy);
return value => context + randy + value;
}

void main()
{
for (int i = 0; i < 3; i++)
{
auto calculator = makeCalculator();
writeln("The result of the calculation: ", 
calculator(0));

}
}
returns:
context = 1
randy = 5
The result of the calculation: 6
context = 2
randy = 2
The result of the calculation: 4
context = 3
randy = 6
The result of the calculation: 9

while the following

void main()
{
auto calculator = makeCalculator();  // thought just one 
would work

for (int i = 0; i < 3; i++)
{
writeln("The result of the calculation: ", 
calculator(0));

}
}
returns:
The result of the calculation: 3
The result of the calculation: 3
The result of the calculation: 3


This actually appears correct ...
The 1-st example:
Each call to makeCalculator() increments a static (i.e. shared 
among all makeCalculator() instances) variable - context.

In addition, makeCalculator() generates a random variable.
Whereas the delegate merely captures these variables, and the 
displayed results reflect this.


The 2-nd example:
There is a single call to makeCalculator().
After this call, context == 1, randy == _apparently 2_.
Now the delegate, as has already been said, merely captures these 
values, so consecutive calls do not change the result.


Re: Create class on stack

2017-08-05 Thread angel via Digitalmars-d-learn

On Saturday, 5 August 2017 at 17:08:32 UTC, Johnson Jones wrote:
using gtk, it has a type called value. One has to use it to get 
the value of stuff but it is a class. Once it is used, one 
doesn't need it.


Ideally I'd like to treat it as a struct since I'm using it in 
a delegate I would like to minimize unnecessary allocations. Is 
there any way to get D to allocate a class on the stack like a 
local struct?


Emplace ?
https://dlang.org/phobos/std_conv.html#emplace



Re: Factory using an alias template parameter to set a member of the new tool ?

2017-02-09 Thread angel via Digitalmars-d-learn

On Thursday, 9 February 2017 at 14:39:41 UTC, angel wrote:

On Thursday, 9 February 2017 at 13:30:07 UTC, jkpl wrote:

I'm looking for a better way to do this, if possible:

```
class Tool
{
string name;
}

T namedTool(alias Variable, T)()
{
T result = new T;
result.name = Variable.stringof;
return result;
}

void main()
{
Tool grep;
grep = namedTool!(grep,Tool);
assert(grep.name == "grep");
}
```

Ideally this would work like this:

```
Tool grep = namedTool!Tool;
assert(grep.name == "grep");
```

Possible ?


Sorry, but this does not make much sense to me ...
You expect "namedTool!Tool" to know that the name of the Tool 
must be set to "grep".
The expression evaluation proceeds from right to left, so at 
the time of analyzing "namedTool!Tool", the compiler knows 
nothing about "grep".
On the other hand, if you supplied the "grep" variable, the 
compiler could infer its type, like this:

```
...
auto namedTool(alias Variable)()
{
alias T = typeof(Variable);
T result = new T;
result.name = Variable.stringof;
return result;
}

void main()
{
Tool grep;
grep = namedTool!(grep);
assert(grep.name == "grep");
}
```


Or actually, maybe this will suite your case better:
```
template namedTool(T, alias Variable)
{
enum namedTool = T.stringof ~ " " ~ Variable ~ " = new " ~ 
T.stringof ~ ";" ~

 Variable ~ ".name = \"" ~ Variable ~ "\";";
}

void main()
{
mixin(namedTool!(Tool, "grep"));
assert(grep.name == "grep");
}
```


Re: Factory using an alias template parameter to set a member of the new tool ?

2017-02-09 Thread angel via Digitalmars-d-learn

On Thursday, 9 February 2017 at 13:30:07 UTC, jkpl wrote:

I'm looking for a better way to do this, if possible:

```
class Tool
{
string name;
}

T namedTool(alias Variable, T)()
{
T result = new T;
result.name = Variable.stringof;
return result;
}

void main()
{
Tool grep;
grep = namedTool!(grep,Tool);
assert(grep.name == "grep");
}
```

Ideally this would work like this:

```
Tool grep = namedTool!Tool;
assert(grep.name == "grep");
```

Possible ?


Sorry, but this does not make much sense to me ...
You expect "namedTool!Tool" to know that the name of the Tool 
must be set to "grep".
The expression evaluation proceeds from right to left, so at the 
time of analyzing "namedTool!Tool", the compiler knows nothing 
about "grep".
On the other hand, if you supplied the "grep" variable, the 
compiler could infer its type, like this:

```
...
auto namedTool(alias Variable)()
{
alias T = typeof(Variable);
T result = new T;
result.name = Variable.stringof;
return result;
}

void main()
{
Tool grep;
grep = namedTool!(grep);
assert(grep.name == "grep");
}
```


Re: capture stdout or stderr

2017-02-01 Thread angel via Digitalmars-d-learn

On Wednesday, 1 February 2017 at 01:08:19 UTC, Emil wrote:
is it possible to intercept the  STDOUT or STDERR and capture 
the output into a variable ?



some pseudocode to explain what I mean

string[] output_buffer;
stdout.capture_to(output_buffer);

writeln("test 1"); # not printed
writeln("test 2"); # not printed

stdout.release(output_buffer);

writeln("test 3"); # printed
writeln(output_buffer); # prints '["test 1","test 2"]'


No.
Please keep in mind, that in Linux, for example, stdout is a file 
...
writeln() interacts with OS API, which, of course, has nothing to 
do with internal data structures of your application.
What you could probably do is replace writeln() with a custom 
logger. In your logger implementation you can add such 
functionality.






Re: @property get/set or public varaible?

2016-12-04 Thread angel via Digitalmars-d-learn

On Sunday, 4 December 2016 at 15:30:22 UTC, vladdeSV wrote:

Hello!

I have a question not directly related to D as it is with 
coding standards.


My issue at hand is if I have one variable for a class, which I 
want to be directly accessible for anything else, should it be

 1. public, or
 2. private, with @property get/setters?

From what I have been told is that variables should be private. 
But if I do not want to make any checks whatsoever when setting 
a variable, I see no benefit to the private approach.


Are there any other reasons to use get/setters?


Make the member variable public, if it serves your purpose.
If (and when) you feel like taking some control over setting and 
getting its value, you will upgrade it to @property 
setter/getter, making the actual member variable private.
For most reasonable use cases the upgrade should pass with no 
problems.

...
If you envision such an upgrade possibility, try to keep away 
from taking your member variable address, and other not 
method-friendly operations, that might hold the upgrade back.


Re: spawnProcess() not child?

2014-11-03 Thread angel via Digitalmars-d-learn

The parent / child relationship always exists.
In POSIX OSs, you may ignore SIGCHLD signal (announcing child 
process death), so that in case of child process exit it will not 
become zombie, rather it will be disposed on the spot.
As a side note, in Linux, there exist a system call allowing 
process re-parenting, but it is intended for use in 
experimenting, rather than in normal use case.
Side note II, there is no real chance of wrapping PID numbers 
around in a reasonable time frame.