Is there a similar library to FreeMarker like in Java

2020-11-11 Thread Namal via Digitalmars-d-learn

Hello,

I want to do a small project but I need a text replacement 
tool/lib like Apache's FreeMarker. Is there something similar for 
D?


Thx


Re: preset counter variable in a for loop

2020-02-28 Thread Namal via Digitalmars-d-learn

On Friday, 28 February 2020 at 12:48:17 UTC, mipri wrote:

On Friday, 28 February 2020 at 12:44:52 UTC, Namal wrote:

Hello,

I don't understand why this simple code causes a compiler 
error..


import std.stdio;

void main(){

 int b = 0;

 for (b; b<3; b++){
   writeln(b);  
 }
}

$Error: b has no effect


Well, that's the error. b has no effect, so there's
no need for it, and it's likely written in error. It
has no effect in C++ either, and you may get a warning:

example.cc: In function 'int main()':
example.cc:7:8: warning: statement has no effect 
[-Wunused-value]

  for (b; b<3; b++){


Thanks, I didn't know that you don't need it there and can leave 
it out!




preset counter variable in a for loop

2020-02-28 Thread Namal via Digitalmars-d-learn

Hello,

I don't understand why this simple code causes a compiler error..

import std.stdio;

void main(){

 int b = 0;

 for (b; b<3; b++){
   writeln(b);  
 }
}

$Error: b has no effect

Same works perfectly fine in C++

#include 

int main(){
 int i = 0;

 for(i; i<3; i++)
   std::cout<

preset counter variable in a for loop --> 'has no effect' Error

2020-02-28 Thread Namal via Digitalmars-d-learn

Hello,

I don't understand why this simple code causes a compiler error..

import std.stdio;

void main(){

 int b = 0;

 for (b; b<3; b++){
   writeln(b);  
 }
}

$Error: b has no effect

Same works perfectly fine in C++

#include 

int main(){
 int i = 0;

 for(i; i<3; i++)
   std::cout<

Re: Question about the $ sign in arrays and strings

2020-02-18 Thread Namal via Digitalmars-d-learn

oooh... I used

str = std.readln();

to get my string and there must have been some other sign, line 
break or whitespace or something at the end  :(


Now I understand it, thx


Question about the $ sign in arrays and strings

2020-02-18 Thread Namal via Digitalmars-d-learn
Hello, I wanted to remove the lastchar in a string and figured 
that you can do that wit


str = str[0..$-2];

but why is

str = str[0..$] and str=str[0..$-1]

the same ?


How to remove whitespace from a string

2020-01-16 Thread Namal via Digitalmars-d-learn
Hello, what is the way to remove whitespace from a string (not 
only at the beginning and end)..


User input

2017-11-08 Thread Namal via Digitalmars-d-learn
Is there any other way to do a simple user input into integer 
variables without using std.conv?


Re: sorting a string

2017-07-14 Thread Namal via Digitalmars-d-learn
On Friday, 14 July 2017 at 17:23:41 UTC, Steven Schveighoffer 
wrote:



import std.string: representation, assumeUTF;
import std.algorithm: sort;

auto bytes = line.representation.dup;
bytes.sort;
auto result = bytes.assumeUTF; // result is now char[]



Why does it have to be char[]?

 auto bytes = line.representation.dup;
 bytes.sort;
 string result = bytes.assumeUTF;

works too.


Re: sorting a string

2017-07-14 Thread Namal via Digitalmars-d-learn

On Friday, 14 July 2017 at 16:43:42 UTC, Anton Fediushin wrote:

On Friday, 14 July 2017 at 15:56:49 UTC, Namal wrote:
Thx Steve! By sorting string I mean a function or series of 
functions that sorts a string by ASCII code, "cabA" to "Aabc" 
for instance.


import std.algorithm : sort;
import std.stdio : writeln;

"cabA".dup.sort.writeln;

`dup` is used, because string cannot be modified, so a copy of 
string used instead.


Thx alot. One final question. If I do it like that. I get a 
deprrecation warning:


use std.algorithm.sort instead of .sort property

Wasn't .sort() the proper way to use it, no? Because that won't 
compile.


Re: sorting a string

2017-07-14 Thread Namal via Digitalmars-d-learn
On Friday, 14 July 2017 at 15:15:42 UTC, Steven Schveighoffer 
wrote:




import std.algorithm: filter;
import std.uni: isWhite;

line.filter!(c => !c.isWhite).to!string;

be warned, this is going to be a bit slow, but that's the cost 
of autodecoding.


If you are looking for just removing ascii whitespace, you can 
do it a bit more efficiently, but it's not easy.


About the string sorting, you'd have to be more specific.

-Steve


Thx Steve! By sorting string I mean a function or series of 
functions that sorts a string by ASCII code, "cabA" to "Aabc" for 
instance.





sorting a string

2017-07-14 Thread Namal via Digitalmars-d-learn
Is there a 'easy' way to sort a string in D like it is possible 
in Python? Also how can I remove whitespace between characters if 
I am reading a line from a file and safe it as a string?


string[] buffer;

foreach (line ; File("test.txt").byLine)
buffer ~= line.to!string;


Help me fix my compiler

2017-07-12 Thread Namal via Digitalmars-d-learn

Hello,

I used the Install Script command line to install the newest dmd 
compiler (Ubuntu 16.04.2 LTS). Now I have to type 'source 
~/dlang/dmd-2.074.1/activate' before I can use it and it is also 
not show in the software center like it used to be. How can I fix 
it or how can I remove it?


Thx


Re: reading from file

2016-12-13 Thread Namal via Digitalmars-d-learn
Sorry if I wasn't clear. The array should be two demensional and 
each line in text line should be a row in that 2x2 array.


Re: reading from file

2016-12-13 Thread Namal via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 16:57:40 UTC, Namal wrote:
Sorry if I wasn't clear. The array should be two demensional 
and each line in text line should be a row in that 2x2 array.


Also, it should be saved as an integer.


reading from file

2016-12-13 Thread Namal via Digitalmars-d-learn
Hello, comming from C++, I find it hard to remember and 
understand how reading from file should be done in D. Especially 
since I am not very good in functional programming. So I have a 
file which looks like this:


1,2,3,4
5,6,7,8
9,11,11,12

and so on

How could I read it row by row and create an array accordingly 
without reading the comma?


Re: How to set array length for multidimensional static arrays

2016-02-01 Thread Namal via Digitalmars-d-learn
Sorry guys that I didn't express myself well. I also mixed some 
stuff up. What I wanted to ask is this, in c++ this is valid


int x = 3;
int y = 10;
int arr [x][y];

x,y are known at the compile time and arr is a static array. I 
can't do that in D so what is the best way to declare an array of 
that size?


Re: How to set array length for multidimensional static arrays

2016-02-01 Thread Namal via Digitalmars-d-learn
On Monday, 1 February 2016 at 12:12:00 UTC, Jonathan M Davis 
wrote:
On Monday, February 01, 2016 11:15:40 Namal via 
Digitalmars-d-learn wrote:
Sorry guys that I didn't express myself well. I also mixed 
some stuff up. What I wanted to ask is this, in c++ this is 
valid


  int x = 3;
  int y = 10;
  int arr [x][y];

x,y are known at the compile time and arr is a static array. I 
can't do that in D so what is the best way to declare an array 
of that size?


If x and y are known at compile time, then you can declare a 
static array using them for dimensions. e.g.


enum x = 3;
enum y = 10;
int[y][x] arr;

But x and y must be something that it is evaluated by the 
compiler at compile time - e.g. an enum or a static variable. A 
local variable that just so happens to be directly initialized 
(like in your example) won't work.


If x and y are _not_ known at compile time, then you can't use 
the to declare a static array. You'll have to use a dynamic 
array. e.g.


auto arr = new int[][](x, y);

- Jonathan M Davis


Thanks alot, I didn't know that way with new.


Re: How to set array length for multidimensional static arrays

2016-01-31 Thread Namal via Digitalmars-d-learn

On Monday, 1 February 2016 at 07:41:33 UTC, Namal wrote:
I understand that I cannot pass a variable to the static array 
like in C++, and have to use dynamic arrays. But how can I set 
the length for them without using a loop?


I mean std::vector in C++, not array.


is there a set container?

2016-01-25 Thread Namal via Digitalmars-d-learn

Hello,

I am looking for data structure equivalent to std::set from C++. 
Couldn't find it in the documentation.


Calling functions from other files/modules

2016-01-06 Thread Namal via Digitalmars-d-learn

Hello,

finally I want to learn how to do it right and I tried to 
understand it from here


https://en.wikibooks.org/wiki/D_(The_Programming_Language)/d2/Modules

But I have a few questions. Do I have always to include std.stdio 
in every file like in the example or is it enough just to import 
a module which has this already included?


Why do I need to define my main as the main module? I can't 
imagine that I can import it somewhere else.


Is there something like #pragma once that needs to be done?




Re: Calling functions from other files/modules

2016-01-06 Thread Namal via Digitalmars-d-learn

On Wednesday, 6 January 2016 at 22:15:43 UTC, Adam D. Ruppe wrote:


You can import it as long as you define it!


I just tried to import one module with a main into another, but I 
get this:


Error: only one main allowed




Re: Calling functions from other files/modules

2016-01-06 Thread Namal via Digitalmars-d-learn

On Wednesday, 6 January 2016 at 23:06:38 UTC, Adam D. Ruppe wrote:

On Wednesday, 6 January 2016 at 23:00:43 UTC, Namal wrote:
I just tried to import one module with a main into another, 
but I get this:


You can't have two mains, but you can import a module with main 
from another module without one.


How can I produce a program file from that module which has no 
main but uses some functions from main module?


Re: functional way doing array stuff/ lambda functions

2015-12-14 Thread Namal via Digitalmars-d-learn

On Sunday, 13 December 2015 at 01:01:07 UTC, cym13 wrote:

That's because you want to modify it in product passing it by 
ref.


Hmm, that seems different to c++.


On Sunday, 13 December 2015 at 11:37:50 UTC, cym13 wrote:



As cryptic as it is this means that the range you passed to 
reduce is empty. Reduce needs it not to be because as I said it 
needs a seed and as you didn't pass one explicitely as argument 
it tries to take the first element of the range (and fails 
obviously). You can either pass it a seed explicitely or add a 
non-empty check before the return.



Here is the full code, where I changed the product function. 
Sorry, I don't understand what the difference now and why it 
crashes.


import std.stdio, std.array, std.algorithm;


int product(const ref int[] arr){

/*int p = 1;
foreach(i;arr)
p*=i;
return p;*/

return arr.reduce!((a, b) => a*b)();

}


int [] prim_sieve(int n){

bool [] T;
T.length = n;
T[0] = true;
T[1] = true;

for(int i = 2; i*i <= T.length-1;++i){
for(int j = i*i;j1)
v~=n;

return v.dup.sort.uniq.array;

}


void bubblesort(ref int[2][] v){

size_t n = v.length;
do{
int newn = 1;
for(int i = 0;iv[i+1][1]){
auto temp = v[i];
v[i] = v[i+1];
v[i+1] = temp;
newn = i+1;
}
}
n=newn; 
}
while(n>1);
}



void main(){

int[] P = prim_sieve(500);

int[2][] v;

foreach(k;1..11){
auto t = prim_factors(k,P);
v~= [k,product(t)];
}

bubblesort(v);

writeln(v[][0]);
}


Re: functional way doing array stuff/ lambda functions

2015-12-12 Thread Namal via Digitalmars-d-learn

On Saturday, 12 December 2015 at 23:50:55 UTC, Xinok wrote:

On Saturday, 12 December 2015 at 23:36:43 UTC, cym13 wrote:

...
So, in your example:

int product(const ref int[] arr) {
import std.array: array;
import std.algorithm: reduce;

arr = arr.reduce!((p, i) => p*i).array;
}


A good post overall but you got reduce wrong. In D, reduce 
computes and returns the result immediately, not a lazy range. 
The following code is correct:


int product(const ref int[] arr) {
import std.array: array;
import std.algorithm: reduce;

return arr.reduce!((p, i) => p*i)();
}

Example: http://dpaste.dzfl.pl/fc2c2eab2d02



I tried this, it compiles, but crashes when I try to run it:

object.Exception@/usr/include/dmd/phobos/std/algorithm/iteration.d(2481): 
Enforcement failed

??:? pure @safe void 
std.exception.bailOut!(Exception).bailOut(immutable(char)[], 
ulong, const(char[])) [0x43a547]
??:? pure @safe bool std.exception.enforce!(Exception, 
bool).enforce(bool, lazy const(char)[], immutable(char)[], ulong) 
[0x43a4c4]
??:? pure @safe int 
std.algorithm.iteration.__T6reduceS305ep1247productFKxAiZ9__lambda2Z.reduce!(const(int)[]).reduce(const(int)[]) [0x43a2ed]

??:? int ep124.product(ref const(int[])) [0x439d49]
??:? _Dmain [0x43a229]
??:? 
_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv 
[0x448b9a]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate()) [0x448af0]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).runAll() [0x448b56]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate()) [0x448af0]

??:? _d_run_main [0x448a4d]
??:? main [0x4447ad]
??:? __libc_start_main [0xd9f09ec4]




Re: functional way doing array stuff/ lambda functions

2015-12-12 Thread Namal via Digitalmars-d-learn

On Sunday, 13 December 2015 at 00:02:11 UTC, cym13 wrote:

Now that I think about it, it's true that it would make no 
sense whatsoever to return a range as reduce is typically used 
to return a single value... At least it makes perfect sense.


Thanks alot, this helped alot. But I have another question

I have two functions:

int[] prim_factors(int n, const ref int[] P){

int[] v;

for(int i; P[i]*P[i]<=n;++i){
while(n%P[i]==0){
v~=P[i];
n/=P[i];
}
}
if(n>1)
v~=n;

return v.dup.sort.uniq.array;

}


int product(const ref int[] arr){

int p = 1;
foreach(i;arr)
p*=i;
return p;
}

While vector P contains some primes I get with a prime sieve. So 
if I just try to use those functions like:


writeln(product(prim_factors(10,P)));

I get the error:

function prog.product (ref const(int[]) arr) is not callable 
using argument types (int[])


Why do I have to call it like that first:

auto v = prim_factors(10,P);
writeln(product(v));

?



functional way doing array stuff/ lambda functions

2015-12-12 Thread Namal via Digitalmars-d-learn

Hello guys,

I am still uncertain how to do it right when it comes to lambda 
functions. For instance: how do I multiply all the elements in an 
array ?


int product(const ref int[] arr){

int p = 1;
foreach(i;arr)
p*=i;
return p;
}




Re: reading file byLine

2015-11-07 Thread Namal via Digitalmars-d-learn

On Saturday, 7 November 2015 at 17:13:33 UTC, Namal wrote:

On Saturday, 5 September 2015 at 14:49:13 UTC, deed wrote:

On Saturday, 5 September 2015 at 14:44:19 UTC, deed wrote:

 .map!(s => chomp(s, "\"")
 .map!(s => chompPrefix(s, "\"")


should be

 .map!(s => chomp(s, "\""))
 .map!(s => chompPrefix(s, "\""))


Hello again,

Now I have a file that looks like

a b c d
e f g h


I want to get that in an element of strings but without 
quatation marks


auto a = f.byLine()
.map!(a => a.split)
.array();

f.close();


Sorry for double post, I pressed accidently 'enter'. How can I ad 
chomp so the quatation marks are removed?


Re: reading file byLine

2015-11-07 Thread Namal via Digitalmars-d-learn

On Saturday, 5 September 2015 at 14:49:13 UTC, deed wrote:

On Saturday, 5 September 2015 at 14:44:19 UTC, deed wrote:

 .map!(s => chomp(s, "\"")
 .map!(s => chompPrefix(s, "\"")


should be

 .map!(s => chomp(s, "\""))
 .map!(s => chompPrefix(s, "\""))


Hello again,

Now I have a file that looks like

a b c d
e f g h


I want to get that in an element of strings but without quatation 
marks


auto a = f.byLine()
.map!(a => a.split)
.array();

f.close();




Re: conver BigInt to string

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

On Thursday, 5 November 2015 at 17:40:12 UTC, bearophile wrote:

Namal:

Hello I am trying to convert BigInt to string like that while 
trying to sort it:


void main() {
import std.stdio, std.algorithm, std.conv, std.bigint, 
std.string;


auto n = 17.BigInt ^^ 179;
n.text.dup.representation.sort().release.assumeUTF.writeln;
}

Bye,
bearophile


can I import libraries anywhere? Is this the proper way to do so?


Re: conver BigInt to string

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

On Thursday, 5 November 2015 at 16:35:01 UTC, BBasile wrote:

On Thursday, 5 November 2015 at 16:29:30 UTC, Namal wrote:
Hello I am trying to convert BigInt to string like that while 
trying to sort it:


string s1 = to!string(a).dup.sort;

and get an error

cannot implicitly convert expression (_adSortChar(dup(to(a 
of type char[] to string


what do I do wrong?


try

".idup"

otherwise

"auto s1 = "


auto did it, but idup leads to

Error: can only sort a mutable array



Re: conver BigInt to string

2015-11-05 Thread Namal via Digitalmars-d-learn
On Thursday, 5 November 2015 at 17:13:07 UTC, Ilya Yaroshenko 
wrote:



string s1 = to!string(a).dup.sort.idup;

well, but I was just told not to use sort ??



Re: conver BigInt to string

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

On Thursday, 5 November 2015 at 16:45:10 UTC, Meta wrote:

On Thursday, 5 November 2015 at 16:29:30 UTC, Namal wrote:
Hello I am trying to convert BigInt to string like that while 
trying to sort it:


string s1 = to!string(a).dup.sort;

and get an error

cannot implicitly convert expression (_adSortChar(dup(to(a 
of type char[] to string


what do I do wrong?


Try this instead:

string s1 = to!string(a).idup.sort()


If I try it like that i get:

Error: template std.algorithm.sorting.sort cannot deduce function 
from argument types !()(char[]), candidates are:


/../src/phobos/std/algorithm/sorting.d(996):



conver BigInt to string

2015-11-05 Thread Namal via Digitalmars-d-learn
Hello I am trying to convert BigInt to string like that while 
trying to sort it:


string s1 = to!string(a).dup.sort;

and get an error

cannot implicitly convert expression (_adSortChar(dup(to(a of 
type char[] to string


what do I do wrong?


Re: How to detect overflow

2015-11-04 Thread Namal via Digitalmars-d-learn

On Wednesday, 4 November 2015 at 07:59:44 UTC, Ali Çehreli wrote:

On 11/03/2015 11:52 PM, Namal wrote:


http://dlang.org/phobos/core_checkedint.html


It says:


"The overflow is sticky, meaning a sequence of operations can 
be done

and overflow need only be checked at the end."

But how can I make multiple operations? I can only put 2 
values in the

function.


import core.checkedint;

void main() {
bool overflowed;
auto result = adds(int.max, 1, overflowed); // this 
overflows

adds(1, 2, overflowed); // this does not reset the flag

assert(overflowed);
}

Ali


wow, this I don't understand at all, how do those two operations 
connected to each other? By the bool value?




Re: foreach loop

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

On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe wrote:

On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:
I remember it is possible to get the index for each element in 
the foreach loop, but I forgot how to do it. Can you help me 
out please. Thx.


for many of them it is as simple as:

foreach(index, element; array) { }


Thank you. I am still struggling with the functional ways of D. 
Now how could I write this foreach loop the functional way?


bool[] arr = [ture, false, ture, ...];

int count;
foreach(i;arr){

  if(!i)
count++;
}
writeln(count);




Re: foreach loop

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

On Tuesday, 3 November 2015 at 15:10:43 UTC, wobbles wrote:

On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote:
On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe 
wrote:

On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:
I remember it is possible to get the index for each element 
in the foreach loop, but I forgot how to do it. Can you help 
me out please. Thx.


for many of them it is as simple as:

foreach(index, element; array) { }


Thank you. I am still struggling with the functional ways of 
D. Now how could I write this foreach loop the functional way?


bool[] arr = [ture, false, ture, ...];

int count;
foreach(i;arr){

  if(!i)
count++;
}
writeln(count);


writefln("Count is: %s", arr
  .filter!(a => a==true)
  .sum);

// Note: std.algorithm.sum is the same as
// std.algorithm.reduce!((a,b)=a+b);


well I tried this that way, but my count stays 0, same as if I do 
it in an int function with a return though I clearly have some 
false elements in the arr.


Re: foreach loop

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

On Tuesday, 3 November 2015 at 15:10:43 UTC, wobbles wrote:

On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote:
On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe 
wrote:

On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:
I remember it is possible to get the index for each element 
in the foreach loop, but I forgot how to do it. Can you help 
me out please. Thx.


for many of them it is as simple as:

foreach(index, element; array) { }


Thank you. I am still struggling with the functional ways of 
D. Now how could I write this foreach loop the functional way?


bool[] arr = [ture, false, ture, ...];

int count;
foreach(i;arr){

  if(!i)
count++;
}
writeln(count);


writefln("Count is: %s", arr
  .filter!(a => a==true)
  .sum);

// Note: std.algorithm.sum is the same as
// std.algorithm.reduce!((a,b)=a+b);


How do I save sum as integer or something I try;

arr.writeln;
return arr.filter!(a=>a==false).sum;

but I get
[true, false, true, false, true, true, true, false, true, false]
0




How to detect overflow

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

Is there a way to detect overflow for example for:

int i = 2_000_000_000;

int a = i*i*i;

writeln(a);

-> 1073741824


Re: How to detect overflow

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

On Wednesday, 4 November 2015 at 04:22:03 UTC, BBasile wrote:

On Wednesday, 4 November 2015 at 03:55:13 UTC, Namal wrote:

Is there a way to detect overflow for example for:

int i = 2_000_000_000;

int a = i*i*i;

writeln(a);

-> 1073741824


You can use core.checkedint [1]

---

http://dlang.org/phobos/core_checkedint.html


It says:


"The overflow is sticky, meaning a sequence of operations can be 
done and overflow need only be checked at the end."


But how can I make multiple operations? I can only put 2 values 
in the function.





Re: How to detect overflow

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

On Wednesday, 4 November 2015 at 04:22:03 UTC, BBasile wrote:

On Wednesday, 4 November 2015 at 03:55:13 UTC, Namal wrote:

Is there a way to detect overflow for example for:

int i = 2_000_000_000;

int a = i*i*i;

writeln(a);

-> 1073741824


You can use core.checkedint [1]

---

http://dlang.org/phobos/core_checkedint.html


Is it just an error in the documentation that the return value is 
stated as sum for the multiplication functions?


Re: foreach loop

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

Hello guys,

I remember it is possible to get the index for each element in 
the foreach loop, but I forgot how to do it. Can you help me out 
please. Thx.


foreach loop

2015-10-19 Thread Namal via Digitalmars-d-learn

Is it possible to create a foreach loop with a breakstetemen?

I mean something like that for the second loop where i want to 
break if element from:


int []  g = [9,15,21];
int [] v = [2,3,5,7,8,9,11,13,17,19];

foreach(j;1..10)
for(int i = 0; v[i]

Re: foreach loop

2015-10-19 Thread Namal via Digitalmars-d-learn
On Monday, 19 October 2015 at 14:43:04 UTC, Rikki Cattermole 
wrote:

On 20/10/15 3:28 AM, Namal wrote:

Is it possible to create a foreach loop with a breakstetemen?

I mean something like that for the second loop where i want to 
break if

element from:

int []  g = [9,15,21];
int [] v = [2,3,5,7,8,9,11,13,17,19];

foreach(j;1..10)
for(int i = 0; v[i]

Re: foreach loop

2015-10-19 Thread Namal via Digitalmars-d-learn

Is it possible to use foreach backwards?

foreach(int i;20..1)
 writeln(i);

compiles but I get nothing.



Tree datatype

2015-10-14 Thread Namal via Digitalmars-d-learn

Hello,

I don't remember exactly but I think when I first saw D code 
there was tree datatype implemented without pointers. Is it 
possible to make a tree struct without pointers?


Re: How to do unittests

2015-10-02 Thread Namal via Digitalmars-d-learn

On Wednesday, 30 September 2015 at 14:44:20 UTC, qsdf wrote:

On Wednesday, 30 September 2015 at 14:20:28 UTC, Namal wrote:
On Wednesday, 30 September 2015 at 13:03:52 UTC, Rikki 
Cattermole wrote:

On 01/10/15 1:59 AM, Namal wrote:

Hello,

can someone give me a complete example please how to do 
unittests? I
tried this with the example from german wikipedia, but the 
flag

-unittest didn't make any difference.


Example file with loads of unittests: 
https://github.com/rikkimax/alphaPhobos/blob/master/source/std/experimental/uri.d


If you were to compile it e.g. dmd uri.d it won't be much use 
(unittest wise). You will need to dmd -unittest uri.d to 
compile them in. Don't forget to do the same for your main 
function. When you run the final executable the tests will 
execute before your main function does.


can't I do unittest in the main?


D unit tests are like a stack of free functions. You put them 
separatly.



when there's a main: dmd -unittest a.d
--
module a;
void main(){}

unittest{}
--


when there is no main: (like std.uri): dmd -main -unittest a.d
--
module a;
unittest{}
--

the -main switch adds a dummy main function so that the output 
can be executed.


But most of the time you'll think that nothing happens because 
the tests succeed...


So do I understand it right that it stops after the first failed 
test? Is it possible to continue and get a list of all failed 
tests?





How to do unittests

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

Hello,

can someone give me a complete example please how to do 
unittests? I tried this with the example from german wikipedia, 
but the flag -unittest didn't make any difference.


Re: How to do unittests

2015-09-30 Thread Namal via Digitalmars-d-learn
On Wednesday, 30 September 2015 at 13:03:52 UTC, Rikki Cattermole 
wrote:

On 01/10/15 1:59 AM, Namal wrote:

Hello,

can someone give me a complete example please how to do 
unittests? I

tried this with the example from german wikipedia, but the flag
-unittest didn't make any difference.


Example file with loads of unittests: 
https://github.com/rikkimax/alphaPhobos/blob/master/source/std/experimental/uri.d


If you were to compile it e.g. dmd uri.d it won't be much use 
(unittest wise). You will need to dmd -unittest uri.d to 
compile them in. Don't forget to do the same for your main 
function. When you run the final executable the tests will 
execute before your main function does.


can't I do unittest in the main?


Re: reading file byLine

2015-09-18 Thread Namal via Digitalmars-d-learn
Hello guys, is there a nice functional way to read the file which 
is like



1,2,3,4,5,6
2,3,4,5,6,7
8,9,0,9,2,3

line by line, split numbers and remove each ','
convert it to int and save in a matrix int[][] arr?



Re: reading file byLine

2015-09-18 Thread Namal via Digitalmars-d-learn
On Friday, 18 September 2015 at 10:34:41 UTC, Edwin van Leeuwen 
wrote:

On Friday, 18 September 2015 at 10:26:46 UTC, Namal wrote:
Hello guys, is there a nice functional way to read the file 
which is like



1,2,3,4,5,6
2,3,4,5,6,7
8,9,0,9,2,3

line by line, split numbers and remove each ','
convert it to int and save in a matrix int[][] arr?


Not tested, but I think the following should work:

auto matrix = str
  .byLine
  .map!((l) => l.split(",")// Split each line
.map!(to!int)  // Turn into ints
.array)// Return an array
  .array // Copy into an array


And how do tell here to read my file?


Re: reading file byLine

2015-09-18 Thread Namal via Digitalmars-d-learn
On Friday, 18 September 2015 at 11:37:15 UTC, Edwin van Leeuwen 
wrote:

On Friday, 18 September 2015 at 11:11:51 UTC, Namal wrote:

compiles but crashes


For me it works fine. You probably have extra spaces or 
something in your file. It would help if you posted the error 
message you get when running the program.


Oh, yes, sorry, there was a space after the end of one line


Re: reading file byLine

2015-09-18 Thread Namal via Digitalmars-d-learn
On Friday, 18 September 2015 at 11:06:46 UTC, Edwin van Leeuwen 
wrote:

On Friday, 18 September 2015 at 10:48:25 UTC, Namal wrote:
On Friday, 18 September 2015 at 10:34:41 UTC, Edwin van 
Leeuwen wrote:

On Friday, 18 September 2015 at 10:26:46 UTC, Namal wrote:
Hello guys, is there a nice functional way to read the file 
which is like



1,2,3,4,5,6
2,3,4,5,6,7
8,9,0,9,2,3

line by line, split numbers and remove each ','
convert it to int and save in a matrix int[][] arr?


Not tested, but I think the following should work:

auto matrix = str
  .byLine
  .map!((l) => l.split(",")// Split each line
.map!(to!int)  // Turn into ints
.array)// Return an array
  .array // Copy into an array


And how do tell here to read my file?


Replace str with File("myfile"):

auto matrix = File("myfile")
   .byLine
   .map!((l) => l.split(",")// Split each line
 .map!(to!int)  // Turn into ints
 .array)// Return an array
   .array // Copy into an array




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


void main(){

auto matrix = File("test.txt")
   .byLine
   .map!((l) => l.split(",")// Split each line
 .map!(to!int)  // Turn into ints
 .array)// Return an array
   .array();

matrix.writeln;
}

compiles but crashes


Re: reading file byLine

2015-09-18 Thread Namal via Digitalmars-d-learn
So do I understand it right: does  => in map! indicates a lambda 
function?


Re: Why does reverse also flips my other dynamic array?

2015-09-12 Thread Namal via Digitalmars-d-learn
Why is also b flipped here? This doesn't happen if I use static 
arrays.


nvm. I need to .dup that.


Re: reading file byLine

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

On Monday, 7 September 2015 at 10:28:20 UTC, deed wrote:

On Monday, 7 September 2015 at 10:25:09 UTC, deed wrote:

writeln(x);// or you can pass it to a function.


I meant `writeln(x + 5)`


If I have just red your post before I started using reverse on 
dynamic arrays...
Anyway, there is no .reverse for strings I guess, what is the way 
to completely reverse a string in D?


Sum and other algorithm functions

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

Hello,

how can I define the range for the sum function which I want to 
sum up? For instance how do I sum up the first 3 elements of an 
array


int[] a = [1,2,3,4,5,6,7,8,9];

or the last 3?


Re: reading file byLine

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

On Sunday, 6 September 2015 at 20:39:27 UTC, deed wrote:

On Sunday, 6 September 2015 at 17:57:49 UTC, Namal wrote:

Yeah, I just checked, it is 2.066,  how can I install the new 
version on ubuntu with sudo apt-get?


sudo apt-get install dmd
will give you dmd v2.067.1. Don't know when it will be upgraded 
to 2.068 though.


I used the software center to install the newest one. Now it 
compiles but I have no clue how to use uniq properly. I just tried


uniq(sort(arr));

and

auto arr = sort(a).uniq!("a==b").array;

but I don't get an array with unique elements.


Re: reading file byLine

2015-09-06 Thread Namal via Digitalmars-d-learn
That should be it though... Could you try this minimal complete 
test?


import std.stdio;
import std.algorithm;

void main(string[] args) {
int[] arr = [1, 2, 4, 2, 3, 4, 1];
arr.sort.uniq.writeln;
}

// [1, 2, 3, 4]


yes, it works likte that.

unique(arr) I get

Error: undefined identifier 'unique', did you mean template 
'uniq(alias pred = "a == b", Range)(Range r) if 
(isInputRange!Range && is(typeof(binaryFun!pred(r.front, 
r.front)) == bool))'?





Re: reading file byLine

2015-09-06 Thread Namal via Digitalmars-d-learn
Well, if you don't type function names right, it will be hard 
to help you.


oh, sorry. But I found out what I have been doing wrong besides 
that.


arr.sort.uniq;

uniq(arr) or arr.sort.uniq; compiles but doesn't store it in the 
arr array, I need to store it in a new one.




Re: reading file byLine

2015-09-06 Thread Namal via Digitalmars-d-learn
Are you on 2.066 or older? Back then std.algorithm hasn't been 
split into submodules yet. Just import std.algorithm then 
instead of std.algorithm.comparison, std.algorithm.iteration, 
etc.


Yeah, I just checked, it is 2.066,  how can I install the new 
version on ubuntu with sudo apt-get? I don't realy want to mess 
with is manually. Also tried just to use algorithm and I get same 
error message.


Re: reading file byLine

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

Note that there's a specialized `std.algorithm.iteration.sum`.


is there any function that removes double elements in a sorted 
array?




Re: reading file byLine

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

On Sunday, 6 September 2015 at 15:52:38 UTC, anonymous wrote:

On Sunday, 6 September 2015 at 15:41:34 UTC, Namal wrote:
is there any function that removes double elements in a sorted 
array?


std.algorithm.iteration.uniq

http://dlang.org/phobos/std_algorithm_iteration.html#uniq


Hmm, I get

Error: module comparison is in file 'std/algorithm/comparison.d' 
which cannot be read

import path[0] = /usr/include/dmd/phobos
import path[1] = /usr/include/dmd/druntime/import

when I try to load the headers like in the example


Re: reading file byLine

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

On Saturday, 5 September 2015 at 18:57:52 UTC, deed wrote:

On Saturday, 5 September 2015 at 17:31:39 UTC, Namal wrote:

Yeah, I have have been trying this example from wiki books

https://en.wikibooks.org/wiki/Learning_D_With_Project_Euler

It is not even compiling.


What exactly is not compiling?


the last codesample on the bottom. I think because of the old D? 
Index for the last array.length-1 is now $-1. But also I get


Error: undefined identifier 'file'

for the read line. But even when I fixed those errors the strings 
I got were with those quotation marks and backslashes. However, 
with your help I could solve it now.


I moved to the next problem and wrote the program for it

import std.stdio, std.algorithm, std.array;

bool abundant(int n){

int[] a;

foreach(i;1..n)
if(!(n%i))
a~=i;
auto sum = reduce!((a,b)=>a+b)(0,a);

return sum>n;
}



void main(){

long sum;
int[] arr;
int[28123] mark;

foreach(i;1..28124)
if(abundant(i))
arr~=i;

foreach(i;arr)
foreach(j;arr){

if(i+j>28123)
break;
mark[i+j-1] = 1;
}
for(auto i = 0;i

Re: reading file byLine

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

On Saturday, 5 September 2015 at 14:49:13 UTC, deed wrote:

On Saturday, 5 September 2015 at 14:44:19 UTC, deed wrote:

 .map!(s => chomp(s, "\"")
 .map!(s => chompPrefix(s, "\"")


should be

 .map!(s => chomp(s, "\""))
 .map!(s => chompPrefix(s, "\""))


Yeah, I have have been trying this example from wiki books

https://en.wikibooks.org/wiki/Learning_D_With_Project_Euler

It is not even compiling.


Re: reading file byLine

2015-09-05 Thread Namal via Digitalmars-d-learn
On Friday, 4 September 2015 at 12:09:19 UTC, Edwin van Leeuwen 
wrote:
On Friday, 4 September 2015 at 12:06:08 UTC, Edwin van Leeuwen 
wrote:

On Friday, 4 September 2015 at 11:50:23 UTC, deed wrote:


import std.algorithm, std.range, std.array, std.string, 
std.stdio,

std.conv;

int[] arr1 = [1, 2, 30];
//arr1.max.writeln; // Doesn't work, as you say
arr1.reduce!max.writeln;// This does. Prints 30.


Again using reduce is the functional way to do it. The above 
basically boils down to:


int[] arr1 = [1, 2, 30];
int maxElement = arr1[1];
foreach( element; arr1[2..$] ) //2..$ is short hand for second 
till last ($) element

{
  maxElement = max( maxElement, element );
}
writeln( maxElement );


Sorry been using too much R, so my indexes are off by 1:

int[] arr1 = [1, 2, 30];
int maxElement = arr1[0];
foreach( element; arr1[1..$] ) //1..$ is short hand for second 
till last ($) element

{
  maxElement = max( maxElement, element );
}
writeln( maxElement );



Thx guys. Now I try out the split function. I read the file as a 
single string?


auto arr = split(cast(string)read(filename),",");

where the file has "A", "B", "C"

and I get the output ["\"A\"", " \"B\"", " \"C\"\n"]

I can understand that read functions reads the endl but what does 
it with the quotation marks? how can I modify read so I get just 
["A", "B", "C"]






Re: reading file byLine

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

On Friday, 4 September 2015 at 01:55:13 UTC, deed wrote:

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


Sorry, I don't understand the syntax yet. How do I tell max to 
search all elements? I mean, why does sort(myarray) is legit and 
max(myarray) isn't.


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



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





Re: reading file byLine

2015-09-02 Thread Namal via Digitalmars-d-learn
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);
}


Re: reading file byLine

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

On Wednesday, 2 September 2015 at 13:12:39 UTC, cym13 wrote:

On Wednesday, 2 September 2015 at 13:01:31 UTC, Namal wrote:

Hello,

I want to read a file line by line and store each line in a 
string. I found this example with byLine and ranges. First of 
all, do I need the range lib at all to do this and if so what 
is the range of the end of the file?


You don't need the range lib at all, std.range provides 
advanced functions to work with ranges but ranges are a general 
concept. You need std.stdio though as this is doing file 
operations.


A way to do it is:

void main() {
auto f = File("myfile");
string buffer;

foreach (line ; f.byLine) {
buffer ~= line;
}

f.close();
writeln(buffer);
}

Note that by default byLine doesn't keep the line terminator. 
See http://dlang.org/phobos/std_stdio.html#.File.byLine for 
more informations.


Thx, cym. I have a question about a D strings though. In c++ I 
would just reuse the string buffer with the "=" how can I clear 
the string after i store a line in the buffer and do something 
with it. I also tried to append a line to an array of strings but 
it failed because the line is a char?


reading file byLine

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

Hello,

I want to read a file line by line and store each line in a 
string. I found this example with byLine and ranges. First of 
all, do I need the range lib at all to do this and if so what is 
the range of the end of the file?


Re: array function

2015-08-31 Thread Namal via Digitalmars-d-learn

On Monday, 31 August 2015 at 11:27:20 UTC, Rikki Cattermole wrote:


You cannot define static arrays using runtime information.
You must use dynamic arrays.

int[] foo(int N) {
int[] v;
v.length = N;
// do something with it
int[] 2;
return s;
}


Of course you can pass it in via a template argument. But this 
of course does not work at runtime like you are wanting.



Hmm, this has never been a problem for me in C++

#include 


std::vector foo(int N){

std::vector V(N);
int some_array[N];

std::vector other_V;

return other_V;

}

int main(){

std::vector V = foo(12);


}

compiles



array function

2015-08-31 Thread Namal via Digitalmars-d-learn

Hello,

can someone explain to me please what I am doing wrong by passing 
an integer to this function and then just creating a static 
array? The error I get is:


Error: variable N cannot be read at compile time

int[] foo(int N){


int[N] v;
//do something with it
int[] s;
return s;
}

void main(){

int N = 12;
int[] A;
A=prim_numbers(N);

}


Re: array function

2015-08-31 Thread Namal via Digitalmars-d-learn
Hey guys, since I am learning D arrays here, can you tell me the 
best way to remove an element at the end of an array or at some 
index i?


Re: array function

2015-08-31 Thread Namal via Digitalmars-d-learn

On Monday, 31 August 2015 at 12:00:26 UTC, bearophile wrote:

Namal:


std::vector foo(int N){

std::vector V(N);
int some_array[N];


VLAs are not present in D.

Bye,
bearophile


Yah, I guess I have been damaged with them when I started to 
learn programming in C++ >(


Setting um makefile for dmd

2015-06-30 Thread Namal via Digitalmars-d-learn

Hello,

i get myself a nice makefile from here:

https://gist.github.com/darkstalker/2221824

How can I modify it so that I can use the run option of dmd? I 
tried to add another compiler flag variable which would be only 
used with run but then it messed with -of$@ option and said it 
couldn't read it.


Thanks