Re: Help with Ranges

2020-07-27 Thread Charles via Digitalmars-d-learn
On Monday, 27 July 2020 at 18:15:26 UTC, Steven Schveighoffer 
wrote:

On 7/27/20 1:10 PM, Charles wrote:


[...]


Let's talk about a concrete example, so you can see why:

int[] arr = [21, 83, 45, 60];

[...]


I had very incorrect model of how ranges function, and after 
reading your post along with map's source, I think I know how to 
proceed from here. I really appreciate your patience, and next 
time I'll clearly explain my constraints.


Thank you, gentlemen.


Re: Help with Ranges

2020-07-27 Thread Charles via Digitalmars-d-learn

On Monday, 27 July 2020 at 16:52:51 UTC, H. S. Teoh wrote:
On Sun, Jul 26, 2020 at 07:10:41AM +, Charles via 
Digitalmars-d-learn wrote:
Suppose I have the following line of code where arr is an 
array, doSomething is some predicate that does a lot of 
processing on each element, sort must come after the mapping, 
and there are more operations done to the range after sort:


arr.map!doSomething.sort. ...;

[...]

As Steven said, you cannot sort a range that doesn't support 
swapping elements, and most ranges cannot unless they're backed 
by actual storage, like an array. (*Something* has got to keep 
track of where the elements are, after all.)


But in my example, isn't the output of map backed by actual 
storage? After all, it's taking in an array.


In this particular case, though, if the contents of your 
original doesn't need to be preserved, perhaps .schwartzSort 
might be what you're looking for?


If you cannot modify the original array for whatever reason, 
then an allocation is probably unavoidable -- either you'll 
have to create an array of your mapped elements, or you could 
create an index and sort that instead (see .makeIndex or 
std.range.zip for different approaches).



T


I'll take a look at both of these, since I need to be aware of 
both cases. I'm trying to find the most efficient way of building 
a pipeline for my own betterment. Thank you.


Re: Help with Ranges

2020-07-27 Thread Charles via Digitalmars-d-learn
On Sunday, 26 July 2020 at 14:56:35 UTC, Steven Schveighoffer 
wrote:
A map that returns an lvalue would be sortable, but you would 
be sorting the processed elements, and probably not the 
original elements.


Indeed, but that's what I want: sort the process elements. 
Otherwise, I'd place sort before the transformations.


I have found this handy tool quite useful in my code where I 
need a temporary array:


// creates a concrete range (std.container.array.Array range) 
out of the
// original range that is eagerly fetched, and then can be 
processed, without

// allocating extra garbage on the heap.
auto concreteRange(Range)(Range r)
{
import std.range : ElementType;
import std.container.array : Array;
return Array!(ElementType!Range)(r)[];
}

Slicing an Array will keep the reference count correctly, and 
destroy the memory automatically after you're done using it. So 
it's perfect for temporary arrays in pipelining.


-Steve


This works well, and it's rather nifty. Still, I'm confused 
since, as far as I know, map wraps its source, i.e. the array in 
this case, which is sortable. It seems to me the only reason I 
can't sort MapResult is because it doesn't have the proper 
interface.


I'm obviously missing something.


Help with Ranges

2020-07-26 Thread Charles via Digitalmars-d-learn
Suppose I have the following line of code where arr is an array, 
doSomething is some predicate that does a lot of processing on 
each element, sort must come after the mapping, and there are 
more operations done to the range after sort:


arr.map!doSomething.sort. ...;

Sort fails to instantiate because the range it's receiving 
doesn't support element swapping. This may and might be resolved 
by calling array:


arr.map!doSomething.array.sort. ...;

However, this might trigger an allocation, and there's still more 
to do! Is there something I'm missing with regards to ranges that 
could help me make the first line work without using array, or is 
it more of an issue with my code's design?


Re: Setup help?

2019-02-06 Thread Charles via Digitalmars-d-learn

On Thursday, 7 February 2019 at 02:55:15 UTC, evilrat wrote:
You need C++ tools from Microsoft to debug D code, don't mind 
the name, its debugger works for any (compatible formats) 
native code.


