Re: byLine(n)?

2017-06-10 Thread Stanislav Blinov 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?


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;
}



byLine(n)?

2017-06-10 Thread helxi via Digitalmars-d-learn
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?


Re: brew install dmd

2017-06-10 Thread Seb via Digitalmars-d-learn

On Sunday, 11 June 2017 at 03:48:57 UTC, Joel wrote:

On Sunday, 11 June 2017 at 02:38:21 UTC, Mike Parker wrote:

On Sunday, 11 June 2017 at 01:30:37 UTC, Joel wrote:

but still 2.074.0 (not 74.1)



Joels-MacBook-Pro:Testing joelchristensen$ dmd
DMD64 D Compiler v2.074.0


It's probably related to this:

http://forum.dlang.org/post/abyacihlxtcphnhzi...@forum.dlang.org


Looks like I could put this:

curl https://i.dlang.io | bash -s

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


Re: brew install dmd

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

On Sunday, 11 June 2017 at 03:54:05 UTC, Mike Parker wrote:

On Sunday, 11 June 2017 at 03:48:57 UTC, Joel wrote:



curl https://i.dlang.io | bash -s

But I'm not sure about doing this.


I'm don't what that means, but Homebrew is giving you 2.074.1, 
it's just that the binary has the wrong version number compiled 
in.


Oh, ok. Thanks Mike.


Re: brew install dmd

2017-06-10 Thread Mike Parker via Digitalmars-d-learn

On Sunday, 11 June 2017 at 03:48:57 UTC, Joel wrote:



curl https://i.dlang.io | bash -s

But I'm not sure about doing this.


I'm don't what that means, but Homebrew is giving you 2.074.1, 
it's just that the binary has the wrong version number compiled 
in.


Re: brew install dmd

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

On Sunday, 11 June 2017 at 02:38:21 UTC, Mike Parker wrote:

On Sunday, 11 June 2017 at 01:30:37 UTC, Joel wrote:

but still 2.074.0 (not 74.1)



Joels-MacBook-Pro:Testing joelchristensen$ dmd
DMD64 D Compiler v2.074.0


It's probably related to this:

http://forum.dlang.org/post/abyacihlxtcphnhzi...@forum.dlang.org


Looks like I could put this:

curl https://i.dlang.io | bash -s

But I'm not sure about doing this.



Re: brew install dmd

2017-06-10 Thread Mike Parker via Digitalmars-d-learn

On Sunday, 11 June 2017 at 01:30:37 UTC, Joel wrote:

but still 2.074.0 (not 74.1)



Joels-MacBook-Pro:Testing joelchristensen$ dmd
DMD64 D Compiler v2.074.0


It's probably related to this:

http://forum.dlang.org/post/abyacihlxtcphnhzi...@forum.dlang.org


brew install dmd

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

but still 2.074.0 (not 74.1)

I'm not that familier brew. I've tried `brew upgrade` and `brew 
update`.


Joels-MacBook-Pro:Testing joelchristensen$ brew upgrade dmd
==> Upgrading 1 outdated package, with result:
dmd 2.074.1
==> Upgrading dmd
==> Downloading 
https://homebrew.bintray.com/bottles/dmd-2.074.1.sierra.bottle.t

 100.0%
==> Pouring dmd-2.074.1.sierra.bottle.tar.gz
==> Using the sandbox
Warning: An old dmd.conf was found and will be moved to 
/usr/local/etc/dmd.conf.old.

🍺  /usr/local/Cellar/dmd/2.074.1: 587 files, 67MB
Joels-MacBook-Pro:Testing joelchristensen$ dmd
DMD64 D Compiler v2.074.0



Re: Dub or Dmd trying to use some funny path to find linker.exe

2017-06-10 Thread Mike Wey via Digitalmars-d-learn

On 06/10/2017 07:04 PM, WhatMeWorry wrote:


Dub or Dmd dies when it can't find the linker.  Like so:

