Re: Trouble using 'sort'

2016-07-26 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/26/2016 11:42 AM, drug wrote: > Another option is `makeIndex` (std.algorithm.sorting) and then sorting > of that index. That's an interesting option; at least I don't have to touch the range. Thanks. -- Bahman

Re: Trouble using 'sort'

2016-07-26 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/26/2016 10:41 AM, Jonathan M Davis via Digitalmars-d-learn wrote: >> So it may be something about what kind of range I'm passing to `sort`. >> Am I right? > > sort requires a random access range. Without knowing exactly which > algorithms your using, I can't say for sure that that's the

Re: Trouble using 'sort'

2016-07-25 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/26/2016 10:11 AM, Bahman Movaqar wrote: > Alright...further experiments. The following works: > > sort!((pp1, pp2) => cmp(pp1.price, pp2.price) > 0)(theRange) > > So it may be something about what kind of range I'm passing to `sort`. > Am I right? > I meant sort!((pp1, pp2) =>

Re: Trouble using 'sort'

2016-07-25 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/26/2016 09:35 AM, Bahman Movaqar wrote: > I have a range which is the result of a couple of chained range > operations, and each element is: > > Tuple!(string, "product", double, "price") > > Now I'd like to sort the range by "price" using: > > sort!((pp1, pp2) => cmp(pp1.price,

Trouble using 'sort'

2016-07-25 Thread Bahman Movaqar via Digitalmars-d-learn
I have a range which is the result of a couple of chained range operations, and each element is: Tuple!(string, "product", double, "price") Now I'd like to sort the range by "price" using: sort!((pp1, pp2) => cmp(pp1.price, pp2.price) > 0)(theRange) But I get a compile time error:

Re: Trouble checking for null-ness

2016-07-25 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/25/2016 05:47 PM, Adam D. Ruppe wrote: > On Monday, 25 July 2016 at 13:09:22 UTC, Bahman Movaqar wrote: >> From what I could gather, it's not possible to check for `null` at >> runtime for reference based types. Am I right? > > No, it is only possible to check for null for reference based

Re: Trouble checking for null-ness

