Re: byLine(n)?

2017-06-11 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, June 11, 2017 06:28:18 Stanislav Blinov via Digitalmars-d-learn 
wrote:
> On Sunday, 11 June 2017 at 05:36:08 UTC, helxi wrote:
> > I was writing a program that reads and prints the first nth
> > lines to the stdout:
> >
> > import std.stdio;
> >
> > void main(string[] args)
> > {
> >
> > import std.algorithm, std.range;
> > import std.conv;
> > stdin.byLine.take(args[1].to!ulong).each!writeln;
> >
> > }
> >
> > As far as I understand the stdin.byLine.take(args[1].to!ulong)
> > part reads all the lines written in stdin.
> > What if I want to make byLine read only and only first nth line?
> >
> > stdin.byLine(args[1].to!ulong).each!writeln;
> >
> > Obviously the code above won't work. Is there any efficient
> > workaround?
>
> You need only the nth line? Then you'd need to `drop` the
> preceding ones:
>
> void main(string[] args) {
>  import std.algorithm, std.range, std.stdio, std.conv;
>  stdin.byLine.drop(args[1].to!int-1).front.writeln;
> }
>
> Or if you need every nth line, combine `drop` and `stride`:
>
> void main(string[] args) {
>  import std.algorithm, std.range, std.stdio, std.conv;
>  auto step = args[1].to!int;
>  stdin.byLine.drop(step-1).stride(step).each!writeln;
> }

Another thing to note is that byLine reuses its buffer. So, every time
popFront is called on it, the contents of the buffer are replaced. So, if
any code keeps that dynamic array around without (i)duping it, then you get
buggy code. So, whether byLine's reuse of the buffer is a nice efficiency
boost (since it avoids reallocating the buffer) or a bug waiting to happen
depends on what your code is doing. I think that there's a decent chance
that byLine will work properly in this case, but I don't know for sure. If
it _is_ a problem that byLine reuses its buffer, then use byLineCopy
instead.

Personally, I'd be leery of using byLine outside of foreach loops because of
the buffer reuse, but some range-based code _can_ use it correctly. You just
need to be aware of the issue so that you switch to byLineCopy or (i)dup the
buffers manually if byLine's behavior is not what's correct for your code.

- Jonathan M Davis



Re: byLine(n)?

2017-06-11 Thread Cym13 via Digitalmars-d-learn

On Sunday, 11 June 2017 at 05:36:08 UTC, helxi wrote:
I was writing a program that reads and prints the first nth 
lines to the stdout:


import std.stdio;

void main(string[] args)
{
import std.algorithm, std.range;
import std.conv;
stdin.byLine.take(args[1].to!ulong).each!writeln;
}

As far as I understand the stdin.byLine.take(args[1].to!ulong) 
part reads all the lines written in stdin.

What if I want to make byLine read only and only first nth line?

stdin.byLine(args[1].to!ulong).each!writeln;

Obviously the code above won't work. Is there any efficient 
workaround?


Ok, if I read you right you are writing to stdin and want first 
to print the first args[1] lines, then to do other things with 
the other lines of stdin.


If you use byLine you will not read all the lines of stdin, but 
you will lose a line. From there I see three possibilities:


1) If you control the input, add a limit line (if you want to 
take 2 then the third line will be lost):


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

void main(string[] args) {
auto limit = args.length > 1 ? args[1].to!ulong : 2;

writefln("First %d lines", limit);
stdin.byLineCopy.take(limit).each!writeln;
writeln("Next lines");
stdin.byLineCopy.each!writeln;
}



2) Read all stdin and separate those you want to print from the 
others later:


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

void main(string[] args) {
// I used byLineCopy because of the buffer reuse issue
auto input = stdin.byLineCopy;
auto limit = args.length > 1 ? args[1].to!ulong : 2;

writefln("First %d lines", limit);
input.take(limit).each!writeln;
writeln("Next lines");
input.each!writeln;
}

3) Do not use byLine for the first lines in order to control how 
much you read.


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

void main(string[] args) {
auto limit = args.length > 1 ? args[1].to!ulong : 2;

writefln("First %d lines", limit);
foreach (line ; 0 .. limit) {
// I use write here because readln keeps the \n by default
stdin.readln.write;
}
writeln("Next lines");
stdin.byLine.each!writeln;
}


There are other options but I think these are worth considering 
first.


Re: byLine(n)?

2017-06-11 Thread Cym13 via Digitalmars-d-learn

On Sunday, 11 June 2017 at 08:33:16 UTC, Cym13 wrote:

On Sunday, 11 June 2017 at 05:36:08 UTC, helxi wrote:

[...]


Ok, if I read you right you are writing to stdin and want first 
to print the first args[1] lines, then to do other things with 
the other lines of stdin.


[...]