Then add C++ Windows debug configuration and set your paths. 
Done. You can debug now. (Though it is possible that it will 
require Visual Studio Build Tools installation)


Of course this will not work for default DMD builds because it 
is using ancient object files format that is not compatible 
with VS debugger engine, so using DMD you need to build with 
-m32mscoff (dub --arch=x86_mscoff) or -m64 (dub --arch=x86_64) 
flags.


Thanks, I'll give it a shot.


Setup help?

2019-02-06 Thread Charles via Digitalmars-d-learn
Does anyone know of a video that shows setting up vscode (or 
another editor with debugging support)? I always feel like I miss 
a step when I decide to try D out again, and it never ends well.


I don't use C++, and I do use Windows, which has me wondering if 
I'm just missing some normal/exepcted configuration.


My most recent attempt I tried to get Native Debug to make VS 
Code debugging stop on the first line. Instead, it just runs the 
program, and exits.


Debug console output:

No symbol table is loaded.  Use the "file" command.
Running executable
[New Thread 10256.0x23d8]
[New Thread 10256.0x2d30]
[New Thread 10256.0xebc]
[New Thread 10256.0x19f4]
Edit source/app.d to start your project.
[Thread 10256.0xebc exited with code 0]
[Thread 10256.0x2d30 exited with code 0]
[Thread 10256.0x19f4 exited with code 0]

Thanks


Re: In language tooling

2016-03-04 Thread Charles via Digitalmars-d-learn

On Wednesday, 2 March 2016 at 03:41:57 UTC, sigod wrote:


Very interesting. I wonder what Walter would say about it.


Yeah, I'm curious what others' thoughts on it are for sure.


In language tooling

2016-03-02 Thread Charles via Digitalmars-d-learn
Watched a video on Jonathan Blow's language that he's developing, 
and he has a pretty neat idea of having tools being part of the 
language. Looking at the first 15 
minutes(https://www.youtube.com/watch?v=OHZwYYW9koI) or so of the 
video, is this something that could be accomplished in D with 
CTFE?


I think he makes a decent case for whether or not it'd be useful.


Re: Is this a good singleton?

2016-02-13 Thread Charles via Digitalmars-d-learn

On Saturday, 13 February 2016 at 19:32:33 UTC, Ali Çehreli wrote:
David Simcha's DConf 2013 presentation has a singleton 
implementation at 27:55:


  https://www.youtube.com/watch?v=yMNMV9JlkcQ

Ali


Neat video! Watched the singleton section to end up watching the 
rest of the video. Anything every come of the std.patterns idea?


Re: Questions about vibe.d

2016-02-12 Thread Charles via Digitalmars-d-learn
On Friday, 12 February 2016 at 12:31:57 UTC, Guillaume Piolat 
wrote:

1. Can vibe.d handle HTTPS connections?

2. Can vibe.d "rewrite" HTTP connections to HTTPS?

3. Can vibe.d be put behind a nginx reverse proxy?

4. Can vibe.d send mails?




1. Yes. Example: 
https://github.com/rejectedsoftware/vibe.d/tree/master/examples/https_server
2. I'd do this with nginx. Example: 
http://serverfault.com/a/337893

3. Yes.
4. Yes. Example: 
https://github.com/rejectedsoftware/vibe.d/tree/master/examples/sendmail


Sorry if these questions are a bit basic, the implied subtext 
is "and does it work well?".


Yes, also Sönke actively works one Vibe almost (if not) every 
single day, so it's also exceptionally well maintained.


Re: Questions about vibe.d

2016-02-12 Thread Charles via Digitalmars-d-learn
On Friday, 12 February 2016 at 14:36:18 UTC, Ola Fosheim Grøstad 
wrote:
On Friday, 12 February 2016 at 12:31:57 UTC, Guillaume Piolat 
wrote:
Sorry if these questions are a bit basic, the implied subtext 
is "and does it work well?".


Just in case you didn't know, browsers now support HTTP/2 (and 
SPDY)...


https://en.wikipedia.org/wiki/HTTP/2


