Re: cURL alternative for enable javascript

2015-09-03 Thread Rikki Cattermole via Digitalmars-d-learn

On 04/09/15 2:09 AM, xky wrote:

hello! :)

Well.. yes. cURL couldn't enable javascript.
How can i use other library alternative cURL for D ?

regards,


Curl is meant to transfer data and in this case, download files over 
http. It is not meant to render them via JavaScript.


If you are wanting rendering which by the sounds you are by JavaScript 
then you are entering complicated and not so nice area of programming.


You may get what you want by e.g. PhantomJS[0], of course you could set 
it up so that it could do what ever you want externally to your app. Of 
course then you would be limited to another process/files required.


This problem is not limited to D. Even in C++ you would have to think 
twice about doing this.


Of course somebody may know a better way.

[0] http://phantomjs.org/


Re: Casting away immutability

2015-09-03 Thread Mike Parker via Digitalmars-d-learn
On Thursday, 3 September 2015 at 13:28:54 UTC, Sergei Degtiarev 
wrote:


Agree, however, memory obtained with mmap(..., PROT_READ, ..); 
is essentially read-only and any attempt to write to it will 
cause immediate crash with segmentation violation. It would be 
very desirable in this case to return something that could be 
cast to immutable(type)[] but not type[].

This is what I tried to find out. Thank you for the help.


immutable(T)[] getGetData(T)() {
return cast(immutable(T)[])data;
}

...

auto ints = obj.getData!int;


cURL alternative for enable javascript

2015-09-03 Thread xky via Digitalmars-d-learn

hello! :)

Well.. yes. cURL couldn't enable javascript.
How can i use other library alternative cURL for D ?

regards,


Re: interprocess communication and sharing memory

2015-09-03 Thread Justin Whear via Digitalmars-d-learn
On Thu, 03 Sep 2015 01:27:14 +, j55 wrote:

> I've read many posts about shared memory and interprocess communication
> in D, but I didn't see any conclusive information about whether this
> type of interprocess memory sharing will be convenient or practical in
> D.  If it doesn't work out, I have some prototype already working in c++
> with boost interprocess, but I'd prefer to use a native D solution if
> possible.

I really like ZeroMQ for messaging--it scales transparently from IPC to 
TCP and packages lots of smart topologies for you.  There's a D wrapper 
here: https://github.com/kyllingstad/zmqd

For shared memory, it's pretty easy to do with std.mmfile.


Working Windows GUI library?

2015-09-03 Thread Andre Polykanine via Digitalmars-d-learn
Hi everyone,
Does   anyone   of   you  work  with a Windows GUI library with native
controls in order to write desktop apps in D?
Here  is  why  I'm  asking:  actually, there are quite a number of GUI
libraries listed at wiki.dlang.org.
However, I have one specific requirement: the resulting apps should be
accessible  for blind and visually impaired users. To do that, Windows
native  controls are highly preferred since they are treated correctly
by  screen  reading  software.  So  no  QT,  no GTK (they are both not
accessible under Windows).
I've  tried  to  build  samples for each library, and got virtually no
results:  DGui was the only one that caused no problems at all, but it
has   almost   no   documentation   and   most   of  the  samples  are
drawing-related.
So  my  question  is:  is  there any reliable GUI library implementing
native Windows controls?
Thanks!
  

-- 
With best regards from Ukraine,
Andre
Skype: Francophile
Twitter: @m_elensule; Facebook: menelion
My blog: http://menelion.oire.org/



Re: Casting away immutability

2015-09-03 Thread Sergei Degtiarev via Digitalmars-d-learn

On Thursday, 3 September 2015 at 14:36:12 UTC, Mike Parker wrote:


immutable(T)[] getGetData(T)() {
return cast(immutable(T)[])data;
}



Absolutely, instead of returning raw void[] and allow user to 
cast it, std.mmfile should implement template function to return 
desired type.
This would allow all necessary checks inside the module, 
returning pure result.




Re: Working Windows GUI library?

2015-09-03 Thread Andre Polykanine via Digitalmars-d-learn
Hello Adam,

ADRvDdl> Easily usable by the blind or people with motor difficulties and
ADRvDdl> other similar challenges.

Exactly, thank you.
I.e.,   the   app   should   be usable without mouse and with a screen
reader (to over-simplify the
things).

-- 
With best regards from Ukraine,
Andre
Skype: Francophile
Twitter: @m_elensule; Facebook: menelion
My blog: http://menelion.oire.org/


 Original message 
From: Adam D. Ruppe via Digitalmars-d-learn 
To: digitalmars-d-learn@puremagic.com
Date created: , 7:56:52 PM
Subject: Working Windows GUI library?


  On Thursday, 3 September 2015 at 16:49:51 UTC, BBasile wrote:
> I don't know what you meant by 'accessible'

Easily usable by the blind or people with motor difficulties and 
other similar challenges.



Re: Why isn't int[] automatically convertible to long[]?

2015-09-03 Thread Adam D. Ruppe via Digitalmars-d-learn
On Thursday, 3 September 2015 at 17:27:03 UTC, Jack Stouffer 
wrote:

pragma(msg, is(int[] : long[]));

false
Why?