C:\Users\kheaser\Git\Delivery\projects\00_01_print_ogl_ver>dub run 
--arch=x86_64 --force

Performing "debug" build using dmd for x86_64.
derelict-util 2.0.6: building configuration "library"...
derelict-al 1.0.3: building configuration "library"...
derelict-assimp3 1.3.0: building configuration "library"...
derelict-fi 2.0.3: building configuration "library"...
derelict-fmod 2.0.4: building configuration "library"...
derelict-ft 1.1.3: building configuration "library"...
derelict-gl3 1.0.23: building configuration "library"...
derelict-glfw3 3.1.3: building configuration "derelict-glfw3-dynamic"...
00_01_print_ogl_ver ~master: building configuration "application"...
Linking...
Error: can't run 
'C:\Users\Administrator\Desktop\VC\\bin\x86_amd64\link.exe', check PATH

dmd failed with exit code -1.

C:\Users\kheaser\OneDrive for 
Business\GitHub\Delivery\projects\00_01_print_ogl_ver>where link.exe

C:\D\dmd2\windows\bin\link.exe

First off, I have no idea why dub (or dmd) is trying to use the path 
C:\Users\Administrator\Desktop\VC\\bin\x86_amd64\ to find link.exe?


I did the where command link.exe to show that the linker is in my PATH 
at C:\D\dmd2\windows\bin\.


So can I specify the linker path explicitly and if so, how is that done?

Sorry if this is a stupid question, but I've looked at the documentation 
all morning and not finding anything.  Thanks.


You are building a 64bit executable, and in that case the linker shiped 
with dmd isn't used, as it only handles 32bit (OMF). dmd instead relies 
on the microsoft linker. Judging from the error the path to the Windows 
SDK / linker is not set correctly in dmd's sc.ini.


--
Mike Wey


Re: when I run dub within git bash, dub prompts are not displayed. For instance...

2017-06-10 Thread Anonymouse via Digitalmars-d-learn

On Saturday, 10 June 2017 at 16:11:31 UTC, WhatMeWorry wrote:
kheaser@IT-ASST-SB MINGW64 
/c/Users/kheaser/Git/Delivery/projects (master)

$ dub init 00_01_print_ogl_ver



... All this white space here is me just pressing the Enter key
... to get the default values.


If the problem is that the text doesn't show until you've entered 
past all the prompts and the program exits, then it's the 
terminal you're using not playing well with D.


With the default Cygwin terminal this will not show a delayed 
countdown, but rather all numbers at once after the combined 
pause:


import std.stdio;
import core.thread;

void main()
{
foreach (i; 0..5)
{
writeln(i);
Thread.sleep(1.seconds);
}
}

With the normal Windows command prompt, it behaves as it should. 
Someone else will have to explain what the problem is, but it's 
something about the terminal not acting like a proper tty or 
something along those lines.


Re: std.stdio.stderr

2017-06-10 Thread Russel Winder via Digitalmars-d-learn
On Sat, 2017-06-10 at 16:45 +, Antonio Corbi via Digitalmars-d-
learn wrote:
> […]
> 
> It seems to work for me with a dumb example:
[…]

Spurred on by your report of success, I discovered my error. D treats a
char[1024] as 1024 characters when using the %s format specifier. I had
to use fromStringz using a cast to get the %s to get the null
terminated string.

The problems of idiomatic C code (crap) converted to D code (good, but
with hacks).

Thanks for your reply, it was most helpful.

-- 
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


Dub or Dmd trying to use some funny path to find linker.exe

2017-06-10 Thread WhatMeWorry via Digitalmars-d-learn


Dub or Dmd dies when it can't find the linker.  Like so:

C:\Users\kheaser\Git\Delivery\projects\00_01_print_ogl_ver>dub 
run --arch=x86_64 --force