Vibe.d doesn't though. There's a branch for it here: 
https://github.com/rejectedsoftware/vibe.d/tree/http2-botan-cleanup, but it still has a good bit of work.


algorithm's .filter!() by range key

2016-02-09 Thread Charles via Digitalmars-d-learn
This seems to be true of any range function really... is there a 
way to access the key within my range?


Example of what I want to do:

auto x = [1,2,3,4,5];
x.filter( x_key % 2 == 1 ).sum(); // sum odd elements in array


Re: algorithm's .filter!() by range key

2016-02-09 Thread Charles via Digitalmars-d-learn

On Tuesday, 9 February 2016 at 20:44:34 UTC, cym13 wrote:

On Tuesday, 9 February 2016 at 20:40:44 UTC, Charles wrote:
This seems to be true of any range function really... is there 
a way to access the key within my range?


Example of what I want to do:

auto x = [1,2,3,4,5];
x.filter( x_key % 2 == 1 ).sum(); // sum odd elements in array


x.filter!(x_key => x_key % 2 == 1).sum();


Oh man, I really messed up my example, and did a poor one at that.

Better example:

auto x = [2,4,6,8,10];
x.filter( x_key => x_key % 2 == 1 ).sum(); // sums 2 + 6 + 10 == 
18


Re: algorithm's .filter!() by range key

2016-02-09 Thread Charles via Digitalmars-d-learn
On Tuesday, 9 February 2016 at 20:48:01 UTC, Steven Schveighoffer 
wrote:

On 2/9/16 3:40 PM, Charles wrote:
This seems to be true of any range function really... is there 
a way to

access the key within my range?

Example of what I want to do:

auto x = [1,2,3,4,5];
x.filter( x_key % 2 == 1 ).sum(); // sum odd elements in array


An array is not an indexed range. It only works with foreach by 
key because of special foreach behavior.


What you want is std.range.enumerate



Exactly! Thanks!

Interestingly, hackerrank doesn't seem to have it. They're using 
2.067.0-b1 on Ubuntu 14.04.




Re: Build utf.d with MS-COFF failed

2015-11-20 Thread Charles via Digitalmars-d-learn

On Friday, 20 November 2015 at 21:27:12 UTC, Pierre wrote:

Hello, I can't build my project with MS-COFF option.

I'm using DMD 2.069.

I got this error :
utf.d(1109) : invalid UTF-8 sequence (at index 1)

I used these options with visual D:
Compiler   : DMD
D-Version  : D2
Output Type: DLL
Subsystem  : Not set

Compilation: Combined compile and link

Thank you for your help.


What string is throwing the error? I've had that error when I was 
trying to decode windows1252 strings.


Am I using std.encoding correctly?

2015-11-14 Thread Charles via Digitalmars-d-learn
I have some binary files that I'm reading. At compile time it's 
unknown what types I'm reading, and if they're strings, how it's 
encoded.


I'm doing something like this:

Variant value;
switch(type)
{
...
case Type.STRING:
value = cast(dchar[])[];
const(ubyte)[] buffer = raw[0 .. length];
while (buffer.length > 0)
value ~= encodingScheme.decode(buffer);
break;
...
}

I know there's safeDecode, but I'm also fairly confident that all 
strings can decode safely, already, and if it isn't I'd want an 
exception thrown from it.


Is this correct usage? I guess I was a little surprised there was 
no decodeString that basically did this.


Re: question about using std.bitmanip.read

2015-11-06 Thread Charles via Digitalmars-d-learn

On Saturday, 7 November 2015 at 04:25:00 UTC, Mike Parker wrote:

Missed this in my previous reply.


No problem. I appreciate you taking the time to help me either 
way :)


Re: question about using std.bitmanip.read

2015-11-06 Thread Charles via Digitalmars-d-learn
On Saturday, 7 November 2015 at 03:53:14 UTC, Nicholas Wilson 
wrote:

On Saturday, 7 November 2015 at 03:19:44 UTC, Charles wrote:

Hi guys,

It's me again... still having some issues pop up getting 
started, but I remain hopeful I'll stop needing to ask so many 
questions soon.


I'm trying to use std.bitmanip.read; however, am having some 
issues using it. For basic testing I'm just trying to use:


read!double(endianess, ubyteArr).writeln;

endianess is an Endian from std.system, and ubyteArr is an 8 
byte ubyte[].


When I run this I get:

Error: template std.bitmanip.read cannot deduce function from 
argument types !(double)(Endian, ubyte[]), candidates are:
std.bitmanip.read(T, Endian endianness = Endian.bigEndian, 
R)(ref R range) if (canSwapEndianness!T && isInputRange!R && 
is(ElementType!R : const(ubyte)))

dmd failed with exit code 1.


Clearly that didn't work, so I tried excluding the endianess:

read!double(ubyteArr).writeln;

and that does work! But its the wrong byte order, so its 
incorrect anyways.


I went to std.bitmanip to look for unittests using the Endian, 
and the only one that does uses read!(T, endianness), which 
needs endianness to be known at compile time, which I don't 
have.


Any suggestions?


Cheat!

T read(T,R)(Endian endianness , R r)
{
 if(endianness == Endian.bigEndian)
return 
std.bitmanip.read!(T,Endian.bigEndian,R)(r);

 else if (endianness == Endian.littleEndian)
return 
std.bitmanip.read!(T,Endian.littleEndian,R)(r);

}


Thanks!


but...
you are on a little endian system (bigEndian gave wrong byte 
order )


The actual use case is reading a binary file of unknown 
endianness. I don't think I'm that fortunate sadly.


question about using std.bitmanip.read

2015-11-06 Thread Charles via Digitalmars-d-learn

Hi guys,

It's me again... still having some issues pop up getting started, 
but I remain hopeful I'll stop needing to ask so many questions 
soon.


I'm trying to use std.bitmanip.read; however, am having some 
issues using it. For basic testing I'm just trying to use:


read!double(endianess, ubyteArr).writeln;

endianess is an Endian from std.system, and ubyteArr is an 8 byte 
ubyte[].


When I run this I get:

Error: template std.bitmanip.read cannot deduce function from 
argument types !(double)(Endian, ubyte[]), candidates are:
std.bitmanip.read(T, Endian endianness = Endian.bigEndian, 
R)(ref R range) if (canSwapEndianness!T && isInputRange!R && 
is(ElementType!R : const(ubyte)))

dmd failed with exit code 1.


Clearly that didn't work, so I tried excluding the endianess:

read!double(ubyteArr).writeln;

and that does work! But its the wrong byte order, so its 
incorrect anyways.


I went to std.bitmanip to look for unittests using the Endian, 
and the only one that does uses read!(T, endianness), which needs 
endianness to be known at compile time, which I don't have.


Any suggestions?


Re: Unittest in a library

2015-11-06 Thread Charles via Digitalmars-d-learn

On Friday, 6 November 2015 at 04:34:28 UTC, TheFlyingFiddle wrote:

On Friday, 6 November 2015 at 03:59:07 UTC, Charles wrote:
Is it possible to have unittest blocks if I'm compiling a 
library?


I've tried having this:

test.d:

class Classy {
unittest { assert(0, "failed test"); }
}


and then build it with `dmd test.d -lib -unittest` and it 
doesn't fail the unittest.


You can test the unittests by using the -main switch.
http://dlang.org/dmd-linux.html#switch-main


That's exactly what I was missing. Thanks!



Unittest in a library

2015-11-05 Thread Charles via Digitalmars-d-learn

Is it possible to have unittest blocks if I'm compiling a library?

I've tried having this:

test.d:

class Classy {
unittest { assert(0, "failed test"); }
}


and then build it with `dmd test.d -lib -unittest` and it doesn't 
fail the unittest.


Re: ODBC Library?

2015-10-31 Thread Charles via Digitalmars-d-learn

On Monday, 10 November 2014 at 20:37:51 UTC, Charles wrote:


For anyone in the future: I needed odbc32.lib, so I created the 
following odbc32.def and used implib.


Thanks me.

My computer I was using recently died, and ran into this problem 
again when getting everything set up. Is there any way to just 
fix this issue altogether?


What's the "right" way to do openmp-style parallelism?