2016-07-25 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/25/2016 05:07 PM, Bahman Movaqar wrote: > Suppose I have the following function: > > public auto max(alias comp, Range)(Range r) > in { > assert(r !is null && !r.empty); > } > body { > // ... > } > > When the function after a series of chained `map`

Trouble checking for null-ness

2016-07-25 Thread Bahman Movaqar via Digitalmars-d-learn
Suppose I have the following function: public auto max(alias comp, Range)(Range r) in { assert(r !is null && !r.empty); } body { // ... } When the function after a series of chained `map` operations, I get the following error: Error: incompatible types for

Re: unittests not being run

2016-07-16 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/17/2016 12:38 AM, Seb wrote: > There is no need for a module, but dub by default only checks files in > the 'source' folder. The file is already in the 'source' folder. > For such simple tests you could also run them directly with rdmd > -unittest. There is an additional -main flag if you

Re: unittests not being run

2016-07-15 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/15/2016 04:16 PM, Jerry wrote: > Unittests have to be inside a module to be run on DMD atleast. > So putting module foo at top should fix it. Strange. Still not getting picked up. $ dmd --version DMD64 D Compiler v2.071.0 Copyright (c) 1999-2015 by Digital Mars written by

unittests not being run

2016-07-15 Thread Bahman Movaqar via Digitalmars-d-learn
The test I have in 'app.d' don't get picked up by 'dub test' in a freshly created project by 'dub init'. $ dub test Generating test runner configuration '__test__library__' for 'library' (library). Performing "unittest" build using dmd for x86_64. dplay ~master: building

Experimenting with templates

2016-07-13 Thread Bahman Movaqar via Digitalmars-d-learn
Following up my D practices, I've created a 'groupBy' template[1] for Stockman (my practice project). I'd like to ask you more experienced folks to please take a look at it. As this is my first template, I'd like to know if I am doing anything idiomatically/logically wrong. PS: I've also

Re: Docs for `Group` type

2016-07-12 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/12/2016 04:21 PM, Edwin van Leeuwen wrote: > On Tuesday, 12 July 2016 at 11:40:48 UTC, Bahman Movaqar wrote: >> On 07/12/2016 01:01 PM, Mike Parker wrote: >>> Do you have some sample code that shows the error? >> >> Yes. I'm working on Stockman[1] a playground to learn D. >> In file

Re: Docs for `Group` type

2016-07-12 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/12/2016 04:23 PM, ag0aep6g wrote: > On 07/12/2016 01:40 PM, Bahman Movaqar wrote: >> Yes. I'm working on Stockman[1] a playground to learn D. >> In file `etl.d`, line 110 [2], if I change the line to >> auto refInvoice = group[1].takeOne(); >> the file will not compile. I have

Re: Docs for `Group` type

2016-07-12 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/12/2016 01:01 PM, Mike Parker wrote: > Do you have some sample code that shows the error? Yes. I'm working on Stockman[1] a playground to learn D. In file `etl.d`, line 110 [2], if I change the line to auto refInvoice = group[1].takeOne(); the file will not compile. I have attached

Re: Docs for `Group` type

2016-07-12 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/12/2016 11:06 AM, Mike Parker wrote: > The 'Group' type is an implementation detail -- a type used internally > -- that you aren't supposed to care about. All you need to care about is > that it's a range. The documentation for chunkBy [1] explains what the > return type is. > > [1]

Re: Passing ranges around

2016-07-12 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/12/2016 11:07 AM, Mike Parker wrote: > auto foo(R)(R r) { ... } That did it. Thanks. Out of curiosity, does the same pattern apply to functions which take `tuple`s as input arguments? -- Bahman

Docs for `Group` type

2016-07-11 Thread Bahman Movaqar via Digitalmars-d-learn
When using `chunkBy` (unary pred) the result is a list of tuples. Each tuple holds a key and a `Group` which belong to that key. Where can I find the docs for this `Group` type (I have already tried searching library on dlang.org)? Thanks, -- Bahman

Re: Associative Array c'tor

2016-07-11 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/11/2016 07:15 PM, Ali Çehreli wrote: > Both AAs and slices behave like reference types even when passed by > value: When a function adds an element, the argument sees that element > as well. This is not the case when the argument is an empty (more > correctly, null) AA or slice: > > void

Re: How to use `format` to repeat a character

2016-07-11 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/11/2016 03:02 PM, Mike Parker wrote: > You can do it in D with custom format specifiers. See: > > https://wiki.dlang.org/Defining_custom_print_format_specifiers Thanks for the pointer. I'll keep that in mind. -- Bahman

Re: Associative Array c'tor

2016-07-11 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/11/2016 06:30 PM, Steven Schveighoffer wrote: > Untested, but you could try MySt[][string].init. That did it. Thanks. > But passing empty AA by value sometimes can be surprising. I'm not sure > if it will work. Could you elaborate more? -- Bahman

Associative Array c'tor

2016-07-11 Thread Bahman Movaqar via Digitalmars-d-learn
I'm processing a list of structs (MySt) with `reduce` to produce an associate array of type `MySt[][string]`. Right now I'm using the following (slimmed down) code: MySt[][string] result; return reduce!( function MySt[][string](MySt[][string] acc, MySt val) { // do something

Re: How to use `format` to repeat a character

2016-07-11 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/11/2016 02:44 PM, ketmar wrote: > On Monday, 11 July 2016 at 09:31:49 UTC, Ali Çehreli wrote: >> What makes you expect that format should have that feature? :) I somehow recalled I could do that in C and then there was the "minimum field width" in the docs, so I thought it's possible I'm

How to use `format` to repeat a character

2016-07-11 Thread Bahman Movaqar via Digitalmars-d-learn
I'm sure I'm missing something very simple but how can I create a string like "" using `format`? I check the docs on `format` and tried many variations including `format("%.*c\n", 4, '-')` but got nowhere. I'd appreciate any hint/help on this. -- Bahman Movaqar http://BahmanM.com -

Difference between back (`) and double (") quoted strings

2015-09-12 Thread Bahman Movaqar via Digitalmars-d-learn
Is there any or they are just simply syntactically equivalent? Are there any official docs on this? -- Bahman Movaqar http://BahmanM.com - https://twitter.com/bahman__m https://github.com/bahmanm - https://gist.github.com/bahmanm PGP Key ID: 0x6AB5BD68 (keyserver2.pgp.com) signature.asc

Re: How To: Passing curried functions around

2015-09-12 Thread Bahman Movaqar via Digitalmars-d-learn
On Friday, 11 September 2015 at 21:06:32 UTC, Ali Çehreli wrote: On 09/11/2015 02:04 PM, Ali Çehreli wrote: The same keyword has a different use with templates: And the official documentation: http://dlang.org/template.html#TemplateAliasParameter Thanks again!

Re: Difference between back (`) and double (") quoted strings

2015-09-12 Thread Bahman Movaqar via Digitalmars-d-learn
On 09/12/2015 12:52 PM, NX wrote: > What if I told you, you should search the official reference before > asking such things in the forum? I did search the net for terms such as "d lang back quoted string" or "d lang multi line string" or "d lang string interpolation" before asking here. However

Re: Adjacent Pairs Range

2015-09-12 Thread Bahman Movaqar via Digitalmars-d-learn
On 09/12/2015 02:47 PM, "Nordlöw" wrote: > How do I most elegantly iterate all the adjacent pairs in an > `InputRange` using Phobos? > > Something like > > [1,2,3,4] => [(1,2), (2,3), (3,4)] That's call `collate`ing IIRC. A quick solution would be using `std.range.transposed`: auto a =

Re: Adjacent Pairs Range

2015-09-12 Thread Bahman Movaqar via Digitalmars-d-learn
On 09/12/2015 04:04 PM, Bahman Movaqar wrote: > Oops! Here's one using only `InputRange` interface: I believe I need to warn you that I'm just learning D; so take my solution at your own risk :-) -- Bahman Movaqar

Re: Adjacent Pairs Range

2015-09-12 Thread Bahman Movaqar via Digitalmars-d-learn
On 09/12/2015 03:09 PM, "Nordlöw" wrote: > InputRange please, not RandomAccessRanges ;) Oops! Here's one using only `InputRange` interface: T[][] collate(T)(T[] a) { alias CollateResult = Tuple!(T[][], "result", T, "tlHd"); CollateResult _collate(CollateResult collres)

Multiple implicit type converters

2015-09-11 Thread Bahman Movaqar via Digitalmars-d-learn
As only one `alias this` is possible for any type, how should one implement multiple implicit type converters? Actually I'm looking for something similar to Groovy's `asType` method[1]. An example in Groovy: Point p = new Point(1, 1) assert (p as BigDecimal[]) == [1, 1] assert

Re: What is "FilterResult" type?

2015-09-11 Thread Bahman Movaqar via Digitalmars-d-learn
On Wednesday, 9 September 2015 at 13:16:49 UTC, cym13 wrote: True. But is pumping the output of `filter` as the seed into `reduce` really considered weird usage!? I don't think it is really weird per se, I just can't think of a case where there isn't a better way to do it. I find it

Re: How To: Passing curried functions around

2015-09-11 Thread Bahman Movaqar via Digitalmars-d-learn
On Friday, 11 September 2015 at 06:14:18 UTC, Ali Çehreli wrote: On 09/06/2015 12:05 PM, Bahman Movaqar wrote: > alias bool function(int n) validator_t; There is the relatively newer alias syntax which is more intuitive: alias Validator = bool function(int n); Great. This is easily

Re: Multiple implicit type converters

2015-09-11 Thread Bahman Movaqar via Digitalmars-d-learn
On Friday, 11 September 2015 at 19:51:09 UTC, Dave Akers wrote: That's enough for me, I suppose. I am thinking of having a family of functions in my structs/classes as `as` family, such as `asDouble`, `asFooBar`. Would it be possible to create it as an 'as' template? Hmm...there's already

Re: Multiple implicit type converters

2015-09-11 Thread Bahman Movaqar via Digitalmars-d-learn
On Friday, 11 September 2015 at 16:33:52 UTC, Meta wrote: The only ways to get implicit conversion between two types in D are through `alias this`, inheritance, or implementing an interface. That's enough for me, I suppose. I am thinking of having a family of functions in my structs/classes

Re: Multiple implicit type converters

2015-09-11 Thread Bahman Movaqar via Digitalmars-d-learn
On Friday, 11 September 2015 at 16:31:46 UTC, Adam D. Ruppe wrote: explicit is the only way to go. That's easy to do, just write like a .get method or something that does the conversion and returns it. Fair enough. Type conversion is one of those spots that I'd like it to as explicit as

Re: How To: Passing curried functions around

2015-09-11 Thread Bahman Movaqar via Digitalmars-d-learn
On Friday, 11 September 2015 at 18:39:15 UTC, Ali Çehreli wrote: >> import std.stdio; >> >> bool isEven(int n) { >> return !(n % 2); >> } >> >> int readValidInt(alias validator)(string prompt) { readValidInt() is a function template that takes two information: 1) The validator as its

Re: What is "FilterResult" type?

2015-09-09 Thread Bahman Movaqar via Digitalmars-d-learn
On Wednesday, 9 September 2015 at 07:59:57 UTC, Sebastiaan Koppe wrote: What is going wrong is that the types aren't the same. That is, the type of the seed you supplied - `typeof(foobars)` - isn't the type that your function returns - `typeof(acc.filter!...)`. Alright. So, `reduce` initial

Re: What is "FilterResult" type?

2015-09-09 Thread Bahman Movaqar via Digitalmars-d-learn
On Tuesday, 8 September 2015 at 18:45:33 UTC, Jonathan M Davis wrote: If you're returning a range, you should be returning auto. @Jonathan, @cym13 and @Meta It's reasonable to use `auto`. However there are times when you need to pass the `auto` value to another function and the receiving

Re: What is "FilterResult" type?

2015-09-09 Thread Bahman Movaqar via Digitalmars-d-learn
On Wednesday, 9 September 2015 at 08:29:20 UTC, cym13 wrote: You are using reduce in a weird way here... Oh? Perhaps it was all because of the lame example I used :-) The real problem I was trying to solve, source of which I just pushed[1], was the `select` method on line 130. Is this

Re: What is "FilterResult" type?

2015-09-09 Thread Bahman Movaqar via Digitalmars-d-learn
On Wednesday, 9 September 2015 at 09:08:28 UTC, Atila Neves wrote: No, it doesn't. It needs to know what the compile-time interface is, i.e. what it can do with that type. If the type in question happens to be an InputRange, then the consumer function would be: void func(R)(R range)

What is "FilterResult" type?

2015-09-08 Thread Bahman Movaqar via Digitalmars-d-learn
From what I can gather the output of `std.algorithm.iteration : filter` is a `FilterResult` type. I need a bit of help dealing with this type: 1. Why this type is there in the first place instead of simply using the type of input range? 2. Where is the documentation for this type? The

Re: What is "FilterResult" type?

2015-09-08 Thread Bahman Movaqar via Digitalmars-d-learn
On Tuesday, 8 September 2015 at 10:08:03 UTC, cym13 wrote: Filter is a template and returns a FilterResult range which is used to lazily compute the result. This behaviour is the same for map and the majority of functions in std.algorithm. Ah...now it makes sense why use a proxy to the

Re: How To: Passing curried functions around

2015-09-07 Thread Bahman Movaqar via Digitalmars-d-learn
On Monday, 7 September 2015 at 03:55:01 UTC, Meta wrote: The name validator_t is not idiomatic in D. Something like ValidatorFun should be preferred. Same for intReader_t; ReadIntFun is probably preferred, or even IntReader (but that would imply that it's a struct/class in my mind). Noted.

What are (dis)advantages of using pure and immutable by default?

2015-09-07 Thread Bahman Movaqar via Digitalmars-d-learn
It seems to me a good practice to mark all functions that I write as `pure` and define all the variables as `immutable`, unless there is a reason not to. I can see some serious advantages of this, most notable of which is minimum side-effect and predictability of the code. However I suppose

Better unittest failure output

2015-09-07 Thread Bahman Movaqar via Digitalmars-d-learn
I am working on a simple project created with DUB[1]. When unit tests the output reads really cryptic to me; for example: $ dub test Generating test runner configuration '__test__library__' for 'library' (library). Target dunit 1.0.11 is up to date. Use --force to rebuild.

Re: Better unittest failure output

2015-09-07 Thread Bahman Movaqar via Digitalmars-d-learn
On Monday, 7 September 2015 at 12:16:14 UTC, anonymous wrote: On Monday 07 September 2015 14:12, Bahman Movaqar wrote: Thanks. This is indeed helpful. OT but where can I view the documentation for `unittest` and `assert`? unittest: http://dlang.org/unittest.html assert:

Re: What are (dis)advantages of using pure and immutable by default?

2015-09-07 Thread Bahman Movaqar via Digitalmars-d-learn
On Monday, 7 September 2015 at 11:49:32 UTC, anonymous wrote: void f(int a) {} void g(int* a) {} void main() { int xm; immutable int xi; f(xm); /* ok, obviously */ f(xi); /* ok */ int* ym = immutable int* yi = g(ym); /* ok, obviously */ g(yi); /* doesn't

Re: What are (dis)advantages of using pure and immutable by default?

2015-09-07 Thread Bahman Movaqar via Digitalmars-d-learn
On Monday, 7 September 2015 at 10:55:13 UTC, anonymous wrote: On Monday 07 September 2015 12:40, Bahman Movaqar wrote: I can see some serious advantages of this, most notable of which is minimum side-effect and predictability of the code. However I suppose it's going to impact the performance

Re: Better unittest failure output

2015-09-07 Thread Bahman Movaqar via Digitalmars-d-learn
On Monday, 7 September 2015 at 12:06:09 UTC, anonymous wrote: On Monday 07 September 2015 13:57, Bahman Movaqar wrote: $ dub test [...] core.exception.AssertError@source/e002.d(111): unittest failure [...] From that one line I left intact above, you should also be able to figure

Chaining struct method invocations

2015-09-07 Thread Bahman Movaqar via Digitalmars-d-learn
I need some help understand the behaviour of my code[1]. Specifically I have trouble with `add` method on line 79. My impression is that since it returns `this`, multiple invocations can be chained like `obj.add(X).add(Y).add(Z)`. However the test on line 92 fails and if I do a `writeln`,

Re: Chaining struct method invocations

2015-09-07 Thread Bahman Movaqar via Digitalmars-d-learn
On Monday, 7 September 2015 at 14:54:04 UTC, Jacob Carlborg wrote: On 2015-09-07 16:44, Bahman Movaqar wrote: Does this mean that in the following piece of code, what is passed to `add` is actually a copy of `rec1`? auto rec1 = SalesRecord("p10", 1.0, 10); coll.add(rec1); Yes.

Re: Chaining struct method invocations

2015-09-07 Thread Bahman Movaqar via Digitalmars-d-learn
On Monday, 7 September 2015 at 14:26:57 UTC, mzf wrote: On Monday, 7 September 2015 at 14:12:25 UTC, Bahman Movaqar wrote: struct is a value type,you can convert to ref type by "ref": struct Test { int a; Test add1() { a++; return

Re: Chaining struct method invocations

2015-09-07 Thread Bahman Movaqar via Digitalmars-d-learn
On Monday, 7 September 2015 at 14:28:06 UTC, Namespace wrote: On Monday, 7 September 2015 at 14:12:25 UTC, Bahman Movaqar wrote: Structs are value types and therefore you return only a copy currently. Does this mean that in the following piece of code, what is passed to `add` is actually a

How To: Passing curried functions around

2015-09-06 Thread Bahman Movaqar via Digitalmars-d-learn
I'm just learning D, so please bear with me if I'm asking something naive. Consider the following code skeleton: // in part A of the application... // - alias bool function(int n) validator_t; bool isEven(int n) {

Re: How To: Passing curried functions around

2015-09-06 Thread Bahman Movaqar via Digitalmars-d-learn
On Sunday, 6 September 2015 at 19:22:41 UTC, welkam wrote: I dont know much about functional programming, but what stops you defining int readInt(string prompt, validator_t validator) { ... } as a free standing function and just call it from both parts of your code? What is the benefit of