Meh... I just noticed my first and second propositions are 
essentially the same, the main difference is that the range 
stdin.ByLineCopy is stored so you don't create a new one when 
reading from stdin again. This is what causes the loss of a line.


D, shared objects and MacOS

2017-06-11 Thread Russel Winder via Digitalmars-d-learn
Is the creation of shared objects still not possible on MacOS with:

1. DMD;
2. LDC2;
3. GDC.

?

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

signature.asc
Description: This is a digitally signed message part


Re: D, shared objects and MacOS

2017-06-11 Thread Guillaume Piolat via Digitalmars-d-learn

On Sunday, 11 June 2017 at 09:53:09 UTC, Russel Winder wrote:
Is the creation of shared objects still not possible on MacOS 
with:


1. DMD;
2. LDC2;
3. GDC.

?



DMD:
  - possible with runtime disabled, all versions
  - or -betterC approach

LDC:
  - possible with shared runtime dynlib IIRC since >= LDC 1.1
(https://github.com/ldc-developers/ldc/pull/1705) confidence 
75%

  - possible with runtime disabled (or -betterC), all versions

GDC:
  - unknown, probably similar to LDC





Re: byLine(n)?

2017-06-11 Thread helxi via Digitalmars-d-learn

On Sunday, 11 June 2017 at 06:28:18 UTC, Stanislav Blinov wrote:

On Sunday, 11 June 2017 at 05:36:08 UTC, helxi wrote:
I was writing a program that reads and prints the first nth 
lines to the stdout:


import std.stdio;

void main(string[] args)
{
import std.algorithm, std.range;
import std.conv;
stdin.byLine.take(args[1].to!ulong).each!writeln;
}

As far as I understand the stdin.byLine.take(args[1].to!ulong) 
part reads all the lines written in stdin.
What if I want to make byLine read only and only first nth 
line?


stdin.byLine(args[1].to!ulong).each!writeln;

Obviously the code above won't work. Is there any efficient 
workaround?


You need only the nth line? Then you'd need to `drop` the 
preceding ones:


void main(string[] args) {
import std.algorithm, std.range, std.stdio, std.conv;
stdin.byLine.drop(args[1].to!int-1).front.writeln;
}

Or if you need every nth line, combine `drop` and `stride`:

void main(string[] args) {
import std.algorithm, std.range, std.stdio, std.conv;
auto step = args[1].to!int;
stdin.byLine.drop(step-1).stride(step).each!writeln;
}


I was actually just looking for ways to read the first n line and 
then print it ($man head). My current program

1. reads all the lines provided by the stdin (bottleneck)
2. takes the first n lines (bottleneck)
3. prints each line


I want to
1. read all the lines
2. when line number n is reached, stop reading the rest of the 
input

3. print each line


Re: byLine(n)?

2017-06-11 Thread Cym13 via Digitalmars-d-learn

On Sunday, 11 June 2017 at 12:44:05 UTC, helxi wrote:

On Sunday, 11 June 2017 at 06:28:18 UTC, Stanislav Blinov wrote:

On Sunday, 11 June 2017 at 05:36:08 UTC, helxi wrote:

[...]


You need only the nth line? Then you'd need to `drop` the 
preceding ones:


void main(string[] args) {
import std.algorithm, std.range, std.stdio, std.conv;
stdin.byLine.drop(args[1].to!int-1).front.writeln;
}

Or if you need every nth line, combine `drop` and `stride`:

void main(string[] args) {
import std.algorithm, std.range, std.stdio, std.conv;
auto step = args[1].to!int;
stdin.byLine.drop(step-1).stride(step).each!writeln;
}


I was actually just looking for ways to read the first n line 
and then print it ($man head). My current program

1. reads all the lines provided by the stdin (bottleneck)
2. takes the first n lines (bottleneck)
3. prints each line


I want to
1. read all the lines
2. when line number n is reached, stop reading the rest of the 
input

3. print each line


byLine doesn't reall all input at once. Using byline and take you 
are effectively reading only the right amount of lines and not 
reading the rest. You already have what you want, what makes you 
think the contrary?


Re: byLine(n)?

2017-06-11 Thread helxi via Digitalmars-d-learn

On Sunday, 11 June 2017 at 12:49:51 UTC, Cym13 wrote:
print each line


byLine doesn't reall all input at once. Using byline and take 
you are effectively reading only the right amount of lines and 
not reading the rest. You already have what you want, what 
makes you think the contrary?


Oh it was my lack of understanding then. Thank you. I would also 
be really humbled if you demonstrate a faster approach of 
achieving the goal of the program :) (without explicitly using 
loops and conditions)


Re: std.path.buildPath

2017-06-11 Thread Ryan Frame via Digitalmars-d-learn

On Sunday, 4 June 2017 at 18:15:36 UTC, Russel Winder wrote:
On Sun, 2017-06-04 at 17:56 +0200, Jacob Carlborg via 
Digitalmars-d- learn wrote:

On 2017-06-04 07:44, Jesse Phillips wrote:

> What is your expected behavior? Throw an exception? You can't
> really
> append an absolute path to another.

Of course you can. I expect buildPath("/foo", "/bar") to 
result in "/foo/bar". That's how Ruby behaves.


And Python, Groovy, Java, Kotlin, Ceylon, C++, …


Python 3.5.1 on my machine:

>>> os.path.join("/asdf", "/bcd")
'/bcd'
>>> os.path.join("asdf", "/bcd")
'/bcd'


Re: byLine(n)?

2017-06-11 Thread ag0aep6g via Digitalmars-d-learn

On 06/11/2017 03:00 PM, helxi wrote:
I would also be 
really humbled if you demonstrate a faster approach of achieving the 
goal of the program :) (without explicitly using loops and conditions)


Do you have a reason to believe that your version is slow? I don't see 
why it would be.


Re: How to implement opCmp?

2017-06-11 Thread Honey via Digitalmars-d-learn

On Friday, 9 June 2017 at 17:50:28 UTC, Honey wrote:
Looking at the implementation of Tuple.opCmp, I'm not sure I'd 
bet on existence of opCmp for fundamental types:


int opCmp(R)(R rhs)
if (areCompatibleTuples!(typeof(this), R, "<"))
{
foreach (i, Unused; Types)
{
if (field[i] != rhs.field[i])
{
return field[i] < rhs.field[i] ? -1 : 1;
}
}
return 0;
}


It turned out that there is a standard three way comparison 
function for ranges and strings [1].


Doesn't it make sense to introduce another overload of cmp 
similar to Steve's doCmp [2] right at that spot?


This would simplify the implementation of opCmp for aggregates 
and can also lead to a moderate performance benefit [3]. (Note 
that [3] does not provide an additional overload of cmp but uses 
a less elegant approach with the same performance 
characteristics.)


// $ ldc2 -O3 -release -boundscheck=off cmpTuple.d
//
// real 0m18.787s
// user 0m18.784s
// sys  0m0.003s
//
//
// $ ldc2 -O3 -release -boundscheck=off cmpTuple.d 
-d-version=singlePassCmp

// $ time ./cmpTuple
//
// real 0m16.109s
// user 0m16.102s
// sys  0m0.007s

[1] https://dlang.org/phobos/std_algorithm_comparison.html#cmp
[2] http://forum.dlang.org/post/ohedtb$1phe$1...@digitalmars.com
[3] https://dpaste.dzfl.pl/7cbfa2e1965b


Re: How to implement opCmp?

2017-06-11 Thread Honey via Digitalmars-d-learn

On Sunday, 11 June 2017 at 15:24:30 UTC, Honey wrote:
Doesn't it make sense to introduce another overload of cmp 
similar to Steve's doCmp [2] right at that spot?


Moreover, it seems that std.algorithm.cmp should employ three way 
comparison as well. The current implementation


int cmp(alias pred = "a < b", R1, R2)(R1 r1, R2 r2)
if (isInputRange!R1 && isInputRange!R2 && !(isSomeString!R1 && 
isSomeString!R2))

{
for (;; r1.popFront(), r2.popFront())
{
if (r1.empty) return -cast(int)!r2.empty;
if (r2.empty) return !r1.empty;
auto a = r1.front, b = r2.front;
if (binaryFun!pred(a, b)) return -1;
if (binaryFun!pred(b, a)) return 1;
}
}

does not seem to be great.


Re: How to implement opCmp?

2017-06-11 Thread Honey via Digitalmars-d-learn

On Sunday, 11 June 2017 at 15:40:42 UTC, Honey wrote:

On Sunday, 11 June 2017 at 15:24:30 UTC, Honey wrote:
Doesn't it make sense to introduce another overload of cmp 
similar to Steve's doCmp [2] right at that spot?


Moreover, it seems that std.algorithm.cmp should employ three 
way comparison as well.


Forget about it. I think a different approach is required, here.


First time user of LDC and getting newbie problems.

2017-06-11 Thread WhatMeForget via Digitalmars-d-learn


Just trying to compile a "Hello World" using dub and ldc2. The 32 
bit works fine:


generic@generic-ThinkPad-T61:~/Desktop/test$ dub run 
--compiler=ldc2 --arch=x86

Performing "debug" build using ldc2 for x86.
test ~master: target for configuration "application" is up to 
date.

To force a rebuild of up-to-date targets, run again with --force.
Running ./test
hello there

But all hell breaks out when I try a 64 bit build. I've got a 64 
bit cpu and 64 bit Xubuntu so I thought, if I was going to get 
errors, it would be the previous case.



generic@generic-ThinkPad-T61:~/Desktop/test$ dub run 
--compiler=ldc2 --arch=x86_64