2015-09-06 Thread Charles via Digitalmars-d-learn

Friends,

I have a program that would be pretty easy to parallelize with an 
openmp pragra in C. I'd like to avoid the performance cost of 
using message passing, and the shared qualifier seems like it's 
enforcing guarantees I don't need. Essentially, I have


x = float[imax][jmax]; //x is about 8 GB of floats
for(j = 0; j < jmax; j++){
//create some local variables.
for(i = 0; i < imax; i++){
x[j][i] = complicatedFunction(i, x[j-1], other, local, 
variables);

}
}

In C, I'd just stick a #pragma omp parallel for around the inner 
loop (since the outer loop obviously can't be parallelized).


How should I go about this in D? I want to avoid copying data 
around if it's possible since these arrays are huge.


Cheers,
Charles.


Re: Win32 bindings

2015-03-31 Thread Charles via Digitalmars-d-learn

http://www.dsource.org/projects/bindings/wiki/WindowsApi


Thanks for this! I guess my brain was wrong thinking it'd be in 
http://code.dlang.org if it was still being maintained.


For some functions, you'll need import libraries. You can get 
them from the same project as in the above link, create your 
own from .def files, or use implib/coffimplib.


Are the default libraries in dmd2\windows\lib not current or 
something?


Win32 bindings

2015-03-31 Thread Charles via Digitalmars-d-learn

Hi guys,

What is the best (and/or official) source for win32 bindings?

I know there's this github project: 
https://github.com/AndrejMitrovic/DWinProgramming; however, it 
hasn't been touched in about 2 years. It's currently linked on 
the wiki (http://wiki.dlang.org/D_for_Win32).


I'm also aware that there's core.sys.windows.windows, but the 
documentation for this page is less than helpful: 
http://dlang.org/library/core/sys/windows/windows.html. It also 
seems to be missing portions of it that'd be useful (see github 
here: 
https://github.com/D-Programming-Language/druntime/tree/master/src/core/sys/windows).


So, perhaps, a better question is what do you use for win32 
bindings? Are there any additional dependencies when building 
apps with either of these (beyond something like user32.dll for 
example)?


Thanks,
Charles


Re: curl password issue

2015-02-23 Thread Charles via Digitalmars-d-learn

On Tuesday, 24 February 2015 at 00:20:45 UTC, Martin Nowak wrote:

On Monday, 23 February 2015 at 16:10:42 UTC, Andre wrote:

Curl has some issues with passwords containing special 
characters

like the hash key (#).


I don't found any reference for this issue in curl and the D 
wrapper hardly adds anything. You're sure it isn't an issue 
with your program or how you pass the password?


This might be the case here. I was recently dealing with this and 
I had to percent encode the hash key (%23 for reference). Also, I 
ran into issues regarding the http_proxy and https_proxy 
environment variables, but again those are unrelated to curl.d.


Re: vibe-d basic build errors

2015-02-22 Thread Charles via Digitalmars-d-learn

On Friday, 20 February 2015 at 14:36:47 UTC, MartinNowak wrote:

On Friday, 20 February 2015 at 04:48:09 UTC, Charles wrote:

They're installer versions, dub is 0.9.22 Nov 22 I want to say,
and DMD is 2.066.1


Same ones I tried.

With --force dmd seems to fail but there is not output.
Can you run only the link command with a -v to get verbose dmd 
output?


dmd -c 
-of.dub\build\application-debug-windows-x86-dmd_2066-7FF336D92D4F5796EA8623FC9A6A9B90\web.obj 
-debug -g -w -version=VibeDefaultMain -version=VibeWin32Driver 
-version=Have_web -version=Have_vibe_d -version=Have_openssl 
-Isource 
-IC:\Users\Charles\AppData\Roaming\dub\packages\vibe-d-0.7.22\source 
-IC:\Users\Charles\AppData\Roaming\dub\packages\openssl-1.1.3_1.0.1g 
-Jviews source\app.d 
C:\Users\Charles\AppData\Roaming\dub\packages\vibe-d-0.7.22\source\vibe\appmain.d


Thanks for the help with this. The link command ran without 
error, and it's output is here: http://pastebin.com/eqT4vst6


To be exact, the command I ran was this:  dmd -v -c 
-of.dub\build\application-debug-windows-x86-dmd_2066-7FF336D92D4F5796EA8623FC9A6A9B90\web.obj 
-debuenssl -Isource 
-IC:\Users\Charles\AppData\Roaming\dub\packages\vibe-d-0.7.22\source 
-IC:\Users\Charles\AppData\-d-0.7.22\source\vibe\appmain.d  
%userprofile%\Desktop\output.txt


It didn't produce any errors as far as I can tell, but I'm not 
sure how to tell what the mangled function names are.


Sorry about the delay in getting back to you on this.

Charles



Re: vibe-d basic build errors

2015-02-19 Thread Charles via Digitalmars-d-learn

On Friday, 20 February 2015 at 02:55:32 UTC, MartinNowak wrote:

On Friday, 20 February 2015 at 02:50:05 UTC, Charles wrote:

Pastebin of dub --vverbose: http://pastebin.com/4BcHJM74



Target vibe-d 0.7.22 is up to date. Use --force to rebuild.

Have you tried the --force switch to rebuild vibe.d?

Looks like the existing vibe.d lib was build against a different
build of druntime.


Yes, I have. Here's a pastebin with --force --vverbose: 
http://pastebin.com/qZEKUN46


Re: vibe-d basic build errors

2015-02-19 Thread Charles via Digitalmars-d-learn

On Friday, 20 February 2015 at 04:13:08 UTC, MartinNowak wrote:

On Friday, 20 February 2015 at 04:00:21 UTC, Charles wrote:
Yes, I have. Here's a pastebin with --force --vverbose: 
http://pastebin.com/qZEKUN46


Just tried the dub init web vibe.d  cd web  dub thing, 
works for me.

So the most interesting questions.

- What version of dub and dmd are you using?
- Are those installer versions or did you build anything 
yourself?



They're installer versions, dub is 0.9.22 Nov 22 I want to say,
and DMD is 2.066.1


vibe-d basic build errors

2015-02-19 Thread Charles via Digitalmars-d-learn

Hi,

I'm trying to follow the instructions for vibe-d with:

dub init web vibe.d
cd web
dub

and then add the line subConfigurations: {vibe-d: win32} 
to the dub.json file.


This however is producing errors during linking. Could I get a 
hand?


Pastebin of dub --vverbose: http://pastebin.com/4BcHJM74

Thanks,
Charles


Re: Issue with template function

2015-02-07 Thread Charles via Digitalmars-d-learn

On Friday, 6 February 2015 at 17:40:31 UTC, ketmar wrote:

On Fri, 06 Feb 2015 17:09:28 +, Charles wrote:


 readString(toBytes!string(test),0,4).writeln;


if you'll take a look into druntime sources, you'll find that 
string is
just an alias to `immutable(char)[]`. so you actually doing 
thing:


  readString(toBytes!(immutable(char)[])(test),0,4).writeln;

i bet that this is not what you meant. ;-)


Thanks!


Re: Issue with template function

2015-02-07 Thread Charles via Digitalmars-d-learn
On Saturday, 7 February 2015 at 12:04:12 UTC, Nicholas Wilson 
wrote:
Are you wanting to to convert each element in arr to a byte 
thus truncating and losing data (when T.sizeof != 1)?

as in
toBytes([1,2,3, 42, 500 /*this will be truncated to 244 
*/]);// T  == int here
or are you wanting to convert each element to a ubyte array and 
then concatenate it to the result.

as is
 ubyte[] toBytes(T)(T[] arr)
 {
 ubyte[T.sizeof] buf;
 if (arr is null)
 {
 return null;
 }

 ubyte[] result = new ubyte[arr.length * T.sizeof];

 foreach (i, val; arr)
 {
 buf[] = cast(ubyte[T.sizeof])(arr[i])[0 .. 
T.sizeof]

 result ~= buf;
 }

 return result;
 }
?


The original code I was using was written in Java, and only had a 
method for strings. This is closer to what I wanted. My unit 
tests were just going back and forth with readString function, so 
I was completely missing this for other types. Nice catch!


There were a couple issues with your code so I've included the 
corrected version:


ubyte[] toUbytes(T)(T[] arr)
{
if (arr is null)
{
return null;
}

ubyte[T.sizeof] buffer;
ubyte[] result = new ubyte[arr.length * T.sizeof];

foreach (i, val; arr)
{
buffer[] = cast(ubyte[T.sizeof])((arr[i]))[0 .. 
T.sizeof]; // Parenthesis and missing semicolon
result[i * T.sizeof .. (i * T.sizeof) + T.sizeof] = 
buffer; // Specify appropriate slice for buffer to be inserted 
into

}

return result;
}


Issue with template function

2015-02-06 Thread Charles via Digitalmars-d-learn
I'm trying to create a template function that can take in any 
type of array and convert it to a ubyte array. I'm not concerned 
with endianness at the moment, but I ran into a roadblock when 
trying to do this with strings. It already works with ints, 
chars, etc.


Here's the relevant test code:

module byteReader;

public import std.system : Endian;

ubyte[] toBytes(T)(T[] arr)
{
if (arr == null)
{
return null;
}

ubyte[] result = new ubyte[arr.length];

foreach (key, val; arr)
{
result[key] = cast(ubyte) val;// This is line 16
}

return result;
}

string readString(ubyte[] buffer, uint offset, uint length)
{
assert( buffer.length = offset + length );

char[] chars = new char[length];
foreach(key, val; buffer[offset .. offset + length])
{
chars[key] = cast(char) val;
}

return cast(string)chars;

}

void main() {
import std.stdio;
readString(toBytes!char(['t','e','s','t']),0,4).writeln;
readString(toBytes!string(test),0,4).writeln;// 
This is line 39

}

Here's the output:
byteReader.d(16): Error: cannot cast val of type string to 
type ubyte
byteReader.d(39): Error: template instance 
byteReader.toBytes!string error instantiating


Re: Issue with template function

2015-02-06 Thread Charles via Digitalmars-d-learn

Can I not do this cast because it's immutable?


Question about Vectors

2014-11-20 Thread Charles via Digitalmars-d-learn
So I was reading the documentation page: 
http://dlang.org/simd.html and noticed what appears to be a typo:


int4 v;
(cast(int*)v)[3] = 2;   // set 3rd element of the 4 int vector
(cast(int[4])v)[3] = 2;  // set 3rd element of the 4 int vector
v.array[3] = 2;  // set 3rd element of the 4 int vector
v.ptr[3] = 2;// set 3rd element of the 4 int vector

v.array[3] = 2; and v.ptr[3] = 2; set the fourth element, and not 
the third.


As I was verifying this, I realized I had to compile it in 64 bit 
code. The 32 bit code produced the error SIMD vector types not 
supported on this platform.


My test code is:

void main() {
import std.stdio;
import core.simd;

int4 v = 7;
v.ptr[3] = 2;
writeln(v.array[]);
}


Is that related to me compiling while using a 64 bit OS, or is 
that true of any 32 bit OS, and thus, vectors can't be used in 
programs intended to be run on 32 bit OSs?


Thanks,
Charles


ODBC Library?

2014-11-10 Thread Charles via Digitalmars-d-learn

Hi guys,

I've been looking and haven't found any libraries for ODBC or 
MSSQL. I saw some for D v1, but nothing for v2. Anyone know of 
any, or anyone know of a tutorial that I could use to create this 
myself?


Thanks,
Charles


Re: ODBC Library?

2014-11-10 Thread Charles via Digitalmars-d-learn

I kinda slapped one together but idk if it actually works.

https://github.com/adamdruppe/arsd

database.d and mssql.d from that repo. I haven't even tried to 
compile it for a while though, so it might not work at all.


The way I made it was to write the extern(C) function 
declarations and then call it like in C.


It didn't compile, says, mssql.d(12): Error: module sql is in 
file 'win32\sql.d' which cannot be read and then lists the 
import paths, of which none of them have the win32 directory. 
Database.d was able to compile though.


Assuming you're using ODBC on Windows, here's an old port of an 
even older C++ wrapper I used to use for ODBC work.  It 
includes an ODBC header and library, so should serve as a good 
basis for whatever you're trying to do.  If you're on Unix, you 
may have to update the ODBC header a bit.  I got partway 
through that project back in the day but never finished: 
http://invisibleduck.org/sean/tmp/sql.zip


I'll look into this, thanks for the example.


Re: ODBC Library?

2014-11-10 Thread Charles via Digitalmars-d-learn

On Monday, 10 November 2014 at 18:13:58 UTC, Adam D. Ruppe wrote:

On Monday, 10 November 2014 at 17:57:21 UTC, Charles wrote:
It didn't compile, says, mssql.d(12): Error: module sql is in 
file 'win32\sql.d' which cannot be read


Oh, I forgot I used those. You can download the win32 folder 
from here 
https://github.com/AndrejMitrovic/DWinProgramming/tree/master/WindowsAPI


The Windows bindings that come with phobos are pathetically 
incomplete so a separate download or a bunch of copy/pasted 
declarations is needed for any serious windows api work.


Thanks for that.

For anyone in the future: I needed odbc32.lib, so I created the 
following odbc32.def and used implib.


LIBRARY odbc32
EXETYPE NT
SUBSYSTEM WINDOWS
EXPORTS
_SQLAllocEnv@4 = SQLAllocEnv
_SQLAllocConnect@8 = SQLAllocConnect
_SQLAllocHandle@12 = SQLAllocHandle
_SQLColAttribute@28 = SQLColAttribute
_SQLConnect@28 = SQLConnect
_SQLDisconnect@4 = SQLDisconnect
_SQLDescribeCol@36 = SQLDescribeCol
_SQLDriverConnect@32 = SQLDriverConnect
_SQLDrivers@32 = SQLDrivers
_SQLDataSources@32 = SQLDataSources
_SQLExecDirect@12 = SQLExecDirect
_SQLFetch@4 = SQLFetch
_SQLFreeConnect@4 = SQLFreeConnect
_SQLFreeHandle@8 = SQLFreeHandle
_SQLFreeEnv@4 = SQLFreeEnv
_SQLEndTran@12 = SQLEndTran
_SQLFreeStmt@8 = SQLFreeStmt
_SQLGetData@24 = SQLGetData
_SQLGetDiagField@28 = SQLGetDiagField
_SQLGetDiagRec@32 = SQLGetDiagRec
_SQLGetInfo@20 = SQLGetInfo
_SQLNumResultCols@8 = SQLNumResultCols
_SQLSetConnectOption@12 = SQLSetConnectOption
_SQLSetEnvAttr@16 = SQLSetEnvAttr
_SQLSetStmtOption@12 = SQLSetStmtOption

I've only tested it on a couple select statements and the 
fieldNames, but so far its working.


Possible difference in compilers?

2014-08-31 Thread Charles via Digitalmars-d-learn

Hi everyone

So I've been working on the problems over at HackerRank.com
trying to gain some familiarity with D. I use a Windows computer
with VisualD, but the server used to test the program uses Ubuntu
(I can't tell which compiler they're actually using).

The problem I'm stuck on now is the Utopian Tree
(https://www.hackerrank.com/challenges/utopian-tree).

My solution I came up with (and works locally) is:

import std.stdio;
void main() {
 int height=1,t=1,oldN=0,n;
 readf( %d\n, t);
 foreach (i;0 .. t) {
 readf( %d\n, n);
 foreach (j; oldN .. n)
 height = (j % 2) ? height + 1 : height * 2;
 writeln(height);
 oldN = n;
 }
}

For the test cases this only produces the first output
(correctly), but then hits a compiler error with format.d before
the next one. Any ideas what might be going on?

Thanks,
Charles


Re: Possible difference in compilers?

2014-08-31 Thread Charles via Digitalmars-d-learn

For the test cases this only produces the first output
(correctly), but then hits a compiler error with format.d before
the next one. Any ideas what might be going on?



Figured it out. The issue was the \n character at the end of the 
readf statements.