[Beginner]Variable length arrays

2017-02-25 Thread helxi via Digitalmars-d-learn
I am trying to create an array which has a user defined size. However the following program is not compiling: import std.stdio; void main(){ write("Enter your array size: "); int n; readf(" %s", ); int[n] arr; //<-Error: variable input cannot be read at compile time

Re: [Beginner]Variable length arrays

2017-02-25 Thread helxi via Digitalmars-d-learn
On Saturday, 25 February 2017 at 14:34:31 UTC, rikki cattermole wrote: On 26/02/2017 3:31 AM, helxi wrote: I am trying to create an array which has a user defined size. However the following program is not compiling: import std.stdio; void main(){ write("Enter your array size: ");

foreach, is and pointer

2017-03-26 Thread helxi via Digitalmars-d-learn
What's the difference between 1. string x = "abcd"; foreach(character; x) write(character); and string x = "abcd"; foreach(character; x[0..$]) write(character); 2. is and == 3. pointer and address and reference?

Sorting Assosiative Arrays and Finding Largest Common Substring

2017-03-16 Thread helxi via Digitalmars-d-learn
I was looking for ways to find the largest common substring between two given substrings and have learnt 1. .length is of type ulong 2. writing string[int] will not give me a sorted array 3. ulong cannot be sorted by sorted What's the trick to sort the associative array by their keys? Code

Best ways to declare associative arrays

2017-03-12 Thread helxi via Digitalmars-d-learn
How would an experienced programmer declare an associative array of strings that has 2 keys? My initial impression was string[string][2] my_array; which does not seem to work. Here is a snippet of the code I am working on: import std.string; import std.stdio; string[string] change(ref

Re: Best ways to declare associative arrays

2017-03-12 Thread helxi via Digitalmars-d-learn
On Sunday, 12 March 2017 at 07:58:40 UTC, helxi wrote: return def; I meant return arg_array;

Can't get expected strings

2017-07-18 Thread helxi via Digitalmars-d-learn
import std.stdio, std.datetime, std.conv, std.algorithm; void main() { immutable DEADLINE = DateTime(2017, 7, 16, 23, 59, 59).to!SysTime; immutable NOW = Clock.currTime; immutable INTERVAL = (DEADLINE - NOW) .abs .to!string;

Re: [BEGINNER] reccurence! and sequence!

2017-07-06 Thread helxi via Digitalmars-d-learn
On Thursday, 6 July 2017 at 00:21:44 UTC, Ali Çehreli wrote: On 07/05/2017 04:38 PM, helxi wrote: >> [...] > > Oh thank you. Just 2 follow-up questions: >> [...] > 1. In the last example of reccurence, what does n in (a,n) refer to? n is "the index of the current value". Each time the lambda

Re: [BEGINNER] reccurence! and sequence!

2017-07-05 Thread helxi via Digitalmars-d-learn
On Monday, 26 June 2017 at 10:34:22 UTC, ag0aep6g wrote: On 06/26/2017 11:51 AM, helxi wrote: [...] `a` is a tuple of the run-time arguments you pass to `sequence`. In this example, no arguments are passed (empty parens at the end of the call), so `a` is empty. [...] a[0] = 1 a[1] = 2

[BEGINNER] reccurence! and sequence!

2017-06-26 Thread helxi via Digitalmars-d-learn
Can someone give me a very watered-down explanation of what std.range's recurrence! and sequence! do? auto tri = sequence!((a,n) => n*(n+1)/2)(); /** okay, it's a triangular number array * I understand n is the index number, the nth term * However where does this 'a' go? */ auto odds =

Input interrupt

2017-05-28 Thread helxi via Digitalmars-d-learn
Hello, I just wrote a mock-up of Unix's $cat. However unlike the actual $cat, when input interrupt (Cntrl+D) is pressed the following program does not stop. So I tried using C's EOF but the types aren't compatible since EOF is probably aliased to -1 //... if (args.length < 2) {

Re: Input interrupt

2017-05-28 Thread helxi via Digitalmars-d-learn
On Sunday, 28 May 2017 at 22:14:46 UTC, Adam D. Ruppe wrote: On Sunday, 28 May 2017 at 22:07:12 UTC, helxi wrote: So I tried using C's EOF but the types aren't compatible since EOF is probably aliased to -1 The readln docs for D say it returns null on end of file. The example given is:

O(1) sum

2017-06-11 Thread helxi via Digitalmars-d-learn
Is it possible to sum an array in O(1)?

byLine(n)?

2017-06-10 Thread helxi via Digitalmars-d-learn
I was writing a program that reads and prints the first nth lines to the stdout: import std.stdio; void main(string[] args) { import std.algorithm, std.range; import std.conv; stdin.byLine.take(args[1].to!ulong).each!writeln; } As far as I understand the

Re: byLine(n)?

2017-06-11 Thread helxi via Digitalmars-d-learn
On Sunday, 11 June 2017 at 06:28:18 UTC, Stanislav Blinov wrote: On Sunday, 11 June 2017 at 05:36:08 UTC, helxi wrote: I was writing a program that reads and prints the first nth lines to the stdout: import std.stdio; void main(string[] args) { import std.algorithm, std.range; import

Re: byLine(n)?

2017-06-11 Thread helxi via Digitalmars-d-learn
On Sunday, 11 June 2017 at 12:49:51 UTC, Cym13 wrote: print each line byLine doesn't reall all input at once. Using byline and take you are effectively reading only the right amount of lines and not reading the rest. You already have what you want, what makes you think the contrary? Oh it

opCast'ing strings

2017-11-12 Thread helxi via Digitalmars-d-learn
struct Fraction { private: int numerator = 1, denominator = 1; public: string opCast(T : string)() const { import std.conv : to; return numerator.to!string() ~ "/" ~ denominator.to!string(); } } void main() { import

Taking a constant reference to a constant/non const object

2017-11-15 Thread helxi via Digitalmars-d-learn
Hi. What function signature should I use for receiving a constant reference of an r/l value object? Is it auto fn(inout ref const myClass obj)? I want to: 1. Take a constant reference of the object, not copy them 2. The object itself may be const or non const.

Re: Taking a constant reference to a constant/non const object

2017-11-15 Thread helxi via Digitalmars-d-learn
On Wednesday, 15 November 2017 at 09:23:53 UTC, Jonathan M Davis wrote: On Wednesday, November 15, 2017 09:04:50 helxi via Digitalmars-d-learn wrote: Hi. What function signature should I use for receiving a constant reference of an r/l value object? Is it auto fn(inout ref const myClass obj

Template specialisation, "Generic type locking", offline stdlib docs and case-based template question

2017-11-30 Thread helxi via Digitalmars-d-learn
1. Template specialisation. Why is this useful?: T getResponse(T = int)(string question); And how does it differ from int getResponse(string question); ? Context: http://ddili.org/ders/d.en/templates.html : Section: "Default template parameters" 2. "Generic locking". Is it possible to

Private imports and Objects

2017-11-29 Thread helxi via Digitalmars-d-learn
1. Why are imports visible from outside the package when you do selective imports? //mod.d module mod; import std.stdio : writeln; public void greet() { writeln("Hello"); } //app.d import mod; void main() { mod.greet(); writeln("You should not be seeing this."); }

Re: Private imports and Objects

2017-11-30 Thread helxi via Digitalmars-d-learn
On Thursday, 30 November 2017 at 06:44:43 UTC, Jonathan M Davis wrote: On Thursday, November 30, 2017 06:29:43 helxi via Digitalmars-d-learn wrote: [] I don't understand the question. You're asking whether casting from a base class to a derived class creates overhead? Or are you asking

Using enum types

2017-12-04 Thread helxi via Digitalmars-d-learn
Why can't enums be used as types in this (simplified) example? enum Positivity { Positive, Negative } struct Wave { public: Positivity slope; } enum Waves { Sin = Wave(Positivity.Positive), Cos = Wave(Positivity.Negative) } int nth_value(T : Waves)(int

Re: Taking a constant reference to a constant/non const object

2017-11-15 Thread helxi via Digitalmars-d-learn
On Wednesday, 15 November 2017 at 09:34:32 UTC, helxi wrote: On Wednesday, 15 November 2017 at 09:23:53 UTC, Jonathan M Davis wrote: On Wednesday, November 15, 2017 09:04:50 helxi via Digitalmars-d-learn wrote: Hi. What function signature should I use for receiving a constant reference of an r

Seperating class methods in a different module

2017-12-06 Thread helxi via Digitalmars-d-learn
1. How can I separate class methods from the declaration block? And how can I implement them in a separate module? module frame; class Test { public: int x; this(); } Test.this() { x = 34; } // does not work In this scenario I would like to take the constructor to a

Optimizing a bigint fibonacci

2017-12-06 Thread helxi via Digitalmars-d-learn
This is question not directly related to language concepts, it's got more to do with the application. I would appreciate if anyone would point to me how I could optimize this bit of code auto fib(const int n) { import std.bigint; if (n == 0) return BigInt(0);

Re: Optimizing a bigint fibonacci

2017-12-06 Thread helxi via Digitalmars-d-learn
On Wednesday, 6 December 2017 at 10:00:48 UTC, Biotronic wrote: On Wednesday, 6 December 2017 at 09:12:08 UTC, helxi wrote: [...] Here's my version:, based on fast squaring: auto fib(ulong n) { import std.bigint : BigInt; import std.meta : AliasSeq; import std.typecons : tuple;

Avoiding default generic types, and allocator awareness

2017-12-30 Thread helxi via Digitalmars-d-learn
As an exercise in http://ddili.org/ders/d.en/pointers.html, I was implementing a very stripped down version of singly linked list like below: struct Node(T) { T item; Node!T* next_item; } string to_string(T)(in Node!T node) { import std.format; return node.nextItem ? "%s ->

Re: Avoiding default generic types, and allocator awareness

2017-12-30 Thread helxi via Digitalmars-d-learn
On Saturday, 30 December 2017 at 15:00:32 UTC, helxi wrote: As an exercise in http://ddili.org/ders/d.en/pointers.html, I was implementing a very stripped down version of singly linked list like below: struct Node(T) { T item; Node!T* next_item; } [...] Correction, I meant: If I

Curiously Recurring Template Pattern Example

2017-12-25 Thread helxi via Digitalmars-d-learn
Hi, Is there any blogs that discuss CRTP, or even Policy based/introspection based design in idiomatic D? I would love to see the strategies used to tackle the overhead involving dynamic dispatch by emulating static polymorphism. Thanks.

Re: Is std.variant.visit not @nogc?

2018-04-09 Thread helxi via Digitalmars-d-learn
On Monday, 9 April 2018 at 15:59:32 UTC, Paul Backus wrote: On Monday, 9 April 2018 at 07:07:58 UTC, Chris Katko wrote: [...] I agree in general, but in this case it's actually completely doable. In fact, I've done it myself: check out 'sumtype' on code.dlang.org. You can replace

Is std.variant.visit not @nogc?

2018-04-08 Thread helxi via Digitalmars-d-learn
import std.variant, core.stdc.stdio; Algebraic!(T, string) fib_nth(T)(T n) { return n % 15 ? n % 5 ? n % 3 ? Algebraic!(T, string)(n) : Algebraic!(T, string)("Fizz") : Algebraic!(T,

What is the equivalent of C++'s std::optional and std::nullopt in D?

2018-04-02 Thread helxi via Digitalmars-d-learn
For reference: https://en.cppreference.com/w/cpp/utility/optional

Re: Where do I learn to use GtkD

2018-10-29 Thread helxi via Digitalmars-d-learn
On Sunday, 13 March 2016 at 19:28:57 UTC, karabuta wrote: Gtk3 from python3 has got I nice book with examples that are not so advanced but enough to get you doing real work(from a beginner point of view). GtkD seem to have changed the API structure compared to python3 Gtk3 and the demo

Re: Where do I learn to use GtkD

2018-10-31 Thread helxi via Digitalmars-d-learn
On Tuesday, 30 October 2018 at 14:38:53 UTC, Michelle Long wrote: On Sunday, 13 March 2016 at 19:28:57 UTC, karabuta wrote: Gtk3 from python3 has got I nice book with examples that are not so advanced but enough to get you doing real work(from a beginner point of view). GtkD seem to have

Exception slipping through the catch block?

2018-11-08 Thread helxi via Digitalmars-d-learn
How does exception work? I am inside a function that calls a constructor. Inside the constructor, an exception is thrown. However even though I have wrapped the body of the function inside a try/catch block, the program crashes from inside that constructor. Shouldn't the catch block in the

Reading into the output of a long running shellExecute

2018-11-10 Thread helxi via Digitalmars-d-learn
Hi. I have not done any multi-threaded programming before. What I basically want is to read into the output of a long shellExecute function each second. In details, I am calling shellExecute("pkexec dd if=/path/to/file of=/dev/sdx status=progress && sync"); It's a long running process and dd

Re: Reading into the output of a long running shellExecute

2018-11-12 Thread helxi via Digitalmars-d-learn
On Saturday, 10 November 2018 at 15:54:07 UTC, JN wrote: On Saturday, 10 November 2018 at 15:05:38 UTC, helxi wrote: Hi. I have not done any multi-threaded programming before. What I basically want is to read into the output of a long shellExecute function each second. In details, I am

How does calling function pointers work?

2018-11-12 Thread helxi via Digitalmars-d-learn
As far as I understand, calling a function pointer with an argument in D looks like: call(, argTofn0, argTofn1, argTofn3); This immediately struck me a very weak syntax to me so I decided to explore my concerns. I made a function pointer that takes an indefinite number of arguments.

Re: How does calling function pointers work?

2018-11-12 Thread helxi via Digitalmars-d-learn
On Monday, 12 November 2018 at 16:08:28 UTC, helxi wrote: Line 12 was meant to print 1234. Line 13 was meant to print 1234 too, but for a different reason. Correction, it was meant to print 12304. My bad.

Re: Exception slipping through the catch block?

2018-11-08 Thread helxi via Digitalmars-d-learn
On Thursday, 8 November 2018 at 15:41:11 UTC, Adam D. Ruppe wrote: On Thursday, 8 November 2018 at 15:08:40 UTC, helxi wrote: Shouldn't the catch block in the function catch the exception? You caught Exception, but it throws Error. They have separate inheritance trees. The common ancestor

Re: How does calling function pointers work?

2018-11-12 Thread helxi via Digitalmars-d-learn
On Monday, 12 November 2018 at 16:25:13 UTC, Rene Zwanenburg wrote: Idk where you got that syntax from, but there's no syntactic difference between calling normal functions and function pointers: import std.stdio; import std.concurrency; import core.thread; void worker(int firstNumber) {

Re: Prevent opening binary/other garbage files

2018-10-01 Thread helxi via Digitalmars-d-learn
On Sunday, 30 September 2018 at 03:19:11 UTC, Adam D. Ruppe wrote: On Saturday, 29 September 2018 at 23:46:26 UTC, helxi wrote: Thanks. Would you say https://dlang.org/library/std/encoding/get_bom.html is useful in this context? Eh, not really, most text files will not have one. Hi, I

Re: Prevent opening binary/other garbage files

2018-09-29 Thread helxi via Digitalmars-d-learn
On Saturday, 29 September 2018 at 16:01:18 UTC, Adam D. Ruppe wrote: On Saturday, 29 September 2018 at 15:52:30 UTC, helxi wrote: I'm writing a utility that checks for specific keyword(s) found in the files in a given directory recursively. What's the best strategy to avoid opening a bin file

Prevent opening binary/other garbage files

2018-09-29 Thread helxi via Digitalmars-d-learn
I'm writing a utility that checks for specific keyword(s) found in the files in a given directory recursively. What's the best strategy to avoid opening a bin file or some sort of garbage dump? Check encoding of the given file? If so, what are the most popular encodings (in POSIX if that

Re: task can't take a class method

2018-11-19 Thread helxi via Digitalmars-d-learn
On Monday, 19 November 2018 at 17:18:14 UTC, John Chapman wrote: 1) task() Thanks, that helped.

task can't take a class method

2018-11-19 Thread helxi via Digitalmars-d-learn
I want to create a task out of an object's method. My Class is: public class Calldd { private: const string tmpFileName = "/tmp/nixwriter.progress.txt"; string deviceName, sourceFileName; public: this(in string sourceFileName, in string deviceName) { this.sourceFileName

Re: task can't take a class method

2018-11-19 Thread helxi via Digitalmars-d-learn
On Monday, 19 November 2018 at 16:10:15 UTC, helxi wrote: ... Oh wait never mind I was missing a bracket: auto proc = task!(ddCall.dd()); Now I have another thing to worry about: ddcall.dd() cannot be read at compile time.