Performing "debug" build using dmd for x86_64.
derelict-util 2.0.6: building configuration "library"...
derelict-al 1.0.3: building configuration "library"...
derelict-assimp3 1.3.0: building configuration "library"...
derelict-fi 2.0.3: building configuration "library"...
derelict-fmod 2.0.4: building configuration "library"...
derelict-ft 1.1.3: building configuration "library"...
derelict-gl3 1.0.23: building configuration "library"...
derelict-glfw3 3.1.3: building configuration 
"derelict-glfw3-dynamic"...
00_01_print_ogl_ver ~master: building configuration 
"application"...

Linking...
Error: can't run 
'C:\Users\Administrator\Desktop\VC\\bin\x86_amd64\link.exe', 
check PATH

dmd failed with exit code -1.

C:\Users\kheaser\OneDrive for 
Business\GitHub\Delivery\projects\00_01_print_ogl_ver>where 
link.exe

C:\D\dmd2\windows\bin\link.exe

First off, I have no idea why dub (or dmd) is trying to use the 
path C:\Users\Administrator\Desktop\VC\\bin\x86_amd64\ to find 
link.exe?


I did the where command link.exe to show that the linker is in my 
PATH at C:\D\dmd2\windows\bin\.


So can I specify the linker path explicitly and if so, how is 
that done?


Sorry if this is a stupid question, but I've looked at the 
documentation all morning and not finding anything.  Thanks.






Re: std.stdio.stderr

2017-06-10 Thread Antonio Corbi via Digitalmars-d-learn

On Saturday, 10 June 2017 at 16:10:18 UTC, Russel Winder wrote:
It appears that std.stdio.stderr does not wor exactly as stdio 
stderr

does. In particular std.stdio.stderr.writef(…) does not work as
fprintf(stderr…) does.

Some code I am porting from C++ to D makes use of ANSI escape 
codes to go up a line and overwrite what was there, as well as 
change colours. This work fine in the C++ code but fails in the 
D code. The codes are definitely all the same, the only 
difference is in the writing functions.


Is this problem to be expected or should it work?


Hi Russel,

It seems to work for me with a dumb example:

```
import std.stdio;

void main()
{
  writefln("stdout: %s", "Edit source/app.d to start your 
project.");
  stderr.writefln("stderr: %s", "Edit source/app.d to 
start your project.");
  stderr.writefln("stderr: %s", "Edit source/app.d to 
start your project.");


  stderr.writefln("%s", "");
}
```
Before copy/paste take into account that in sequences like "[7m", 
etc... there's a hidden ESC char at the beginning, something 
like: "\033[7m", and I can't see it in the preview of my posting.


Antonio


when I run dub within git bash, dub prompts are not displayed. For instance...

2017-06-10 Thread WhatMeWorry via Digitalmars-d-learn
kheaser@IT-ASST-SB MINGW64 /c/Users/kheaser/Git/Delivery/projects 
(master)

$ dub init 00_01_print_ogl_ver



... All this white space here is me just pressing the Enter key
... to get the default values.



Package recipe format (sdl/json) [json]: Name 
[00_01_print_ogl_ver]: Description [A minimal D application.]: 
Author name [kheaser]: License [proprietary]: Copyright string 
[Copyright © 2017, kheaser]: Add dependency (leave empty to skip) 
[]: Successfully created an empty project in 
'C:\Users\kheaser\OneDrive for 
Business\GitHub\Delivery\projects\00_01_print_ogl_ver'.

Package successfully created in 00_01_print_ogl_ver

Any recommendations?

I guess I could just run two command lines in parallel. One 
regular Windows command line for dub and another git bash window 
for GIT commands?


Or should Git and Dub be kept firmly apart?




std.stdio.stderr

2017-06-10 Thread Russel Winder via Digitalmars-d-learn
It appears that std.stdio.stderr does not wor exactly as stdio stderr
does. In particular std.stdio.stderr.writef(…) does not work as
fprintf(stderr…) does.

Some code I am porting from C++ to D makes use of ANSI escape codes to
go up a line and overwrite what was there, as well as change colours.
This work fine in the C++ code but fails in the D code. The codes are
definitely all the same, the only difference is in the writing
functions.