Think of the memory layout... if you implicitly casted, either 
the contents would change or it would need to allocate a new 
array, neither of which is free.


[0, 1] as int[] in memory is like [0,0,0,0,0,0,0,1].

[0, 1] as long[] in memory is like 
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1].



It is twice as long! If you casted without the reallocation, 
[0,1] as int[] becomes just plain [1] as long[] - the length 
changes as well as the contents.


Why isn't int[] automatically convertible to long[]?

2015-09-03 Thread Jack Stouffer via Digitalmars-d-learn

pragma(msg, is(int[] : long[]));

false


Why?


Re: Can we get a video tutorial?

2015-09-03 Thread Stephen via Digitalmars-d-learn

On Wednesday, 2 September 2015 at 01:15:28 UTC, Mike Parker wrote:

* What was previously said *


Ok, so, I am running Windows 10, I have installed VS 2015, I have 
installed DMD 1 and 2 (I know I only need 2 but it shouldn't hurt 
to have both), I have installed VisualD, and I have installed DUB 
with it's installer. (All of these installed successfully and 
work)


So, I'm trying to install gtkD but get "Error: module MainWindow 
is in file 'gtk/MainWindow.d' which cannot be read" when I try to 
run the program. No idea what this means, how to fix it, or what 
step I missed.


What I'd like is for someone to either make a tut of how to get 
from no Dlang on their computer to having a package working in a 
program, or for someone to help me and I'll try to make one later.


Re: Can we get a video tutorial?

2015-09-03 Thread wobbles via Digitalmars-d-learn

On Thursday, 3 September 2015 at 19:13:35 UTC, Stephen wrote:
On Wednesday, 2 September 2015 at 01:15:28 UTC, Mike Parker 
wrote:

* What was previously said *


Ok, so, I am running Windows 10, I have installed VS 2015, I 
have installed DMD 1 and 2 (I know I only need 2 but it 
shouldn't hurt to have both), I have installed VisualD, and I 
have installed DUB with it's installer. (All of these installed 
successfully and work)


So, I'm trying to install gtkD but get "Error: module 
MainWindow is in file 'gtk/MainWindow.d' which cannot be read" 
when I try to run the program. No idea what this means, how to 
fix it, or what step I missed.


What I'd like is for someone to either make a tut of how to get 
from no Dlang on their computer to having a package working in 
a program, or for someone to help me and I'll try to make one 
later.


So, can you show your projects dub.json?



spawn X different workers & wait for results from all of them

2015-09-03 Thread Robert M. Münch via Digitalmars-d-learn

Hi, I'm not sure how to best implement the following:

1. I have 4 different tasks to do.
2. All can run in parallel
3. Every task will return some result that I need

Now how to best do it? When using receive() it fires on the first hit 
and the function continues. It's like a receive(OR), one hit and it's 
done. So, I would need something like a receive(ALL) that continues 
only of all results (messages) have been received.


Is something like this available or do I have to build it myself?

--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



Digger 2.3 / Win32 / 'run' not found

2015-09-03 Thread Robert M. Münch via Digitalmars-d-learn

After getting Digger to work again on OSX now going for the Windows version:

digger: Preparing DigitalMars C++
digger: DMC=Y:\Digger\dl\dm\bin
digger: Preparing DMD
digger: hostDC=
digger: Running: "make" -f win32.mak ^"MODEL=32^" HOST_DC= dmd
digger: PATH=Y:\Digger\dl\dm\bin;C:\WINDOWS\system32;C:\WINDOWS
make -fwin32.mak C=backend TK=tk ROOT=root HOST_DC="" "OPT=-o" "DEBUG=" 
"DDEBUG=" "DOPT=-O -release -inline" "LFLAGS=-L/delexe/la" dmd.exe

run idgen
Error: 'run' not found

--- errorlevel 1
digger: Not caching build failure due to temporary/environment error.
Fatal error: Command ["make", "-f", "win32.mak", "MODEL=32", 
"HOST_DC=", "dmd"] failed with status 1


Could these be some left-overs from an other platform?

--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



Re: Working Windows GUI library?

2015-09-03 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 3 September 2015 at 16:49:51 UTC, BBasile wrote:

I don't know what you meant by 'accessible'


Easily usable by the blind or people with motor difficulties and 
other similar challenges.


Re: Working Windows GUI library?

2015-09-03 Thread BBasile via Digitalmars-d-learn
On Thursday, 3 September 2015 at 15:46:28 UTC, Andre Polykanine 
wrote:

[...]


Hello, there this one: https://github.com/nomad-software/tkd


[...]


I don't know what you meant by 'accessible' but the two 
respective runtimes exist for windows.


Re: cURL alternative for enable javascript

2015-09-03 Thread Laeeth Isharc via Digitalmars-d-learn
On Thursday, 3 September 2015 at 14:17:28 UTC, Rikki Cattermole 
wrote:

On 04/09/15 2:09 AM, xky wrote:

hello! :)

Well.. yes. cURL couldn't enable javascript.
How can i use other library alternative cURL for D ?

regards,


Curl is meant to transfer data and in this case, download files 
over http. It is not meant to render them via JavaScript.


