Re: Regex multiple matches

2017-04-13 Thread rikki cattermole via Digitalmars-d-learn

On 14/04/2017 3:54 AM, Jethro wrote:

using the rule (?Pregex)

e.g., (?P\w*)*

how do we get at all the matches, e.g., Joe Bob Buddy?

When I access the results captures they are are not arrays and I only
ever get the first match even when I'm using matchAll.


Pseudo code:

foreach(result; matcher) {
...
}

It returns an input range that can "act" as an array without the lookup 
by index, it is a very powerful abstraction.


Regex multiple matches

2017-04-13 Thread Jethro via Digitalmars-d-learn

using the rule (?Pregex)

e.g., (?P\w*)*

how do we get at all the matches, e.g., Joe Bob Buddy?

When I access the results captures they are are not arrays and I 
only ever get the first match even when I'm using matchAll.







Re: a newbie problem regarding splitter()

2017-04-13 Thread alex via Digitalmars-d-learn

On Thursday, 13 April 2017 at 06:42:30 UTC, Ali Çehreli wrote:

On 04/12/2017 11:33 PM, alex wrote:
> Hello,
>
"The D Programming Language" by Andrei Alexandrescu.

Great book but a lot has changed in D since the book was 
written in 2010. Your issue is in the book's errata:


  http://erdani.com/tdpl/errata/

So, the following should work today:




Ali


Thank you Ali - very good.
Alex




Re: ordered Associative array

2017-04-13 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Apr 14, 2017 at 12:29:34AM +, Jethro via Digitalmars-d-learn wrote:
> Is there a way to retain the ordering of an associative array? When
> the elements are added then looped over using a foreach, the order is
> different.

An AA is implemented as an unordered hash. So the insertion order is
usually unrelated to the foreach order. If you want keys to be sorted
according to some order, consider using std.container.rbtree instead.

If you want to retain insertion order, you could wrap an AA inside a
custom container that keeps track of insertion order. Something along
these lines:

/* Warning: untested, proof-of-concept code. This assumes you
 * never remove stuff from the AA.  If you want to support
 * .remove you'll have to do a bit more work, obviously.
 */
struct InsertionOrderAA(K,V) {
V[K] _impl;
K[] keyOrder;

void opIndexAssign(V value, K key) {
_impl[key] = value;
keyOrder ~= key;
}
Value opIndex(K key) {
return _impl[key];
}
void opApply(scope void delegate(K,V) dg) {
foreach (key; keyOrder) {
auto ret = dg(key, _impl[key]);
if (ret != 0) return ret;
}
}
}


T

-- 
I am not young enough to know everything. -- Oscar Wilde


Re: ordered Associative array

2017-04-13 Thread solidstate1991 via Digitalmars-d-learn

On Friday, 14 April 2017 at 00:29:34 UTC, Jethro wrote:
Is there a way to retain the ordering of an associative array? 
When the elements are added then looped over using a foreach, 
the order is different.


Use a separate array to store the keys, order them all time when 
you add a new one, etc. This was the only workaround I could come 
up with for my engine (see it here: 
https://github.com/ZILtoid1991/pixelperfectengine/blob/master/source/PixelPerfectEngine/graphics/layers.d at Classes SpriteLayer and SpriteLayer32Bit), maybe in the future I'll come up with a better solution. I call this technique as "prioritized hash table" as the keys give them a certain priority, so there's no issue which sprite where supposed to show up.


ordered Associative array

2017-04-13 Thread Jethro via Digitalmars-d-learn
Is there a way to retain the ordering of an associative array? 
When the elements are added then looped over using a foreach, the 
order is different.




Re: Generating switch at Compile Time

2017-04-13 Thread ag0aep6g via Digitalmars-d-learn

On 04/13/2017 11:06 PM, Jesse Phillips wrote:

-

[...]

private static immutable list = AliasSeq!(
tuple("a", "q"),
tuple("b", "r"),
);

[...]

switch(search) {
--->foreach(li; list) { // li initialization is skipped
mixin("case li[0]:");
mixin("writeln(li[1]);");
return;
}
default:
break;
}

[...]

}
-