Is this problem to be expected or should it work?

-- 
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: Need way to compare classes, and primitive types in bst Template

2017-06-10 Thread Mark via Digitalmars-d-learn

On Saturday, 10 June 2017 at 14:35:48 UTC, ag0aep6g wrote:
...
Just that you shouldn't take my version of addNode and rely on 
it without double checking that it's correct.


Ah, Okay. I rechecked that everything is working. The size is 
correct, and the membership is correct for every insert/delete.


Thanks again.


Re: Need way to compare classes, and primitive types in bst Template

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

On 06/10/2017 06:57 AM, Mark wrote:

On Friday, 9 June 2017 at 15:12:04 UTC, ag0aep6g wrote:
...
Note that this is only supposed to show how to do the special casing 
for classes. addNode probably doesn't do exactly what it's supposed to 
do in your tree.


  I'm not sure what you mean by this?


Just that you shouldn't take my version of addNode and rely on it 
without double checking that it's correct.


Re: Is D slow?

2017-06-10 Thread Nicholas Wilson via Digitalmars-d-learn
On Saturday, 10 June 2017 at 14:14:53 UTC, Steven Schveighoffer 
wrote:
I can confirm on my system, ldc with bounds check off was at 
least twice as slow as with just -release.


Definitely seems like a bug

-Steve


Ya, see https://github.com/ldc-developers/ldc/issues/2161


Re: Is D slow?

2017-06-10 Thread Steven Schveighoffer via Digitalmars-d-learn

On Saturday, 10 June 2017 at 13:43:48 UTC, Nicholas Wilson wrote:

On Saturday, 10 June 2017 at 12:44:07 UTC, Honey wrote:
On Saturday, 10 June 2017 at 12:23:05 UTC, Nicholas Wilson 
wrote:

[...]


Are you saying that introducing additional checks enables the 
optimizer to eliminate more or more costly checks than it 
could without introducing those additional checks in the first 
place? Can you give an example?


My bad I misread the original quote, misread that as 
performance increase.

turning bounds checks off should always result in faster code.


I can confirm on my system, ldc with bounds check off was at 
least twice as slow as with just -release.


Definitely seems like a bug

-Steve


Re: Is D slow?

2017-06-10 Thread Nicholas Wilson via Digitalmars-d-learn

On Saturday, 10 June 2017 at 12:44:07 UTC, Honey wrote:
On Saturday, 10 June 2017 at 12:23:05 UTC, Nicholas Wilson 
wrote:

On Saturday, 10 June 2017 at 12:16:34 UTC, Honey wrote:
Is it expected that turning off bounds checking can lead to a 
performance decrease?


Yes, with it on you are doing an "is the index <= the length" 
for every array access. Now some of them can be elided by the 
complier when it can prove that the index is always in bounds 
but it is generally dangerous to do so as it opens up the 
possibility of buffer overflow.


Are you saying that introducing additional checks enables the 
optimizer to eliminate more or more costly checks than it could 
without introducing those additional checks in the first place? 
Can you give an example?


My bad I misread the original quote, misread that as performance 
increase.

turning bounds checks off should always result in faster code.


Re: Is D slow?

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

On Saturday, 10 June 2017 at 12:23:05 UTC, Nicholas Wilson wrote:

On Saturday, 10 June 2017 at 12:16:34 UTC, Honey wrote:
Is it expected that turning off bounds checking can lead to a 
performance decrease?


Yes, with it on you are doing an "is the index <= the length" 
for every array access. Now some of them can be elided by the 
complier when it can prove that the index is always in bounds 
but it is generally dangerous to do so as it opens up the 
possibility of buffer overflow.


Are you saying that introducing additional checks enables the 
optimizer to eliminate more or more costly checks than it could 
without introducing those additional checks in the first place? 
Can you give an example?


Re: Is D slow?

2017-06-10 Thread Nicholas Wilson via Digitalmars-d-learn