If you are wanting rendering which by the sounds you are by 
JavaScript then you are entering complicated and not so nice 
area of programming.


You may get what you want by e.g. PhantomJS[0], of course you 
could set it up so that it could do what ever you want 
externally to your app. Of course then you would be limited to 
another process/files required.


This problem is not limited to D. Even in C++ you would have to 
think twice about doing this.


Of course somebody may know a better way.

[0] http://phantomjs.org/


And since D can talk to Python via PyD, it's not hard to embed 
phantomjs or the like as a script within your D code.




Re: 2.068.0 std.process.executeShell how to set the shell?

2015-09-03 Thread albatroz via Digitalmars-d-learn
On Thursday, 3 September 2015 at 05:07:10 UTC, Jonathan M Davis 
wrote:

key thing that you're missing is -c.

- Jonathan M Davis


Hi Jonathan, I would just like to say thank you for the time you 
took to help and clarify.


Obrigado


Re: Working Windows GUI library?

2015-09-03 Thread jqb via Digitalmars-d-learn

On Thursday, 3 September 2015 at 16:49:51 UTC, BBasile wrote:

[...]
I don't know what you meant by 'accessible' [...]


https://en.wikipedia.org/wiki/Computer_accessibility

Accessibility is even more important than native language support.




Detecting premature end of spawned threads with std.concurrency

2015-09-03 Thread Matt Kline via Digitalmars-d-learn
TDPL suggests that calls to std.concurrency.send will fail with 
an "OwnedTerminated" or "OwnedFailed" exception if the 
destination thread has exited, but neither the docs nor the 
current Phobos implementation make any mention of such 
exceptions. Thinking the information was just outdated, I 
searched the Git history of Phobos for such types, but found 
nothing.


What are current best practices for determining if a child thread 
has died? And should these types be added to TDPL errata?


Re: reading file byLine

2015-09-03 Thread Namal via Digitalmars-d-learn

On Wednesday, 2 September 2015 at 22:19:11 UTC, wobbles wrote:

On Wednesday, 2 September 2015 at 21:53:20 UTC, Namal wrote:
Thx guys, this helped alot. The next thing I want to do is 
read the file line by line and split the stream into words. I 
found this example of code that seems to do sort of something 
like it. How can I modyfy it so I can store the words in an 
array of strings? Is a => a.length the iterator range?



import std.algorithm, std.stdio, std.string;
// Count words in a file using ranges.
void main()
{
auto file = File("file.txt"); // Open for reading
const wordCount = file.byLine()// Read lines
  .map!split   // Split into 
words
  .map!(a => a.length) // Count words 
per line
  .sum();  // Total word 
count

writeln(wordCount);
}



I would do what you want like this

auto file = File("file.txt");
auto words = file.byLine()   // you've all lines in  
range
 .map!(a => a.split); // read each line, 
splitting it into words
 // now you've a range, 
where each element is an array of words


The map!(a => a.split) line simply maps each element to the 
return value of a.split - this is the predicate.


The a => a.split syntax is a lambda expression that tells map 
what to do on each element.


hello, just copy pasting this brought me those errors:


ep18.d(10): Error: no property 'split' for type 'char[]'
/usr/include/dmd/phobos/std/algorithm.d(427):instantiated 
from here: MapResult!(__lambda1, ByLine!(char, char))
ep18.d(10):instantiated from here: map!(ByLine!(char, 
char))


and then a long list to the end of my code
 Error: undefined identifier a



Re: Prefer Signed or Unsigned in D?

2015-09-03 Thread John Carter via Digitalmars-d-learn

On Wednesday, 2 September 2015 at 21:43:17 UTC, ponce wrote:

Additionally, I was said weeks ago on this NG that and signed 
overflow in D is not actually Undefined Behaviour.


Interesting.. The reference is fairly terse on exactly what 
happens, is it more formally specified anywhere? In which case 
Elder's objection melts away for D and becomes explicitly "prefer 
signed for D, but don't mix".


For integral operands, the *, /, and % correspond to multiply, 
divide, and modulus operations. For multiply, overflows are 
ignored and simply chopped to fit into the integral type.


Re: interprocess communication and sharing memory

2015-09-03 Thread thedeemon via Digitalmars-d-learn
On Thursday, 3 September 2015 at 02:52:00 UTC, Laeeth Isharc 
wrote:

On Thursday, 3 September 2015 at 01:27:15 UTC, j55 wrote:
This is my first attempt at a project in D, so pardon me if 
I'm overlooking something obvious:


I'm using libasync to create an eventloop, to set up something 
like a server/daemon process.  This part works very well.  
We'll call this Server A.


Now I'd like to write another process (running on the same 
computer), we'll call Client B, and pass signals in to Server 
A.


It's probably a stupid idea, but until someone with experience 
answers: what happens if you declare the memory as shared or 
__gshared and send a pointer to it via message passing or a 
pipe?


Pointer from one process doesn't make any sense in another 
process, they point to different data in different address 
spaces, and most probably in the second process memory at this 
"address" isn't even allocated by the app, so it would be an 
access violation.


How to test if a double has no fraction part

2015-09-03 Thread Namal via Digitalmars-d-learn

Hello,

is there a modf function like in C++ or something similar which 
could help me find out if a double has a fractional part or not.