Thoughts?


That's not a static foreach. It's a normal run-time foreach. The switch 
jumps into the loop body without the loop head ever executing. The 
compiler is correct when it says that initialization of li is being skipped.


Make `list` an enum or alias instead. Then the foreach is unrolled at 
compile time, you don't get a deprecation message, and it works correctly.


By the way, in my opinion, `case li[0]:` shouldn't compile with the 
static immutable `list`. `list` and `li[0]` are dynamic values. The 
compiler only attempts (and succeeds) to evaluate them at compile time 
because they're typed as immutable. The way I see it, that only makes 
things more confusing.


Generating switch at Compile Time

2017-04-13 Thread Jesse Phillips via Digitalmars-d-learn
I realize that this is likely really pushing the compile time 
generation but a recent change to the switch statement[1] is 
surfacing because of this usage.


uninitswitch2.d(13): Deprecation: 'switch' skips declaration of 
variable uninits

witch2.main.li at uninitswitch2.d(14)

-
import std.traits;
import std.typecons;
import std.meta;

private static immutable list = AliasSeq!(
tuple("a", "q"),
tuple("b", "r"),
);

void main() {
import std.stdio;
string search;
switch(search) {
--->foreach(li; list) { // li initialization is skipped
mixin("case li[0]:");
mixin("writeln(li[1]);");
return;
}
default:
break;
}

// Works
mixin(genSwitch("search"));
}
-

I realize I can build out the entire switch and mix it in:

-
string genSwitch(string search) {
auto ans = "switch(" ~ search ~ ") {\n";
foreach(li; list) {
ans ~= "case \"" ~ li[0] ~ "\":\n";
ans ~= "writeln(\"" ~ li[1] ~ "\");\n";
ans ~= "return;\n";
}
ans ~= "default:\n";
ans ~= "break;\n";
ans ~= "}";

return ans;
}
-

But I'm just wondering if the new initialization check should not 
be triggered from this utilization.


-
// Unrolled based on
// 
https://wiki.dlang.org/User:Quickfur/Compile-time_vs._compile-time description


version(none)
void func2243(Tuple param0, Tuple param1) {
{
{
case param0[0]:
writeln(param0[1]);
return;
}
{
case param1[0]:
writeln(param1[1]);
return;
}
}
}
-

Thoughts?

1. https://issues.dlang.org/show_bug.cgi?id=14532


using joyent manta public cloud storage service

2017-04-13 Thread flamencofantasy via Digitalmars-d-learn

hello,
I'm trying to use the joyent manta storage service via their REST 
api.

https://apidocs.joyent.com/manta/api.html

For that i need to implement http signature over TLS.
Here is a shell function that does that;
function manta {

local alg=rsa-sha256
local keyId=/$MANTA_USER/keys/$MANTA_KEY_ID
local now=$(date -u "+%a, %d %h %Y %H:%M:%S GMT")
local sig=$(echo "date:" $now | \
tr -d '\n' | \
openssl dgst -sha256 -sign $HOME/.ssh/id_rsa | \
openssl enc -e -a | tr -d '\n')

curl -sS $MANTA_URL"$@" -H "date: $now"  \
-H "Authorization: Signature 
keyId=\"$keyId\",algorithm=\"$alg\",signature=\"$sig\""

}

How can I implement it in D using vibe.d? Specifically how do I 
sign and encode using my private key in D;
openssl dgst -sha256 -sign $HOME/.ssh/id_rsa | openssl enc -e -a 
| tr -d '\n')


Thanks.


Re: ctRegex with variable?

2017-04-13 Thread Jesse Phillips via Digitalmars-d-learn

On Wednesday, 12 April 2017 at 21:25:40 UTC, Jethro wrote:
Can regex's have variables in them? I'd like to create a 
ctRegex but match on runtime strings that are known at runtime.