On Saturday, 10 June 2017 at 12:16:34 UTC, Honey wrote:

On Saturday, 10 June 2017 at 11:53:44 UTC, Johan Engelen wrote:
`-release` should be synonymous with `-release 
-boundscheck=off`.


Nope it's not.
http://www.digitalmars.com/d/archives/digitalmars/D/What_s_the_deal_with_-boundscheck_260237.html


Thank you for clarifying that point. Is it expected that 
turning off bounds checking can lead to a performance decrease?


Yes, with it on you are doing an "is the index <= the length" for 
every array access. Now some of them can be elided by the 
complier when it can prove that the index is always in bounds but 
it is generally dangerous to do so as it opens up the possibility 
of buffer overflow.


Re: Is D slow?

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

On Saturday, 10 June 2017 at 11:53:44 UTC, Johan Engelen wrote:
`-release` should be synonymous with `-release 
-boundscheck=off`.


Nope it's not.
http://www.digitalmars.com/d/archives/digitalmars/D/What_s_the_deal_with_-boundscheck_260237.html


Thank you for clarifying that point. Is it expected that turning 
off bounds checking can lead to a performance decrease?


Re: Is D slow?

2017-06-10 Thread Honey via Digitalmars-d-learn
On Saturday, 10 June 2017 at 10:59:24 UTC, Steven Schveighoffer 
wrote:
I wrote it like this, which confirms that it's indeed 
bringToFront (I tried your getTransitionIndex function, but 
that didn't change timings at all):


void insertionSort(alias Less, Range)(Range r)
if (hasLength!Range && isRandomAccessRange!Range && 
hasSlicing!Range)

{
foreach (immutable i; 1 .. r.length)
{
auto ubElem = i - r[0 .. 
i].assumeSorted!Less.upperBound(r[i]).length;

r[ubElem .. i+1].rotateRight;
}
}


Taking the length of upperBound is a great move! ;-)


On my mac, it's completing in about 4.4 seconds, whereas the 
C++ version completes in around 3.


On my machine, I am getting the same performance even without 
resorting to memmove. Using getTransitionIndex directly, however, 
leads to a neglectable improvement (about 2.7 vs. 2.8 seconds), 
here.


Re: Is D slow?

2017-06-10 Thread Johan Engelen via Digitalmars-d-learn

On Saturday, 10 June 2017 at 11:43:06 UTC, Johan Engelen wrote:

On Friday, 9 June 2017 at 16:21:22 UTC, Honey wrote:
What seems particularly strange to me is that -boundscheck=off 
leads to a performance decrease.


Strange indeed.
`-release` should be synonymous with `-release 
-boundscheck=off`.


Nope it's not.
http://www.digitalmars.com/d/archives/digitalmars/D/What_s_the_deal_with_-boundscheck_260237.html


Re: Is D slow?

2017-06-10 Thread Johan Engelen via Digitalmars-d-learn

On Friday, 9 June 2017 at 16:21:22 UTC, Honey wrote:
What seems particularly strange to me is that -boundscheck=off 
leads to a performance decrease.


Strange indeed.
`-release` should be synonymous with `-release -boundscheck=off`.
Investigating...

- Johan



Re: Is D slow?

2017-06-10 Thread Steven Schveighoffer via Digitalmars-d-learn

On 6/10/17 5:00 AM, Honey wrote:

On Friday, 9 June 2017 at 23:10:28 UTC, Ali Çehreli wrote:

You would get the exact performance if you implemented e.g. with
pointers. Your test has been very valuable for exposing an
embarrassing performance issue. :)


I can confirm that, after changing implementation to the following,
performance is on par. It is not immediatetly clear to me how

 r[r[0 .. i].getTransitionIndex!Less(r[i]) .. i + 1]

would look like if written idiomatically.


I wrote it like this, which confirms that it's indeed bringToFront (I 
tried your getTransitionIndex function, but that didn't change timings 
at all):