Thx


Re: How to test if a double has no fraction part

2015-09-03 Thread rumbu via Digitalmars-d-learn

On Thursday, 3 September 2015 at 10:52:39 UTC, Namal wrote:

Hello,

is there a modf function like in C++ or something similar which 
could help me find out if a double has a fractional part or not.


Thx


http://dlang.org/phobos/std_math.html#.modf




Re: Access Violation while trying to use OpenGL

2015-09-03 Thread ponce via Digitalmars-d-learn

On Wednesday, 2 September 2015 at 18:31:32 UTC, spec00 wrote:
On Wednesday, 2 September 2015 at 01:07:01 UTC, Mike Parker 
wrote:


To compile 64-bit programs on Windows, DMD requires the 
Microsoft toolchain. The easiest thing to do is to install the 
Community Edition of Visual Studio 2013 (DMD isn't yet 
compatible with VS 2015).


Also, you aren't using OpenGL in this example, but GLFW :)


Thanks Mike!


Don't forget to check your sc.ini file after that, and uncomment 
the relevant lines.


Call a function passed as template parameter.

2015-09-03 Thread moechofe via Digitalmars-d-learn

Hi,

I would like to create a template that take a function as 
template parameter, create an arguments list for it, fill it with 
some data and call the function.


void foo(uint a, string b)
{
  // ...
}

void bar(long a, long b, string c)
{
  // ...
}

call(alias F)(JSONValue j)
{
  // create arguments list
  // assign arguments with the value from j
  // call the function
}

void main()
{
  call!foo(parseJSON("[123, \"nice\"]"));
  call!bar(parseJson("[1,2,3]"));
}

I found interesting stuff like ParameterTypeTuple! and Tuple! but 
I'm not be able to make it work together.


How can I do that?


Re: reading file byLine

2015-09-03 Thread Jordan Wilson via Digitalmars-d-learn
On Thursday, 3 September 2015 at 22:48:01 UTC, Jordan Wilson 
wrote:

On Thursday, 3 September 2015 at 22:21:57 UTC, Namal wrote:


ep18.d(10): Error: no property 'split' for type 'char[]'
/usr/include/dmd/phobos/std/algorithm.d(427):
instantiated from here: MapResult!(__lambda1, ByLine!(char, 
char))
ep18.d(10):instantiated from here: map!(ByLine!(char, 
char))


and then a long list to the end of my code
 Error: undefined identifier a


Hmm, seems I forgot to add std.string, now it works, but words 
seems not to be an array, at least I cannot access it like an 
array. words[0][0] leads to


 Error: no [] operator overload for type MapResult!(__lambda1, 
ByLine!(char, char))


So is is a map? How can I convert all the elements in it to 
integer and store it in a real array?


I believe it's by using array:
auto words = file.byLine()   // you've all lines in  
range

  .map!(a => a.split).array();


Sorry, I didn't notice the "convert all the elements in it to 
integer" part.
I think I saw reference to the to! before...that is one way to 
convert.


auto words = file.byLine()   // you've all lines in range
   .map!(a => a.split)
   .map!(a => to!int(a)).array();




Re: reading file byLine

2015-09-03 Thread Namal via Digitalmars-d-learn
Sorry, I didn't notice the "convert all the elements in it to 
integer" part.
I think I saw reference to the to! before...that is one way to 
convert.


auto words = file.byLine()   // you've all lines in 
range

   .map!(a => a.split)
   .map!(a => to!int(a)).array();



import std.file, std.stdio, std.string, std.conv;

void main(){

auto file = File("text.txt");
auto numbers = file.byLine()
   .map!(a => a.split)
   .map!(a => to!int(a)).array();

writeln(numbers);
}

ep18.d(7): Error: no property 'map' for type 'ByLine!(char, char)'




Re: reading file byLine

2015-09-03 Thread Jordan Wilson via Digitalmars-d-learn

Actually, need an extra map I think:

auto word = file.byLine()
.map!(a => a.split)
.map!(a => map!(a => to!int(a))(a))
.array();




Re: reading file byLine

2015-09-03 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Sep 04, 2015 at 12:18:14AM +, Namal via Digitalmars-d-learn wrote:
[...]
> That being said, when do I have to import std.array and std.string?
> Every time I use std.array? I can obviously use arrays and strings
> without those libs.

Arrays and strings are built into the language; but many common
operations on arrays and strings are not built-in, but are provided in
the standard library, i.e., std.array and std.string.

So if you use just the plain ~, ~=, operators, then you don't need
std.array or std.string. But if you need functions like split() or
array(), then you need to import std.array.  If you're only dealing with
strings as char arrays, then that's all you need. But if you want some
string-specific functions, e.g., isNumeric(), chomp(), capitalize(),
etc., then you need std.string.

Also, some of the more generic operations that can be applied to more
than just arrays or strings, will be found in std.algorithm, such as
map(), find(), count(), startsWith(), etc..