Performing "debug" build using ldc2 for x86_64.
test ~master: building configuration "application"...
/usr/bin/ld: skipping incompatible /usr/lib/libphobos2-ldc.so 
when searching for -lphobos2-ldc
/usr/bin/ld: skipping incompatible /usr/lib/libphobos2-ldc.a when 
searching for -lphobos2-ldc
/usr/bin/ld: skipping incompatible 
/usr/lib/gcc/i686-linux-gnu/6/../../../libphobos2-ldc.so when 
searching for -lphobos2-ldc
/usr/bin/ld: skipping incompatible 
/usr/lib/gcc/i686-linux-gnu/6/../../../libphobos2-ldc.a when 
searching for -lphobos2-ldc
/usr/bin/ld: skipping incompatible //usr/lib/libphobos2-ldc.so 
when searching for -lphobos2-ldc
/usr/bin/ld: skipping incompatible //usr/lib/libphobos2-ldc.a 
when searching for -lphobos2-ldc

/usr/bin/ld: cannot find -lphobos2-ldc
/usr/bin/ld: skipping incompatible /usr/lib/libdruntime-ldc.so 
when searching for -ldruntime-ldc
/usr/bin/ld: skipping incompatible /usr/lib/libdruntime-ldc.a 
when searching for -ldruntime-ldc
/usr/bin/ld: skipping incompatible 
/usr/lib/gcc/i686-linux-gnu/6/../../../libdruntime-ldc.so when 
searching for -ldruntime-ldc
/usr/bin/ld: skipping incompatible 
/usr/lib/gcc/i686-linux-gnu/6/../../../libdruntime-ldc.a when 
searching for -ldruntime-ldc
/usr/bin/ld: skipping incompatible //usr/lib/libdruntime-ldc.so 
when searching for -ldruntime-ldc
/usr/bin/ld: skipping incompatible //usr/lib/libdruntime-ldc.a 
when searching for -ldruntime-ldc

/usr/bin/ld: cannot find -ldruntime-ldc
collect2: error: ld returned 1 exit status
Error: /usr/bin/gcc failed with status: 1



Re: brew install dmd

2017-06-11 Thread Joel via Digitalmars-d-learn

On Sunday, 11 June 2017 at 04:08:56 UTC, Seb wrote:

But I'm not sure about doing this.


This is a copy of the __official__ D installer as advertised on 
dlang.org (http://dlang.org/download.html):



curl -fsS https://dlang.org/install.sh | bash -s dmd


(the releases are signed)

i.dlang.io is just a handy alternative, s.t. you can save 
keystrokes on a foreign machine and install dmd in 25 
keystrokes (`curl i.dlang.io | bash -s`).


I only want to use Home Brew.

I'm still getting `pointer not aligned` linker errors!


O(1) sum

2017-06-11 Thread helxi via Digitalmars-d-learn

Is it possible to sum an array in O(1)?


Re: O(1) sum

2017-06-11 Thread Stefan Koch via Digitalmars-d-learn

On Monday, 12 June 2017 at 01:02:58 UTC, helxi wrote:

Is it possible to sum an array in O(1)?


No.
If you want to sum the elements you have to at-least look at all 
the elements.

So it'll always be O(N).
it's the best you can do.


Re: O(1) sum

2017-06-11 Thread Biotronic via Digitalmars-d-learn

On Monday, 12 June 2017 at 01:36:04 UTC, Stefan Koch wrote:

On Monday, 12 June 2017 at 01:02:58 UTC, helxi wrote:

Is it possible to sum an array in O(1)?


No.
If you want to sum the elements you have to at-least look at 
all the elements.

So it'll always be O(N).
it's the best you can do.


On a multi-core system we can do better:

auto nums = iota(10_000_000.0f);
auto sum = taskPool.reduce!"a + b"(nums);

Given arbitrary parallelism (yeah, right!), this will be 
O(log(N)). For real-world systems, it might give a speed-up for 
large arrays, but won't reduce the big-O complexity. Of course, 
there will also be overhead to such a solution, so there is a 
limit to how much one'd actually benefit from it.


--
  Biotronic


Re: Is D slow?

2017-06-11 Thread Ola Fosheim Grøstad via Digitalmars-d-learn

On Friday, 9 June 2017 at 19:43:55 UTC, Era Scarecrow wrote:
On Friday, 9 June 2017 at 18:32:06 UTC, Steven Schveighoffer 
wrote:

Wow, so that's how D code would look like if it were C++ :)


 When dipping my toes into C++ to do a quicksort algorithm, I 
quickly got annoyed I'd have to create all the individual 
comparison functions rather than just one like in D... Which is


Not sure what you mean by that? You only need one in C++.

http://en.cppreference.com/w/cpp/utility/functional/less