void insertionSort(alias Less, Range)(Range r)
if (hasLength!Range && isRandomAccessRange!Range && hasSlicing!Range)
{
foreach (immutable i; 1 .. r.length)
{
auto ubElem = i - r[0 .. 
i].assumeSorted!Less.upperBound(r[i]).length;

r[ubElem .. i+1].rotateRight;
}
}

On my mac, it's completing in about 4.4 seconds, whereas the C++ version 
completes in around 3.


Optimized your rotateRight a bit to get better performance when we can 
use memmove:


void rotateRight(Range)(Range r)
if (hasLength!Range && isRandomAccessRange!Range && hasSlicing!Range)
{
auto t = r[$ - 1];
static if(isDynamicArray!Range)
{
import core.stdc.string;
memmove(r.ptr + 1, r.ptr, (r.length - 1) * r[0].sizeof);
}
else
{
foreach_reverse (i; 0 .. r.length - 1)
{
r[i + 1] = r[i];
}
}
r[0] = t;
}


Brings it to exactly c++ performance :)

I'll update the bug report.

-Steve


Re: What is the best way to include this file from the runtime libraries?

2017-06-10 Thread ulyssesdwolfe via Digitalmars-d-learn

On Saturday, 10 June 2017 at 10:07:54 UTC, rikki cattermole wrote:



You found it.


Ok Thanks :)



Re: What is the best way to include this file from the runtime libraries?

2017-06-10 Thread rikki cattermole via Digitalmars-d-learn

On 10/06/2017 11:06 AM, ulyssesdwolfe wrote:

Hi
I am trying to hook up an app to log to /var/log through the rsyslog 
daemon and

it seems I need this file
https://github.com/dlang/druntime/blob/master/src/core/sys/posix/syslog.d
but I am not sure what the correct way to include files from the runtime 
libs is. Right now I am just doing


import core.sys.posix.syslog;


You found it.


What is the best way to include this file from the runtime libraries?

2017-06-10 Thread ulyssesdwolfe via Digitalmars-d-learn

Hi
I am trying to hook up an app to log to /var/log through the 
rsyslog daemon and

it seems I need this file
https://github.com/dlang/druntime/blob/master/src/core/sys/posix/syslog.d
but I am not sure what the correct way to include files from the 
runtime libs is. Right now I am just doing


import core.sys.posix.syslog;


and that works but I was hoping to get clarified on what is the 
best method for this is?


ty



Re: Is D slow?

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

On Friday, 9 June 2017 at 23:10:28 UTC, Ali Çehreli wrote:
You would get the exact performance if you implemented e.g. 
with pointers. Your test has been very valuable for exposing an 
embarrassing performance issue. :)


I can confirm that, after changing implementation to the 
following, performance is on par. It is not immediatetly clear to 
me how


 r[r[0 .. i].getTransitionIndex!Less(r[i]) .. i + 1]

would look like if written idiomatically.


size_t getTransitionIndex(alias test, Range, V)(Range r, V v)
if (isRandomAccessRange!Range && hasLength!Range)
{
size_t first = 0;
size_t count = r.length;
while (count > 0)
{
immutable step = count / 2;
immutable it = first + step;
if (!test(v, r[it]))
{
first = it + 1;
count -= step + 1;
}
else
{
count = step;
}
}
return first;
}

void rotateRight(Range)(Range r)
if (hasLength!Range && isRandomAccessRange!Range && 
hasSlicing!Range)

{
   auto t = r[$ - 1];
   foreach_reverse (i; 0 .. r.length - 1)
   {
  r[i + 1] = r[i];
   }
   r[0] = t;
}

void insertionSort(alias Less, Range)(Range r)
if (hasLength!Range && isRandomAccessRange!Range && 
hasSlicing!Range)

{
   foreach (i; 1 .. r.length)
   {
  rotateRight(r[r[0 .. i].getTransitionIndex!Less(r[i]) .. i 
+ 1]);

   }
}