Basically, if an operation *only* applies to strings, it should
generally be found in std.string; if it can also apply to arrays, then
it should be found in std.array. If it can be applied to more than just
arrays (that is, ranges, e.g., a file stream, a network socket, a
sequence of values produced by a generating function, etc., anything
with array-like sequential semantics that are not necessarily actual
arrays), then it will generally be found in std.algorithm.  One useful
rule-of-thumb to decide where to look is to ask yourself if an operation
on an array can also be logically applied to a linked-list. If it can,
it's probably in std.algorithm. If not (i.e. it depends on semantics
specific to arrays), then it's probably in std.array.

Hope this helps.


T

-- 
Latin's a dead language, as dead as can be; it killed off all the Romans, and 
now it's killing me! -- Schoolboy


Re: reading file byLine

2015-09-03 Thread Namal via Digitalmars-d-learn

On Thursday, 3 September 2015 at 23:54:44 UTC, H. S. Teoh wrote:
On Thu, Sep 03, 2015 at 11:38:54PM +, Namal via 
Digitalmars-d-learn wrote:
On Thursday, 3 September 2015 at 23:31:27 UTC, Jordan Wilson 
wrote:

>On Thursday, 3 September 2015 at 23:28:37 UTC, Namal wrote:
>>On Thursday, 3 September 2015 at 23:25:52 UTC, Jordan Wilson 
>>wrote:

>>>And also:
>>>import std.algorithm
>>>
>>>Sorry, I should have taken the time to answer properly and 
>>>fully.

>>
>>import std.file, std.stdio, std.string, std.conv, 
>>std.algorithm;

>>
>>void main(){
>>
>>auto file = File("text.txt");
>>auto numbers = file.byLine()
>> .map!(a => a.split)
>> .map!(a => map!(a => to!int(a))(a))
>> .array();
>>
>>writeln(numbers);
>>}
>>
>>Error: no property 'array' for type 'MapResult!(__lambda2,
>>MapResult!(__lambda1, ByLine!(char, char)))'
>>
>>Still an error.
>
>import std.array

Thx, finaly, this is so much harder to understand than c++ 
iostream


I would have written it slightly differently, to emphasize what 
exactly is going on:


auto numbers = File("text.txt")   // read file
.byLine()   // line by line
.map!(a => a.split   // split each line into words
.map!(a => to!int(a)) // convert each word into int
.array) // collect the ints into an array (per 
line)
.array; // collect all line arrays into one big 
array

This is the functional way of doing it, of course. If you're 
more comfortable with the C++-style imperative approach, you 
could do this instead:


auto file = File("text.txt");
int[][] numbers;
foreach (line; file.byLine) {
auto words = line.split;

int[] lineNums;
foreach (word; words) {
lineNums ~= word.to!int;
}
numbers ~= lineNums;
}

The functional approach is admittedly a bit harder to 
understand at first, but it's extremely powerful because it 
processes everything in a pipeline, and you can compose 
operators on the pipeline easily, rearrange the sequence of 
operations, etc..


In the imperative nested-loop approach, things quickly get out 
of hand once the loop is nested about 2-3 levels deep. A nested 
loop of 6-7 levels deep would be basically impossible to 
understand, maintain, or debug without major refactoring into 
smaller functions. (In fact, split() is a library-provided 
function that basically encapsulates one of those nested 
loops.) But if you take the refactoring to its logical 
conclusion, you'll eventually end up with a whole bunch of tiny 
functions with only a single loop each, each calling the next 
function in a chain -- in other words, you arrive at the 
functional pipeline approach.  :-)


D allows you to do it either way, but the superior approach IMO 
is to learn the functional pipeline approach.



T


Thx Theo, this and the lack of foolproof tutorials were the 
reason why I gave up on D 2 years ago and went instead to C++. 
But I am not giving up this time. That being said, when do I have 
to import std.array and std.string? Every time I use std.array? I 
can obviously use arrays and strings without those libs.


Re: Can we get a video tutorial?

2015-09-03 Thread Stephen via Digitalmars-d-learn

On Thursday, 3 September 2015 at 19:41:55 UTC, wobbles wrote:

* What was previously said *


So, can you show your project's dub.json?


{
"name": "test graphics and math",
"description": "A minimal D application.",
"copyright": "Copyright © 2015, Stephen",
"authors": ["Stephen"],
"dependencies": {
"gfm": "~>3.0.2",
"gtk-d": "~>3.1.4",
"gtk-d:gtkd": "~master"
}
}


Re: reading file byLine

2015-09-03 Thread Jordan Wilson via Digitalmars-d-learn

On Thursday, 3 September 2015 at 22:21:57 UTC, Namal wrote:


ep18.d(10): Error: no property 'split' for type 'char[]'
/usr/include/dmd/phobos/std/algorithm.d(427):
instantiated from here: MapResult!(__lambda1, ByLine!(char, 
char))
ep18.d(10):instantiated from here: map!(ByLine!(char, 
char))


and then a long list to the end of my code
 Error: undefined identifier a


Hmm, seems I forgot to add std.string, now it works, but words 
seems not to be an array, at least I cannot access it like an 
array. words[0][0] leads to


 Error: no [] operator overload for type MapResult!(__lambda1, 
ByLine!(char, char))


So is is a map? How can I convert all the elements in it to 
integer and store it in a real array?


I believe it's by using array:
auto words = file.byLine()   // you've all lines in  range
  .map!(a => a.split).array();