e.g.,

auto c = ctRegex~("x{var}")


As mentioned by Ali, benchmark for your use case.

If var has common values (e.g. 1-1000). generate a ctRegex table 
for those values and use runtime for any that aren't in the table.


Re: ndslice summary please

2017-04-13 Thread 9il via Digitalmars-d-learn
On Thursday, 13 April 2017 at 15:22:46 UTC, Martin Tschierschke 
wrote:

On Thursday, 13 April 2017 at 08:47:16 UTC, Ali Çehreli wrote:
I haven't played with ndslice nor followed its deprecation 
discussions. Could someone summarize it for us please. Also, 
is it still used outside Phobos or is Ilya or someone else 
rewriting it?


Ali


We should additionally mention sometimes, that the naming of 
ndslice is derived from ndarray
and this is from N-dimensional Array. No one searching for N - 
dimensional Array OR Matrix will find ndslice, without this 
info easily. Just try a google search: "dlang n dimesional 
array"

Regards mt.


... plus link in the spec
https://github.com/dlang/dlang.org/pull/1634


Re: ndslice summary please

2017-04-13 Thread 9il via Digitalmars-d-learn
On Thursday, 13 April 2017 at 15:22:46 UTC, Martin Tschierschke 
wrote:

On Thursday, 13 April 2017 at 08:47:16 UTC, Ali Çehreli wrote:
I haven't played with ndslice nor followed its deprecation 
discussions. Could someone summarize it for us please. Also, 
is it still used outside Phobos or is Ilya or someone else 
rewriting it?


Ali


We should additionally mention sometimes, that the naming of 
ndslice is derived from ndarray
and this is from N-dimensional Array. No one searching for N - 
dimensional Array OR Matrix will find ndslice, without this 
info easily. Just try a google search: "dlang n dimesional 
array"

Regards mt.


The first link for me in incognito mode is:
https://wiki.dlang.org/Dense_multidimensional_arrays

It contained example for std.experimental.ndslice. But thanks for 
the idea. Reworked  for Mir now.


Thanks,
Ilya


Re: How to continue after the book?

2017-04-13 Thread Vasudev Ram via Digitalmars-d-learn

On Tuesday, 28 March 2017 at 07:27:31 UTC, I Lindström wrote:
After getting the basics down, how did you continue when 
learning programming in general?


Many other good suggestions here already.

1. Another idea: pick some small tools or utilities that you 
would like to create, and write them in D. (And another - start 
writing small parts of your planned app in D, using the knowledge 
from the book you read. Learn more bits of D as needed, and use 
that to implement more parts of the app.)


2. Along the lines of JamesD's link below, here are some small D 
example programs from my blog, but complementary to his, since 
these are mostly command-line ones (at the time of writing this).


https://jugad2.blogspot.com/search/label/DLang [1]

There are few posts at the above link that do not have actual 
code examples, such as a few D videos etc. View or skip those as 
you wish.


Here are the post titles so you can get an idea of what examples 
are there:


Porting the text pager from Python to D (DLang)

Simple parallel processing in D with std.parallelism

Using std.datetime.StopWatch to time sections of D code

Read from CSV with D, write to PDF with Python

Command line D utility - find files matching a pattern under a 
directory


min_fgrep: minimal fgrep command in D

num_cores: find number of cores in your PC's processor

Calling a simple C function from D - strcmp

Func-y D + Python pipeline to generate PDF

file_sizes utility in D: print sizes of all files under a 
directory tree


deltildefiles: D language utility to recursively delete vim 
backup files


[DLang]: A simple file download utility in D

Getting CPU info with D (the D language)

All of those posts are available at the link marked [1] above.

HTH,
Vasudev
---
Vasudev Ram
Site: https://vasudevram.github.io
Blog: https://jugad2.blogspot.com



Re: ndslice summary please

2017-04-13 Thread Martin Tschierschke via Digitalmars-d-learn

