www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - chaining chain Result and underlying object of chain

reply Laeeth Isharc <spamnolaeeth nospamlaeeth.com> writes:
chain doesn't seem to compile if I try and chain a chain of two 
strings and another string.

what should I use instead?


Laeeth.
Sep 14 2015
next sibling parent cym13 <cpicard openmailbox.org> writes:
On Monday, 14 September 2015 at 14:17:51 UTC, Laeeth Isharc wrote:
 chain doesn't seem to compile if I try and chain a chain of two 
 strings and another string.

 what should I use instead?


 Laeeth.
std.algorithm.iteration.joiner?
Sep 14 2015
prev sibling next sibling parent John Colvin <john.loughran.colvin gmail.com> writes:
On Monday, 14 September 2015 at 14:17:51 UTC, Laeeth Isharc wrote:
 chain doesn't seem to compile if I try and chain a chain of two 
 strings and another string.

 what should I use instead?


 Laeeth.
Works for me: http://dpaste.dzfl.pl/a692281f7a80
Sep 14 2015
prev sibling parent reply anonymous <anonymous example.com> writes:
On Monday 14 September 2015 16:17, Laeeth Isharc wrote:

 chain doesn't seem to compile if I try and chain a chain of two 
 strings and another string.
 
 what should I use instead?
Please show code, always. A simple test works for me: ---- import std.algorithm: equal; import std.range: chain; void main() { auto chain1 = chain("foo", "bar"); auto chain2 = chain(chain1, "baz"); assert(equal(chain2, "foobarbaz")); } ----
Sep 14 2015
parent reply Laeeth Isharc <spamnolaeeth nospamlaeeth.com> writes:
On Monday, 14 September 2015 at 14:31:33 UTC, anonymous wrote:
 On Monday 14 September 2015 16:17, Laeeth Isharc wrote:

 chain doesn't seem to compile if I try and chain a chain of 
 two strings and another string.
 
 what should I use instead?
Please show code, always. A simple test works for me: ---- import std.algorithm: equal; import std.range: chain; void main() { auto chain1 = chain("foo", "bar"); auto chain2 = chain(chain1, "baz"); assert(equal(chain2, "foobarbaz")); } ----
Sorry - was exhausted yesterday when I had the code there, or would have posted. I was trying to use the same variable eg auto chain1 = chain("foo", "bar"); chain1 = chain(chain1, "baz"); Realized that in this case it was much simpler just to use the delegate version of toString and sink (which I had forgotten about). But I wondered what to do in other cases. It may be that the type of chain1 and chain2 don't mix.
Sep 14 2015
next sibling parent anonymous <anonymous example.com> writes:
On Monday 14 September 2015 17:01, Laeeth Isharc wrote:

        auto chain1 = chain("foo", "bar");
        chain1 = chain(chain1, "baz");
 
 Realized that in this case it was much simpler just to use the 
 delegate version of toString and sink (which I had forgotten 
 about).  But I wondered what to do in other cases.  It may be 
 that the type of chain1 and chain2 don't mix.
Yes, the types don't match. The result types of most range functions depend on the argument types. Let's say chain("foo", "bar") has the type ChainResult!(string, string). Then chain(chain("foo", "bar"), "baz") has the type ChainResult! (ChainResult!(string, string), string). Those are different and not compatible. You can get the same type by: a) being eager: ---- import std.array: array; auto chain1 = chain("foo", "bar").array; chain1 = chain(chain1, "baz").array; ---- (At that point you could of course just work with the strings directly, using ~ and ~=.) b) being classy: ---- import std.range.interfaces; InputRange!dchar chain1 = inputRangeObject(chain("foo", "bar")); chain1 = inputRangeObject(chain(chain1, "baz")); ---- Those have performance implications, of course. Being eager means allocating the whole thing, and possibly intermediate results. Being classy means allocating objects for the ranges (could possibly put them on the stack), and it means indirections.
Sep 14 2015
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 09/14/2015 08:01 AM, Laeeth Isharc wrote:
 I was trying to use the same variable eg

        auto chain1 = chain("foo", "bar");
        chain1 = chain(chain1, "baz");
[...]
 It may be that the type of chain1
 and chain2 don't mix.
Exactly. I was going to recommend using pragma(msg, typeof(chain1)) to see what they are but it looks like chain()'s return type is not templatized. (?) pragma(msg, typeof(chain1)); pragma(msg, typeof(chain2)); Prints Result Result instead of something like (hypothetical) ChainResult!(string, string) ChainResult!(ChainResult!(string, string), string) Ali
Sep 14 2015
next sibling parent John Colvin <john.loughran.colvin gmail.com> writes:
On Monday, 14 September 2015 at 15:30:14 UTC, Ali Çehreli wrote:
 On 09/14/2015 08:01 AM, Laeeth Isharc wrote:
 I was trying to use the same variable eg

        auto chain1 = chain("foo", "bar");
        chain1 = chain(chain1, "baz");
[...]
 It may be that the type of chain1
 and chain2 don't mix.
Exactly. I was going to recommend using pragma(msg, typeof(chain1)) to see what they are but it looks like chain()'s return type is not templatized. (?) pragma(msg, typeof(chain1)); pragma(msg, typeof(chain2)); Prints Result Result instead of something like (hypothetical) ChainResult!(string, string) ChainResult!(ChainResult!(string, string), string) Ali
It is templated, but by means of it's enclosing function being templated, which doesn't end up in the name.
Sep 14 2015
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 9/14/15 11:30 AM, Ali Çehreli wrote:
 On 09/14/2015 08:01 AM, Laeeth Isharc wrote:
  > I was trying to use the same variable eg
  >
  >        auto chain1 = chain("foo", "bar");
  >        chain1 = chain(chain1, "baz");
 [...]
  > It may be that the type of chain1
  > and chain2 don't mix.

 Exactly.

 I was going to recommend using pragma(msg, typeof(chain1)) to see what
 they are but it looks like chain()'s return type is not templatized. (?)

      pragma(msg, typeof(chain1));
      pragma(msg, typeof(chain2));

 Prints

 Result
 Result

 instead of something like (hypothetical)

 ChainResult!(string, string)
 ChainResult!(ChainResult!(string, string), string)

 Ali
typeid is a bit better: import std.range; void main() { import std.stdio; auto chain1 = chain("hi", "there"); auto chain2 = chain(chain1, "friend"); writeln(typeid(chain1)); writeln(typeid(chain2)); } output: std.range.chain!(string, string).chain.Result std.range.chain!(Result, string).chain.Result I still see that "Result" as a parameter for chain2. I think the compiler should be better at printing these types at compile time. -Steve
Sep 15 2015