Re: reading file byLine

2015-09-03 Thread Namal via Digitalmars-d-learn
On Thursday, 3 September 2015 at 23:31:27 UTC, Jordan Wilson 
wrote:

On Thursday, 3 September 2015 at 23:28:37 UTC, Namal wrote:
On Thursday, 3 September 2015 at 23:25:52 UTC, Jordan Wilson 
wrote:

And also:
import std.algorithm

Sorry, I should have taken the time to answer properly and 
fully.


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


void main(){

auto file = File("text.txt");
auto numbers = file.byLine()
 .map!(a => a.split)
 .map!(a => map!(a => to!int(a))(a))
 .array();

writeln(numbers);
}

Error: no property 'array' for type 'MapResult!(__lambda2, 
MapResult!(__lambda1, ByLine!(char, char)))'


Still an error.


import std.array


Thx, finaly, this is so much harder to understand than c++ 
iostream




Re: reading file byLine

2015-09-03 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Sep 03, 2015 at 11:38:54PM +, Namal via Digitalmars-d-learn wrote:
> On Thursday, 3 September 2015 at 23:31:27 UTC, Jordan Wilson wrote:
> >On Thursday, 3 September 2015 at 23:28:37 UTC, Namal wrote:
> >>On Thursday, 3 September 2015 at 23:25:52 UTC, Jordan Wilson wrote:
> >>>And also:
> >>>import std.algorithm
> >>>
> >>>Sorry, I should have taken the time to answer properly and fully.
> >>
> >>import std.file, std.stdio, std.string, std.conv, std.algorithm;
> >>
> >>void main(){
> >>
> >>auto file = File("text.txt");
> >>auto numbers = file.byLine()
> >> .map!(a => a.split)
> >> .map!(a => map!(a => to!int(a))(a))
> >> .array();
> >>
> >>writeln(numbers);
> >>}
> >>
> >>Error: no property 'array' for type 'MapResult!(__lambda2,
> >>MapResult!(__lambda1, ByLine!(char, char)))'
> >>
> >>Still an error.
> >
> >import std.array
> 
> Thx, finaly, this is so much harder to understand than c++ iostream

I would have written it slightly differently, to emphasize what exactly
is going on:

auto numbers = File("text.txt") // read file
.byLine()   // line by line
.map!(a => a.split  // split each line into words
.map!(a => to!int(a)) // convert each word into int
.array) // collect the ints into an array (per 
line)
.array; // collect all line arrays into one big 
array

This is the functional way of doing it, of course. If you're more
comfortable with the C++-style imperative approach, you could do this
instead:

auto file = File("text.txt");
int[][] numbers;
foreach (line; file.byLine) {
auto words = line.split;

int[] lineNums;
foreach (word; words) {
lineNums ~= word.to!int;
}
numbers ~= lineNums;
}

The functional approach is admittedly a bit harder to understand at
first, but it's extremely powerful because it processes everything in a
pipeline, and you can compose operators on the pipeline easily,
rearrange the sequence of operations, etc..

In the imperative nested-loop approach, things quickly get out of hand
once the loop is nested about 2-3 levels deep. A nested loop of 6-7
levels deep would be basically impossible to understand, maintain, or
debug without major refactoring into smaller functions. (In fact,
split() is a library-provided function that basically encapsulates one
of those nested loops.) But if you take the refactoring to its logical
conclusion, you'll eventually end up with a whole bunch of tiny
functions with only a single loop each, each calling the next function
in a chain -- in other words, you arrive at the functional pipeline
approach.  :-)

D allows you to do it either way, but the superior approach IMO is to
learn the functional pipeline approach.


T

-- 
Life is too short to run proprietary software. -- Bdale Garbee


Re: reading file byLine

2015-09-03 Thread Namal via Digitalmars-d-learn


ep18.d(10): Error: no property 'split' for type 'char[]'
/usr/include/dmd/phobos/std/algorithm.d(427):
instantiated from here: MapResult!(__lambda1, ByLine!(char, 
char))
ep18.d(10):instantiated from here: map!(ByLine!(char, 
char))


and then a long list to the end of my code
 Error: undefined identifier a


Hmm, seems I forgot to add std.string, now it works, but words 
seems not to be an array, at least I cannot access it like an 
array. words[0][0] leads to


 Error: no [] operator overload for type MapResult!(__lambda1, 
ByLine!(char, char))


So is is a map? How can I convert all the elements in it to 
integer and store it in a real array?


Re: reading file byLine

2015-09-03 Thread Namal via Digitalmars-d-learn
On Thursday, 3 September 2015 at 23:25:52 UTC, Jordan Wilson 
wrote:

And also:
import std.algorithm

Sorry, I should have taken the time to answer properly and 
fully.


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

void main(){

auto file = File("text.txt");
auto numbers = file.byLine()
 .map!(a => a.split)
 .map!(a => map!(a => to!int(a))(a))
 .array();

writeln(numbers);
}

Error: no property 'array' for type 'MapResult!(__lambda2, 
MapResult!(__lambda1, ByLine!(char, char)))'


Still an error.


Re: reading file byLine

2015-09-03 Thread Jordan Wilson via Digitalmars-d-learn