On Thursday, 13 April 2017 at 08:47:16 UTC, Ali Çehreli wrote:
I haven't played with ndslice nor followed its deprecation 
discussions. Could someone summarize it for us please. Also, is 
it still used outside Phobos or is Ilya or someone else 
rewriting it?


Ali


We should additionally mention sometimes, that the naming of 
ndslice is derived from ndarray
and this is from N-dimensional Array. No one searching for N - 
dimensional Array OR Matrix will find ndslice, without this info 
easily. Just try a google search: "dlang n dimesional array"

Regards mt.


Re: ndslice summary please

2017-04-13 Thread Ilya Yaroshenko via Digitalmars-d-learn

On Thursday, 13 April 2017 at 15:00:16 UTC, Dejan Lekic wrote:

On Thursday, 13 April 2017 at 10:00:43 UTC, 9il wrote:

On Thursday, 13 April 2017 at 08:47:16 UTC, Ali Çehreli wrote:

[...]


The reasons to use mir-algorithm instead of std.range, 
std.algorithm, std.functional (when applicable):


1. It allows easily construct one and multidimensional random 
access ranges. You may compare `bitwise` implementation in 
mir-algorithm and Phobos. Mir's version few times smaller and 
do not have Phobos bugs like non mutable `front`. See also 
`bitpack`.

2. Mir devs are very cary about BetterC
3. Slice is universal, full featured, and multidimensional 
random access range. All RARs can be expressed through generic 
Slice struct.

4. It is faster to compile and generates less templates bloat.
For example:

slice.map!fun1.map!fun2

is the same as

slice.map!(pipe!(fun1, fun2))

`map` and `pipe` are from mir-algorithm.


It is all good, but I am sure many D programmers, myself 
included, would appreciate if shortcomings of Phobos are fixed 
instead of having a completely separate package with set of 
features that overlap... I understand ndslice was at some point 
in the `experimental` package, but again - it would be good if 
you improve existing Phobos stuff instead of providing a 
separate library that provides better implementation(s).


Work on Phobos is useless for me because when I need something 
and I am ready to write / fix it I have few days, but not few 
months until LDC release. DUB is more flexible, it allows to 
override version with local path for example. Finally, I think 
Phobos should be deprecated and be split into dub packages.


Re: ndslice summary please

2017-04-13 Thread Dejan Lekic via Digitalmars-d-learn

On Thursday, 13 April 2017 at 10:00:43 UTC, 9il wrote:

On Thursday, 13 April 2017 at 08:47:16 UTC, Ali Çehreli wrote:
I haven't played with ndslice nor followed its deprecation 
discussions. Could someone summarize it for us please. Also, 
is it still used outside Phobos or is Ilya or someone else 
rewriting it?


Ali


The reasons to use mir-algorithm instead of std.range, 
std.algorithm, std.functional (when applicable):


1. It allows easily construct one and multidimensional random 
access ranges. You may compare `bitwise` implementation in 
mir-algorithm and Phobos. Mir's version few times smaller and 
do not have Phobos bugs like non mutable `front`. See also 
`bitpack`.

2. Mir devs are very cary about BetterC
3. Slice is universal, full featured, and multidimensional 
random access range. All RARs can be expressed through generic 
Slice struct.

4. It is faster to compile and generates less templates bloat.
For example:

slice.map!fun1.map!fun2

is the same as

slice.map!(pipe!(fun1, fun2))

`map` and `pipe` are from mir-algorithm.


It is all good, but I am sure many D programmers, myself 
included, would appreciate if shortcomings of Phobos are fixed 
instead of having a completely separate package with set of 
features that overlap... I understand ndslice was at some point 
in the `experimental` package, but again - it would be good if 
you improve existing Phobos stuff instead of providing a separate 
library that provides better implementation(s).


Re: ndslice summary please

2017-04-13 Thread 9il via Digitalmars-d-learn

