Hello,
I am new to D. According to that power of 2 rule I want to
reserve 2 sized chunks to my array, But how do I do that in a
struct? Say:
struct Stack(int){
int a[];
}
Where do I put that a.reserve(2);
Because right after the declaration of a I get an error
Error: unexpected ( in decla
So the task is to write a struct object for the saturation
arithmetic. I tried first to write it for the unsigned Types:
struct Saturated(T)
if (isIntegral!T)
{
static assert (isUnsigned!T || isSigned!T);
@property
{
static Saturated min() { retu
255 - 129 is less than 128 so the result is T.max, which is
255, which is not equal to 0.
I dont understand this at all 255 - 129 should be 126 in ubyte or
not?
Thank you very much, I thought the operators are alrdy checked by
if (op == "+" || op == "-" || op == "/")
But I did same tests for ushort uint and ulong, but for ulong it
didn't compile.
unittest{
alias sulong = Saturated!ulong;
assert(sulong(18_446_744_073_709_551_610) + sulong(2)
On Saturday, 25 May 2013 at 10:15:42 UTC, bearophile wrote:
Namal:
And if so, do I always have to use a suffix when the number is
bigger than uint?
It looks a bit silly, I agree.
Bye,
bearophile
Well, now I have same Error for signed long:
else{
static if (op
Thanks, that helped me alot.
I have one more question towards using unsigned datatype
assert(sint(-2_147_483_647) - sint(3) == sint(-2_147_483_648));
assert(sint(-2_147_483_647) - sint(3) == sint(2_147_483_648));
Here I get an error for the second line, because it cannot be
convertet
if i use unsigned
assert(sint(
Hello, what is the way to remove whitespace from a string (not
only at the beginning and end)..
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 ?
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
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<
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<
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++){
w
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
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
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;
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 w
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.algo
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
Is there any other way to do a simple user input into integer
variables without using std.conv?
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?
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]
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
Is it possible to use foreach backwards?
foreach(int i;20..1)
writeln(i);
compiles but I get nothing.
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.
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
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
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
Is there a way to detect overflow for example for:
int i = 2_000_000_000;
int a = i*i*i;
writeln(a);
-> 1073741824
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
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
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
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?
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
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
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 ??
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
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 look
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
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;
}
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 qu
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;
}
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 e
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
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
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 m
Hello,
I am looking for data structure equivalent to std::set from C++.
Couldn't find it in the documentation.
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.
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
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;
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 re
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[]
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 y
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++ >(
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?
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?
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
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 rang
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
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 instan
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
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
Er
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)
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");
au
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
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 Th
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?
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 understan
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,
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 exampl
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
Note that there's a specialized `std.algorithm.iteration.sum`.
is there any function that removes double elements in a sorted
array?
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
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'
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
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 identifi
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.
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?
import std.stdio;
void main(){
int [] a = [1,2,3,4,5];
int [] b = a;
writeln(b, " ", a);
a.reverse;
writeln(b, " ", a);
}
I get:
[1, 2, 3, 4, 5] [1, 2, 3, 4, 5]
[5, 4, 3, 2, 1] [5, 4, 3, 2, 1]
Why is also b flipped here? This doesn't happen if I use static
array
Why is also b flipped here? This doesn't happen if I use static
arrays.
nvm. I need to .dup that.
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
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?
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
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
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
So do I understand it right: does => in map! indicates a lambda
function?
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.
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 diffe
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
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
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.
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.
92 matches
Mail list logo