And also:
import std.algorithm

Sorry, I should have taken the time to answer properly and fully.


Re: reading file byLine

2015-09-03 Thread Jordan Wilson via Digitalmars-d-learn

On Thursday, 3 September 2015 at 23:28:37 UTC, Namal wrote:
On Thursday, 3 September 2015 at 23:25:52 UTC, Jordan Wilson 
wrote:

And also:
import std.algorithm

Sorry, I should have taken the time to answer properly and 
fully.


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

void main(){

auto file = File("text.txt");
auto numbers = file.byLine()
 .map!(a => a.split)
 .map!(a => map!(a => to!int(a))(a))
 .array();

writeln(numbers);
}

Error: no property 'array' for type 'MapResult!(__lambda2, 
MapResult!(__lambda1, ByLine!(char, char)))'


Still an error.


import std.array



Re: reading file byLine

2015-09-03 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Sep 03, 2015 at 11:22:09PM +, Namal via Digitalmars-d-learn wrote:
> >Sorry, I didn't notice the "convert all the elements in it to integer"
> >part.
> >I think I saw reference to the to! before...that is one way to convert.
> >
> >auto words = file.byLine()   // you've all lines in range
> >   .map!(a => a.split)
> >.map!(a => to!int(a)).array();
> 
> 
> import std.file, std.stdio, std.string, std.conv;
> 
> void main(){
> 
>   auto file = File("text.txt");
>   auto numbers = file.byLine()
>.map!(a => a.split)
>.map!(a => to!int(a)).array();
>   
>   writeln(numbers);
> }
> 
> ep18.d(7): Error: no property 'map' for type 'ByLine!(char, char)'

import std.algorithm : map;


T

-- 
Never step over a puddle, always step around it. Chances are that whatever made 
it is still dripping.


Re: reading file byLine

2015-09-03 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Sep 03, 2015 at 11:28:36PM +, Namal via Digitalmars-d-learn wrote:
> On Thursday, 3 September 2015 at 23:25:52 UTC, Jordan Wilson wrote:
> >And also:
> >import std.algorithm
> >
> >Sorry, I should have taken the time to answer properly and fully.
> 
> import std.file, std.stdio, std.string, std.conv, std.algorithm;
> 
> void main(){
> 
>   auto file = File("text.txt");
>   auto numbers = file.byLine()
>  .map!(a => a.split)
>  .map!(a => map!(a => to!int(a))(a))
>  .array();
>   
>   writeln(numbers);
> }
> 
> Error: no property 'array' for type 'MapResult!(__lambda2,
> MapResult!(__lambda1, ByLine!(char, char)))'
> 
> Still an error.

import std.array : array;


T

-- 
Do not reason with the unreasonable; you lose by definition.


Re: reading file byLine

2015-09-03 Thread Jordan Wilson via Digitalmars-d-learn

On Friday, 4 September 2015 at 00:18:15 UTC, Namal wrote:

On Thursday, 3 September 2015 at 23:54:44 UTC, H. S. Teoh wrote:

[...]


Thx Theo, this and the lack of foolproof tutorials were the 
reason why I gave up on D 2 years ago and went instead to C++. 
But I am not giving up this time. That being said, when do I 
have to import std.array and std.string? Every time I use 
std.array? I can obviously use arrays and strings without those 
libs.