On Thursday, 13 April 2017 at 08:47:16 UTC, Ali Çehreli wrote:
I haven't played with ndslice nor followed its deprecation 
discussions. Could someone summarize it for us please. Also, is 
it still used outside Phobos or is Ilya or someone else 
rewriting it?


Ali


The reasons to use mir-algorithm instead of std.range, 
std.algorithm, std.functional (when applicable):


1. It allows easily construct one and multidimensional random 
access ranges. You may compare `bitwise` implementation in 
mir-algorithm and Phobos. Mir's version few times smaller and do 
not have Phobos bugs like non mutable `front`. See also `bitpack`.

2. Mir devs are very cary about BetterC
3. Slice is universal, full featured, and multidimensional random 
access range. All RARs can be expressed through generic Slice 
struct.

4. It is faster to compile and generates less templates bloat.
For example:

slice.map!fun1.map!fun2

is the same as

slice.map!(pipe!(fun1, fun2))

`map` and `pipe` are from mir-algorithm.



Re: ndslice summary please

2017-04-13 Thread 9il via Digitalmars-d-learn

On Thursday, 13 April 2017 at 08:47:16 UTC, Ali Çehreli wrote:
I haven't played with ndslice nor followed its deprecation 
discussions. Could someone summarize it for us please. Also, is 
it still used outside Phobos or is Ilya or someone else 
rewriting it?


Ali


Hello Ali,

ndslice was removed from Phobos because it is to hard to maintain 
Phobos and Mir at the same time. It is better to have dub 
packages instead of big Phobos, IMHO.


ndslice was completely rewritten and extended in mir-algorithm 
package [0, 1]  since ndslice was deprecated in Phobos. Its API 
is stable enough and includes all kinds of tensors. New ndslice 
is used in two Tamediadigital's projects [3, 4]. See also its 
README for more details.


The old ndslice (like in Phobos) is located in parent Mir package 
[2].

The last Mir version with old ndslice is v0.22.1.

[0] http://docs.algorithm.dlang.io
[1] https://github.com/libmir/mir-algorithm
[2] https://github.com/libmir/mir
[3] https://github.com/tamediadigital/lincount
[4] https://github.com/tamediadigital/hll-d

If you have any questions about ndslice I would be happy to 
answer.


Best regards,
Ilya



ndslice summary please

2017-04-13 Thread Ali Çehreli via Digitalmars-d-learn
I haven't played with ndslice nor followed its deprecation discussions. 
Could someone summarize it for us please. Also, is it still used outside 
Phobos or is Ilya or someone else rewriting it?


Ali


Re: a newbie problem regarding splitter()

2017-04-13 Thread Ali Çehreli via Digitalmars-d-learn

On 04/12/2017 11:33 PM, alex wrote:
> Hello,
>
> I've just started learning D, working my way through "The D Programming
> Language" by Andrei Alexandrescu.

Great book but a lot has changed in D since the book was written in 
2010. Your issue is in the book's errata:


  http://erdani.com/tdpl/errata/

So, the following should work today:

import std.stdio, std.string;
import std.algorithm;

void main(){
ulong[string] dictionary;
foreach (line; stdin.byLine()){
foreach (word; splitter(strip(line))){
if (word in dictionary) continue;
auto newID = dictionary.length;
dictionary[word.idup] = newID;
writeln(newID, '\t', word);
}
}
}

Ali



a newbie problem regarding splitter()

2017-04-13 Thread alex via Digitalmars-d-learn

Hello,

I've just started learning D, working my way through "The D 
Programming Language" by Andrei Alexandrescu. On page 8 there is:

import std.stdio, std.string;

void main(){
uint[string] dictionary;
foreach (line; stdin.byLine()){
foreach (word; splitter(strip(line))){
if (word in dictionary) continue;
auto newID = dictionary.length;
dictionary[word] = newID;
writeln(newID, '\t', word);
}
}
}

When i try to run it, i get that splitter is 'undefined'. I can't 
spot the error. Maybe a fresh pair of eyes will do it??


Thank you,
Alex