My 2 cents, as someone who is newish to D, I'd also recommend the 
above approach. I found the whole ranges thing to be hard to get 
at first, but I do find myself using ranges + std.algorithm more 
and more (even though it's mostly just basic map and filter).


Arithmetic conversion -- warnings missing?

2015-09-03 Thread Q. Schroll via Digitalmars-d-learn

import std.stdio;

void main()
{
short  ss = -1;
ushort us =  0;
intsi = -1;
uint   ui =  0;
if (ss < us)
writeln("a");
else
writeln("b");

if (si < ui)
writeln("A");
else
writeln("B");
}

prints "aB" due to integral promotion and arithmetic conversion 
rules. I know it's this kind of thing: If it compiles and looks 
like c, it behaves the same.

But why does it compile and why does it so without any warning?
Is there any rationale to make this even work?


Re: reading file byLine

2015-09-03 Thread deed via Digitalmars-d-learn

On Friday, 4 September 2015 at 01:31:28 UTC, Namal wrote:
How can I get just the maximum element? Do I need to give a 
range for it?


Use max? http://dlang.org/phobos/std_algorithm_comparison.html#max


OSX prompt limit

2015-09-03 Thread Joel via Digitalmars-d-learn
In Mac OS, when typing with readln etc. I can't use the cursor 
keys. Works in Windows though.




Re: Call a function passed as template parameter.

2015-09-03 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 3 September 2015 at 11:31:22 UTC, moechofe wrote:
I would like to create a template that take a function as 
template parameter, create an arguments list for it, fill it 
with some data and call the function.


Check out the sample chapter to my book:
https://www.packtpub.com/application-development/d-cookbook

This is one of the examples I explain there that uses this basic 
idea to convert strings to parameters:


http://arsdnet.net/dcode/book/chapter_08/11/caller.d

Or, a real world example I've written is here:

https://github.com/adamdruppe/arsd/blob/master/jsvar.d#L608

which is similar to what you're trying to do, though JSONValue 
will be a bit harder to convert than my `var`.


Re: Casting away immutability

2015-09-03 Thread Sergei Degtiarev via Digitalmars-d-learn
On Thursday, 3 September 2015 at 05:15:35 UTC, Jonathan M Davis 
wrote:
On Wednesday, September 02, 2015 14:00:07 Sergei Degtiarev via 
Digitalmars-d-learn wrote:
Well, that's how mmap works. You're just getting raw bytes as 
void*, and the D code converts that to void[], which is 
slightly safer. It doesn't inherently have a type, and often, 
the data referred to by mmap never did have a type. It's just 
bytes in a file. So, if you want to use it as a type, you have 
to tell the compiler what it's type is - and get it right - via 
a cast. And yes, you can cast to immutable as part of that, 
because casts let you shoot yourself in the foot, because 
you're telling the compiler that you know better than it does, 
but you shouldn't be casting anything you get from C to 
immutable, because it's not going to be immutable. Most code 
shouldn't be casting to immutable any more than it should be 
casting away immutable.


immutable data is treated by the compiler as immutable - it 
will _never_ change over the course of the program - so if it 
does change, you're going to have fun bugs, and if it really 
refers to mutable data (as would be the case with an mmapped 
file), then it could change. Additionally, immutable is treated 
as implicitly shared, so the compiler could do different 
optimizations based on that (though that probably won't affect 
anything if you only ever have it one a single thread), and if 
it's not really immutable, you could have fun bugs caused by 
that.


Don't cast to or from immutable unless you know what you're 
doing, and in most of those cases, there's a decent chance that 
that's not the best way to do what you're trying to do anyway.


Agree, however, memory obtained with mmap(..., PROT_READ, ..); is 
essentially read-only and any attempt to write to it will cause 
immediate crash with segmentation violation. It would be very 
desirable in this case to return something that could be cast to 
immutable(type)[] but not type[].

This is what I tried to find out. Thank you for the help.



Re: How to test if a double has no fraction part

2015-09-03 Thread Namal via Digitalmars-d-learn
Interesting, in contrary to C++ it saves the integral part in the 
dummy variable. Doing this I noticed if I try to write a double 
variable in the console it gives me only the integral part. I 
only did it with writeln so far. How can I print out a double 
variable with a precision of 2 for instance? I tried it with 
writefln("%d", d)

It compiles but crashes.


Re: How to test if a double has no fraction part

2015-09-03 Thread wobbles via Digitalmars-d-learn

On Thursday, 3 September 2015 at 12:51:42 UTC, Namal wrote:
Interesting, in contrary to C++ it saves the integral part in 
the dummy variable. Doing this I noticed if I try to write a 
double variable in the console it gives me only the integral 
part. I only did it with writeln so far. How can I print out a 
double variable with a precision of 2 for instance? I tried it 
with writefln("%d", d)

It compiles but crashes.


I believe .2 will print it precision two.
writefln("%.2s", d)

More info here:
   http://dlang.org/phobos/std_format.html#formattedWrite


Re: Can we get a video tutorial?

2015-09-03 Thread Mike Parker via Digitalmars-d-learn

On Thursday, 3 September 2015 at 19:13:35 UTC, Stephen wrote:
On Wednesday, 2 September 2015 at 01:15:28 UTC, Mike Parker 
wrote:

* What was previously said *


Ok, so, I am running Windows 10, I have installed VS 2015, I 
have installed DMD 1 and 2 (I know I only need 2 but it 
shouldn't hurt to have both), I have installed VisualD, and I 
have installed DUB with it's installer. (All of these installed 
successfully and work)


DMD is not currently compatible with VS 2015. You'll have trouble 
if you try to use it. That will hopefully be fixed in a 
not-too-distant release.




So, I'm trying to install gtkD but get "Error: module 
MainWindow is in file 'gtk/MainWindow.d' which cannot be read" 
when I try to run the program. No idea what this means, how to 
fix it, or what step I missed.




Based on this error, it appears you've done this in your code:

import MainWindow;

When you should be doing this:

import gtk.MainWindow;


Re: OSX prompt limit

2015-09-03 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 4 September 2015 at 02:17:57 UTC, Joel wrote:
In Mac OS, when typing with readln etc. I can't use the cursor 
keys. Works in Windows though.


That's normal, line editing on Unix terminals is a kinda advanced 
library feature. The most common lib to do it, GNU readline, is 
actually a big thing that pushed the GPL because of how useful it 
was and it happened to use that license.


I wrote one too though it is a bit bulky.
https://github.com/adamdruppe/arsd/blob/master/terminal.d

import terminal;
void main() {
   auto terminal = Terminal(ConsoleOutputMode.linear);
   auto line = terminal.getline("your prompt: ");
   terminal.writeln("You wrote: ", line);
}

compile:

dmd yourapp.d terminal.d


Re: reading file byLine

2015-09-03 Thread Namal via Digitalmars-d-learn

Hope this helps.


Yes, it does. I have a question about arrays. I can sort an array 
A by sort(A);
How can I get just the maximum element? Do I need to give a range 
for it?