Re: Ranges containers : is it possible to get a SortedRange from a RedBlackTree ?

2014-07-07 Thread Meta via Digitalmars-d-learn
On Monday, 7 July 2014 at 12:06:21 UTC, Frédérik wrote: Hi all, I'm discovering the D language that seems very nice in many aspects, but I'm quite confused by the container and range APIs while trying to design a very simple interface-oriented API. Especially I can't figure out how

Re: Ranges containers : is it possible to get a SortedRange from a RedBlackTree ?

2014-07-07 Thread bearophile via Digitalmars-d-learn
Frédérik: Especially I can't figure out how std.containers can connect properly with std.ranges : std.containers is still primitive, it needs to be improved and fleshed out, after Andrei's memory allocators. I would need to get the range that comes from the red black tree (as returned by

Re: Ranges containers : is it possible to get a SortedRange from a RedBlackTree ?

2014-07-07 Thread anonymous via Digitalmars-d-learn
On Monday, 7 July 2014 at 12:06:21 UTC, Frédérik wrote: I'm trying to achieve something like that (approximate D code): interface MyObjectSet { void add(MyObject o); void SortedRange!MyObject opSlice(); } class SomeRedBlackBasedImplementation { private RedBlackTree!MyObject

Re: Ranges containers : is it possible to get a SortedRange from a RedBlackTree ?

2014-07-07 Thread bearophile via Digitalmars-d-learn
Meta: (a SortedRange needs a range with random access, I think SortedRange was recently improved, and now accepts more than just random access ranges. Bye, bearophile

Re: Ranges containers : is it possible to get a SortedRange from a RedBlackTree ?

2014-07-07 Thread Frédérik
Thank you very much to all of you for these very quick and precise answers !! As well as for tour recommendations about D's idiomatisms, it seems very important to me since there are so many available paradigms in D. I'll try these solutions and I'll be able to go ahead with my first D

Re: Ranges containers : is it possible to get a SortedRange from a RedBlackTree ?

2014-07-07 Thread Fr via Digitalmars-d-learn
Hi again, The solution of making an array from the range works, but I'm concerned about the cost of instantiating a (potentially very large) array each time I need to walk across the set. Unless doing that is costless in D for any reason, it does not seem practical in my case. And when

Re: Ranges containers : is it possible to get a SortedRange from a RedBlackTree ?

2014-07-07 Thread anonymous via Digitalmars-d-learn
says that SortedRange could accept ranges weaker than random-access, but it is unable to provide interesting functionality for them. I don't know what to do about this. Looks like you can't get binary search through SortedRange over a Range of a RedBlackTree. If you don't need that, you can just

Re: Ranges containers : is it possible to get a SortedRange from a RedBlackTree ?

2014-07-07 Thread Fr via Digitalmars-d-learn
of a RedBlackTree. If you don't need that, you can just drop SortedRange (and assumeSorted) and use InputRange directly. Yes it seems this is the way to go in my case. As far as I know, true (efficient) random access on a RB tree is not a natural thing and I would nto have expected that ranges coming from

Re: Ranges containers : is it possible to get a SortedRange from a RedBlackTree ?

2014-07-07 Thread Meta via Digitalmars-d-learn
On Monday, 7 July 2014 at 19:20:24 UTC, Fr wrote: It's in the example above : SortedRange!(MyObject[]) opSlice() { sequence[].array.assumeSorted; } I thought that that using .array would lead to instantiating something. Yes, this *will* instantiate an array and copy all of the items from

Re: Ranges containers : is it possible to get a SortedRange from a RedBlackTree ?

2014-07-07 Thread anonymous via Digitalmars-d-learn
On Monday, 7 July 2014 at 19:20:24 UTC, Fr wrote: On Monday, 7 July 2014 at 16:58:51 UTC, anonymous wrote: No array is created in the example. Where do you think an array is created? It's in the example above : SortedRange!(MyObject[]) opSlice() { sequence[].array.assumeSorted; } I

Re: Splitting Ranges using Lambda Predicates

2014-06-11 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 10 June 2014 at 22:31:37 UTC, Nordlöw wrote: Either way, it shouldn't be too hard to implement. Base it off splitter!pred, which is actually quite trivial. AFAIK, your What do you mean by basing it off splitter!pred - should I start with some existing splitter algorithm in Phobos

Re: Splitting Ranges using Lambda Predicates

2014-06-11 Thread Artur Skawina via Digitalmars-d-learn
On 06/11/14 00:31, Nordlöw via Digitalmars-d-learn wrote: Either way, it shouldn't be too hard to implement. Base it off splitter!pred, which is actually quite trivial. AFAIK, your What do you mean by basing it off splitter!pred - should I start with some existing splitter algorithm in

Re: Splitting Ranges using Lambda Predicates

2014-06-11 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 11 June 2014 at 11:42:42 UTC, Artur Skawina via Digitalmars-d-learn wrote: On 06/11/14 00:31, Nordlöw via Digitalmars-d-learn wrote: Either way, it shouldn't be too hard to implement. Base it off splitter!pred, which is actually quite trivial. AFAIK, your What do you mean by

Re: Splitting Ranges using Lambda Predicates

2014-06-11 Thread Artur Skawina via Digitalmars-d-learn
On 06/11/14 14:40, monarch_dodra via Digitalmars-d-learn wrote: For example, you should avoid countUntil and takeExactly when dealing with strings, since these are not O(1) operations, and don't actually return string slices. EG: string s = someGGGreatVariableName.slicer().front; Error:

Re: Splitting Ranges using Lambda Predicates

2014-06-11 Thread Artur Skawina via Digitalmars-d-learn
On 06/11/14 15:44, Artur Skawina wrote: If, instead, you create a string-specific 'countUntil' that returns a type that holds both the byte and code-point counts and implicitly converts to the latter, then you can have a 'takeExactly' overload that uses the extra info to avoid the unnecessary

Re: Splitting Ranges using Lambda Predicates

2014-06-11 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 11 June 2014 at 13:44:25 UTC, Artur Skawina via Digitalmars-d-learn wrote: There is a reason why I never use D's std lib. artur Well, (IMO) it's a problem with no real solution. But for what it's worth, most (if not all) of the algorithms in the standard lib know how to handle

Re: Splitting Ranges using Lambda Predicates

2014-06-11 Thread Artur Skawina via Digitalmars-d-learn
easier to write it that way... That is why, after seeing your solution, I wrote the most compact lazy version I could think of - let's not scare people away from using ranges. AFAIUI the OP basically wanted a version of splitter that does not eat the separators and a predicate that keeps around

Splitting Ranges using Lambda Predicates

2014-06-10 Thread Nordlöw
I'm missing a version of splitter that can be used to split ranges based on arbitrary predicates. I need to for conversion between different symbol casings, typically: 1. someName = SomeName In this case the lambda should take two arguments (a,b) where in 1. a should be lowercase and b

Re: Splitting Ranges using Lambda Predicates

2014-06-10 Thread Nordlöw
1. someName = SomeName My example is dumb and incorrect. I actually want this to do the following 2. _someGreatVariableName = Some Great Varible Name

Re: Splitting Ranges using Lambda Predicates

2014-06-10 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 10 June 2014 at 21:11:17 UTC, Nordlöw wrote: 1. someName = SomeName My example is dumb and incorrect. I actually want this to do the following 2. _someGreatVariableName = Some Great Varible Name The current splitter works on the notion of splitter tokens, eg, it splits when it

Re: Splitting Ranges using Lambda Predicates

2014-06-10 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 10 June 2014 at 21:26:50 UTC, monarch_dodr What exactly are you requesting though? - Split on the edge lowercase to uppercase? - Split on uppercase but keep the uppercase element? Thinking about this more: Do you *actually* have two different predicates, or are they mutually

Re: Splitting Ranges using Lambda Predicates

2014-06-10 Thread Nordlöw
Either way, it shouldn't be too hard to implement. Base it off splitter!pred, which is actually quite trivial. AFAIK, your What do you mean by basing it off splitter!pred - should I start with some existing splitter algorithm in Phobos or start from scratch? Thx.

Re: Creating ranges over mutable, const, or immutable data structures.

2014-06-03 Thread Ali Çehreli via Digitalmars-d-learn
On 05/24/2014 11:01 AM, Ali Çehreli wrote: On 05/24/2014 10:02 AM, w0rp wrote: I have been writing my own hashmap which can provide forward ranges usable in @safe pure nothrow functions, because it's going to be useful for creating graph data structures with the same. I came to writing my

Re: Creating ranges over mutable, const, or immutable data structures.

2014-05-25 Thread Ali Çehreli via Digitalmars-d-learn
On 05/24/2014 10:02 AM, w0rp wrote: Create a mutable KeyRange over a map which forwards on the right constness for the key type, so the following must be true. HashMap!(K, V).keys.front - K const(HashMap!(K, V)).keys.front - const(K) immutable(HashMap!(K, V)).keys.front - immutable(K) I

Creating ranges over mutable, const, or immutable data structures.

2014-05-24 Thread w0rp via Digitalmars-d-learn
I have been writing my own hashmap which can provide forward ranges usable in @safe pure nothrow functions, because it's going to be useful for creating graph data structures with the same. I came to writing my ranges and I figured out how to do everything right for just mutable hashmaps

Re: Creating ranges over mutable, const, or immutable data structures.

2014-05-24 Thread Ali Çehreli via Digitalmars-d-learn
On 05/24/2014 10:02 AM, w0rp wrote: I have been writing my own hashmap which can provide forward ranges usable in @safe pure nothrow functions, because it's going to be useful for creating graph data structures with the same. I came to writing my ranges and I figured out how to do everything

Re: Creating ranges over mutable, const, or immutable data structures.

2014-05-24 Thread w0rp via Digitalmars-d-learn
On Saturday, 24 May 2014 at 18:01:43 UTC, Ali Çehreli wrote: On 05/24/2014 10:02 AM, w0rp wrote: I have been writing my own hashmap which can provide forward ranges usable in @safe pure nothrow functions, because it's going to be useful for creating graph data structures with the same. I

Regarding foreach loop index ranges

2014-04-21 Thread bearophile via Digitalmars-d-learn
In this case I am not sure about bug reports, so I ask here. In this program the first loop doesn't compile, giving a nice error: test.d(3,5): Error: index type 'ubyte' cannot cover index range 0..300 If you comment out the first loop, the second compiles and runs, but it seems to go in

Re: Regarding foreach loop index ranges

2014-04-21 Thread bearophile via Digitalmars-d-learn
For the second loop one possible alternative behavour is to refuse a ubyte index and accept only a size_t index if it loops on a dynamic array. Another alternative is: the i variable can go from 0 to 255, then go up to the modulus of the remaining indexes, and then stop. In this program the

Re: Regarding foreach loop index ranges

2014-04-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On Mon, 21 Apr 2014 07:49:03 -0400, bearophile bearophileh...@lycos.com wrote: In this case I am not sure about bug reports, so I ask here. In this program the first loop doesn't compile, giving a nice error: test.d(3,5): Error: index type 'ubyte' cannot cover index range 0..300 If you

Re: Regarding foreach loop index ranges

2014-04-21 Thread bearophile via Digitalmars-d-learn
https://issues.dlang.org/show_bug.cgi?id=12611 Bye, bearophile

Re: Choice ranges?

2014-03-29 Thread Timon Gehr
On 03/28/2014 08:00 PM, H. S. Teoh wrote: Today I ran into an interesting situation where I have a function f that needs to return ranges of different types (but identical element types): auto f(A...)(A args) { ... if (someCondition

Re: Choice ranges?

2014-03-29 Thread Timon Gehr
On 03/29/2014 01:37 AM, Artur Skawina wrote: You can wrap the range in a common one. When the above approach isn't enough, there's always the next level: (Note that your code does not actually compile.)

Re: Choice ranges?

2014-03-29 Thread Artur Skawina
On 03/29/14 16:31, Timon Gehr wrote: On 03/29/2014 01:37 AM, Artur Skawina wrote: You can wrap the range in a common one. When the above approach isn't enough, there's always the next level: (Note that your code does not actually compile.) Of course it compiles -- with the compiler version

Re: Choice ranges?

2014-03-29 Thread Timon Gehr
On 03/29/2014 06:19 PM, Artur Skawina wrote: Of course it compiles -- with the compiler version I happen to use... The compiler situation is one of the main reasons I gave up on D about a year ago. Every release introduces a new dialect. This may not be such a big problem for trivial toy

Re: Choice ranges?

2014-03-29 Thread Artur Skawina
On 03/29/14 18:30, Timon Gehr wrote: I'm stuck with 2.060 myself, but there's always http://dpaste.dzfl.pl/. For the examples, I used a gdc that claims __VERSION__==2062L. (for /real/ code I'm still using a 2.057 based one...) artur

Re: Choice ranges?

2014-03-29 Thread H. S. Teoh
On 03/28/2014 08:00 PM, H. S. Teoh wrote: Today I ran into an interesting situation where I have a function f that needs to return ranges of different types (but identical element types): auto f(A...)(A args) { ... if (someCondition

Choice ranges?

2014-03-28 Thread H. S. Teoh
Today I ran into an interesting situation where I have a function f that needs to return ranges of different types (but identical element types): auto f(A...)(A args) { ... if (someCondition) return cartesianProduct(x, y

Re: Choice ranges?

2014-03-28 Thread Meta
On Friday, 28 March 2014 at 19:02:48 UTC, H. S. Teoh wrote: Today I ran into an interesting situation where I have a function f that needs to return ranges of different types (but identical element types): auto f(A...)(A args) { ... if (someCondition

Re: Choice ranges?

2014-03-28 Thread bearophile
H. S. Teoh: So how would I implement something like this? One option is to wrap those ranges in classes. See std.range for the adapters. (I have not used them yet). Bye, bearophile

Re: Choice ranges?

2014-03-28 Thread Ali Çehreli
On 03/28/2014 01:46 PM, bearophile wrote: H. S. Teoh: So how would I implement something like this? One option is to wrap those ranges in classes. See std.range for the adapters. (I have not used them yet). Link: http://dlang.org/phobos/std_range.html#.inputRangeObject Short examples

Re: Choice ranges?

2014-03-28 Thread Artur Skawina
On 03/28/14 20:00, H. S. Teoh wrote: Today I ran into an interesting situation where I have a function f that needs to return ranges of different types (but identical element types): auto f(A...)(A args) { ... if (someCondition

Re: Choice ranges?

2014-03-28 Thread H. S. Teoh
On Fri, Mar 28, 2014 at 10:44:11PM +0100, Artur Skawina wrote: On 03/28/14 20:00, H. S. Teoh wrote: Today I ran into an interesting situation where I have a function f that needs to return ranges of different types (but identical element types): auto f(A...)(A args

Re: Choice ranges?

2014-03-28 Thread monarch_dodra
, vs. an alternative algorithm filtered by some other filters. T That would have been my solution too (though maybe using a delegate, to not re-evaluate someconditon per-element). If both ranges are completely incompatible, then I'd either: - array them into submission until they reach

Re: Choice ranges?

2014-03-28 Thread Artur Skawina
On 03/28/14 23:10, H. S. Teoh wrote: On Fri, Mar 28, 2014 at 10:44:11PM +0100, Artur Skawina wrote: On 03/28/14 20:00, H. S. Teoh wrote: Today I ran into an interesting situation where I have a function f that needs to return ranges of different types (but identical element types): auto

Re: Choice ranges?

2014-03-28 Thread H. S. Teoh
in the caller: auto polymorphicRange(size_t i, R...)(R ranges) { static if (i==0) return ranges[0]; else static if (i==1) return ranges[1]; else ... // etc } void processRange(R)(R

Need help how to get started with D ranges

2014-03-24 Thread Uranuz
I see that ranges is primitive to organzie universal approach to write some algorithms. But I'm using algorithms from the library but I still can't start with writing my own algorithms based on ranges. For example I have the following function written without ranges. I want to improve

Re: Need help how to get started with D ranges

2014-03-24 Thread John Colvin
On Monday, 24 March 2014 at 12:13:43 UTC, Uranuz wrote: I see that ranges is primitive to organzie universal approach to write some algorithms. But I'm using algorithms from the library but I still can't start with writing my own algorithms based on ranges. For example I have the following

Re: Need help how to get started with D ranges

2014-03-24 Thread Uranuz
Have you read this: http://ddili.org/ders/d.en/ranges.html ? Yes I have read it. It's difficult to formulate the question in English bu I'l try. In this example I searching for special symbols '' and '='. So when symbol found I use *save* method of range to remember start of *name* or

Re: Need help how to get started with D ranges

2014-03-24 Thread Uranuz
don't know all of it's possibilities/ advantages. It's why I asking. In my algorithms for parsing some strings I often save positions of beginings of some tokens in order to take a slice and put it into some buffer. But for opearting with them in terms of ranges I need to have RandomAccessRange

Re: Need help how to get started with D ranges

2014-03-24 Thread monarch_dodra
On Monday, 24 March 2014 at 14:12:58 UTC, Uranuz wrote: Have you read this: http://ddili.org/ders/d.en/ranges.html ? Yes I have read it. It's difficult to formulate the question in English bu I'l try. In this example I searching for special symbols '' and '='. So when symbol found I use

Re: Ranges/algorithms for aggregation

2014-03-22 Thread bearophile
Luís Marques: It would swap elements, like sort, so it doesn't need to put them anywhere, just permute them. The advantage is this: Input: [7, 3, 7, 1, 1, 1, 1] Output sort: [1, 1, 1, 1, 3, 7, 7] Output groupSort: [3, 7, 7, 1, 1, 1, 1] I think to swap items it must know where to swap them

Ranges/algorithms for aggregation

2014-03-21 Thread Luís.Marques
Is there a neat way to do this transformation with ranges and std.algorithms? Input: --- B foo B bar C ble B big A begga Output: (aggregated and sorted on length) --- B - [foo, bar, big] C - [ble] A - [begga] The most obvious way (to me

Re: Ranges/algorithms for aggregation

2014-03-21 Thread bearophile
Luís Marques: Is there a neat way to do this transformation with ranges and std.algorithms? Input: --- B foo B bar C ble B big A begga Output: (aggregated and sorted on length) --- B - [foo, bar, big] C - [ble] A - [begga] What

Re: Ranges/algorithms for aggregation

2014-03-21 Thread Luís.Marques
On Friday, 21 March 2014 at 15:38:23 UTC, bearophile wrote: Output: (aggregated and sorted on length) --- B - [foo, bar, big] C - [ble] A - [begga] What is the desired output data structure? An associative array of dynamic arrays? Or is a dynamic arrays of dynamic arrays of

Re: Ranges/algorithms for aggregation

2014-03-21 Thread bearophile
Luís Marques: So let's just say that the client is unclear on his requirements, which does happen in the real world anyway :-). Yes, it happens, But it's a problem, because often if you know what you need you can produce the results more efficiently :-) (did anyone actually implement

Re: Ranges/algorithms for aggregation

2014-03-21 Thread Luís.Marques
On Friday, 21 March 2014 at 16:04:45 UTC, bearophile wrote: I think this problem needs a sorting or a hash. One possible solution, if you don't need an associative array as output, is to use a multiSort followed by a building of groups using slicing. It could be efficient enough. Later you

Re: Ranges/algorithms for aggregation

2014-03-21 Thread Justin Whear
On Fri, 21 Mar 2014 15:22:13 +, Luís Marques wrote: Is there a neat way to do this transformation with ranges and std.algorithms? Input: --- B foo B bar C ble B big A begga Output: (aggregated and sorted on length) --- B - [foo, bar, big

Re: Ranges/algorithms for aggregation

2014-03-21 Thread H. S. Teoh
range of lazy ranges. 1 https://github.com/D-Programming-Language/phobos/pull/1186 Be aware, though, that groupBy only compares *adjacent* elements for equivalence; it does not sort the input. So if your input has equivalent elements interspersed with non-equivalent elements, you will have

Re: Ranges/algorithms for aggregation

2014-03-21 Thread Luís.Marques
On Friday, 21 March 2014 at 16:53:46 UTC, H. S. Teoh wrote: Be aware, though, that groupBy only compares *adjacent* elements for equivalence; it does not sort the input. So if your input has equivalent elements interspersed with non-equivalent elements, you will have the equivalent elements

Re: Ranges/algorithms for aggregation

2014-03-21 Thread Luís.Marques
On Friday, 21 March 2014 at 17:20:38 UTC, Luís Marques wrote: I think that's why Justin used sort. The hashGroupBy proposed by bearophile would avoid the sort and the additional memory usage though, so that would be even better. I was thinking, we don't even need the full power of sort. Is

Re: Ranges/algorithms for aggregation

2014-03-21 Thread bearophile
Luís Marques: I was thinking, we don't even need the full power of sort. Is there a standard algorithm that makes elements with equal keys be in sequence, but that otherwise is less expensive than sort? I don't know any, how is it supposed to know where to group items? Usually you build an

Re: Ranges/algorithms for aggregation

2014-03-21 Thread Luís.Marques
On Saturday, 22 March 2014 at 01:08:11 UTC, bearophile wrote: how is it supposed to know where to group items? Usually you build an associative array for that. It would swap elements, like sort, so it doesn't need to put them anywhere, just permute them. The advantage is this: Input: [7, 3,

Explicit Declaration of Pureness and Throwness of Higher Order Ranges

2014-03-10 Thread Nordlöw
Is it, in D today, possible to explicitly tag higher order ranges such as for example `map` with their pureness, safeness and throwness based on the corresponding properties on their function arguments such as `fun...`? And is this motivated? now that the D compiler deduce these properties

Re: Explicit Declaration of Pureness and Throwness of Higher Order Ranges

2014-03-10 Thread Jonathan M Davis
On Monday, March 10, 2014 22:02:39 Nordlöw wrote: Is it, in D today, possible to explicitly tag higher order ranges such as for example `map` with their pureness, safeness and throwness based on the corresponding properties on their function arguments such as `fun...`? And is this motivated

Re: Explicit Declaration of Pureness and Throwness of Higher Order Ranges

2014-03-10 Thread Nordlöw
I'm not quite sure what you're asking. You either mark a function as @safe, pure, and/or nothrow - or you don't, in which case, if it's a templated function, the attributes are inferred to the best of the compiler's capabilities, and if it's not, then the function doesn't have those

Re: Ranges, constantly frustrating

2014-02-17 Thread Regan Heath
it is a count or index into the result set. Lets call this change scheme #0. It solves my issue, and interestingly also would have meant we didn't need to add byKey or byValue to AA's, instead we could have simply made keys/values indexable ranges and not broken any existing code. Further details

Re: Ranges, constantly frustrating

2014-02-14 Thread Regan Heath
On Fri, 14 Feb 2014 02:48:51 -, Jesse Phillips jesse.k.phillip...@gmail.com wrote: On Thursday, 13 February 2014 at 14:30:41 UTC, Regan Heath wrote: Don't get me wrong, counting the elements as you iterate over them is useful, but it isn't the index into the range you're likely after.

Re: Ranges, constantly frustrating

2014-02-14 Thread Jakob Ovrum
a function on a range and foreaching on the result, you would intuitively and immediately expect 'i' to relate to the result, not the input. R How should it behave on ranges without length, such as infinite ranges? Also, `enumerate` has the advantage of the `start` parameter, which usefulness

Re: Ranges, constantly frustrating

2014-02-14 Thread bearophile
Regan Heath: FWIW I disagree. I think it's immediately and intuitively obvious what 'i' should be when you're foreaching over X items taken from another range, even if you do not know take returns another range. Compare it to calling a function on a range and foreaching on the result, you

Re: Ranges, constantly frustrating

2014-02-14 Thread Regan Heath
On Fri, 14 Feb 2014 13:14:51 -, bearophile bearophileh...@lycos.com wrote: Regan Heath: FWIW I disagree. I think it's immediately and intuitively obvious what 'i' should be when you're foreaching over X items taken from another range, even if you do not know take returns another

Re: Ranges, constantly frustrating

2014-02-14 Thread Regan Heath
, even if you do not know take returns another range. Compare it to calling a function on a range and foreaching on the result, you would intuitively and immediately expect 'i' to relate to the result, not the input. R How should it behave on ranges without length, such as infinite ranges

Re: Ranges, constantly frustrating

2014-02-14 Thread bearophile
Regan Heath: In my case I didn't need any of these. I don't understand. Bye, bearophile

Re: Ranges, constantly frustrating

2014-02-14 Thread bearophile
Isn't this discussion about adding an index to a range? If it is, then I have shown why adding it in the language is a bad idea. Bye, bearophile

Re: Ranges, constantly frustrating

2014-02-14 Thread Marc Schütz
for arrays: foreach(v; [1,2,3,4]) writeln(v); foreach(i, v; [1,2,3,4]) writeln(i, = , v); But for ranges, the second form is not possible: foreach(v; iota(4)) // ok writeln(v); foreach(i, v; iota(4))// Error: cannot infer argument types writeln(i, = , v);

Re: Ranges, constantly frustrating

2014-02-14 Thread bearophile
Marc Schütz: As far as I understand it, it's about adding an index to _foreach_, as is already supported for arrays: foreach(v; [1,2,3,4]) writeln(v); foreach(i, v; [1,2,3,4]) writeln(i, = , v); But for ranges, the second form is not possible: foreach(v; iota(4)) // ok

Re: Ranges, constantly frustrating

2014-02-13 Thread Regan Heath
On Wed, 12 Feb 2014 11:08:57 -, Jakob Ovrum jakobov...@gmail.com wrote: On Wednesday, 12 February 2014 at 10:44:57 UTC, Regan Heath wrote: Ahh.. so this is a limitation of the range interface. Any plans to fix this? R Did my original reply not arrive? It is the first reply in the

Re: Ranges, constantly frustrating

2014-02-13 Thread Regan Heath
order (personally haven't seen such a range). But more importantly you're not dealing with random access ranges. The index you're receiving from take(5) can't be used on the range. A forward range can do what I am describing above, it's trivial. Don't get me wrong, counting the elements as you

Re: Ranges, constantly frustrating

2014-02-13 Thread Jesse Phillips
On Thursday, 13 February 2014 at 14:30:41 UTC, Regan Heath wrote: Don't get me wrong, counting the elements as you iterate over them is useful, but it isn't the index into the range you're likely after. Nope, not what I am after. If I was, I'd iterate over the original range instead or keep

Re: Ranges, constantly frustrating

2014-02-12 Thread Regan Heath
On Tue, 11 Feb 2014 17:11:46 -, Ali Çehreli acehr...@yahoo.com wrote: On 02/11/2014 06:25 AM, Rene Zwanenburg wrote: On Tuesday, 11 February 2014 at 10:10:27 UTC, Regan Heath wrote: foreach (i, line; range.take(4)) //Error: cannot infer argument types { ..etc.. } foreach

Re: Ranges, constantly frustrating

2014-02-12 Thread Regan Heath
parameters. byLine has its own issues with reuse of the buffer, it isn't inherent to ranges. I haven't really used it (needed it from std.process), when I wanted to read a large file I went with wrapping std.mmap: https://github.com/JesseKPhillips/libosm/blob/master/source/util/filerange.d Cool

Re: Ranges, constantly frustrating

2014-02-12 Thread Jakob Ovrum
On Wednesday, 12 February 2014 at 10:44:57 UTC, Regan Heath wrote: Ahh.. so this is a limitation of the range interface. Any plans to fix this? R Did my original reply not arrive? It is the first reply in the thread... Reproduced: See this pull request[1] and the linked enhancement

Ranges, constantly frustrating

2014-02-11 Thread Regan Heath
Things like this should just work.. File input ... auto range = input.byLine(); while(!range.empty) { range.popFront(); foreach (i, line; range.take(4)) //Error: cannot infer argument types { ..etc.. } range.popFront(); } Tried adding 'int' and 'char[]' or 'auto' .. no dice.

Re: Ranges, constantly frustrating

2014-02-11 Thread Tobias Pankrath
On Tuesday, 11 February 2014 at 10:10:27 UTC, Regan Heath wrote: Things like this should just work.. File input ... auto range = input.byLine(); while(!range.empty) { range.popFront(); foreach (i, line; range.take(4)) //Error: cannot infer argument types { ..etc.. }

Re: Ranges, constantly frustrating

2014-02-11 Thread Jakob Ovrum
On Tuesday, 11 February 2014 at 10:10:27 UTC, Regan Heath wrote: Things like this should just work.. File input ... auto range = input.byLine(); while(!range.empty) { range.popFront(); foreach (i, line; range.take(4)) //Error: cannot infer argument types { ..etc.. }

Re: Ranges, constantly frustrating

2014-02-11 Thread Regan Heath
On Tue, 11 Feb 2014 10:52:39 -, Tobias Pankrath tob...@pankrath.net wrote: Further, the naive solution of adding .array gets you in all sorts of trouble :p (The whole byLine buffer re-use issue). This should be simple and easy, dare I say it trivial.. or am I just being dense here.

Re: Ranges, constantly frustrating

2014-02-11 Thread Regan Heath
On Tue, 11 Feb 2014 10:58:17 -, Tobias Pankrath tob...@pankrath.net wrote: On Tuesday, 11 February 2014 at 10:10:27 UTC, Regan Heath wrote: Things like this should just work.. File input ... auto range = input.byLine(); while(!range.empty) { range.popFront(); foreach (i, line;

Re: Ranges, constantly frustrating

2014-02-11 Thread Tobias Pankrath
On Tuesday, 11 February 2014 at 13:00:19 UTC, Regan Heath wrote: import std.stdio; struct S1 { private int[] elements = [9,8,7]; int opApply (int delegate (ref uint, ref int) block) { foreach (uint i, int n ; this.elements) block(i, n); return 0; } } void

Re: Ranges, constantly frustrating

2014-02-11 Thread Rene Zwanenburg
On Tuesday, 11 February 2014 at 10:10:27 UTC, Regan Heath wrote: Things like this should just work.. File input ... auto range = input.byLine(); while(!range.empty) { range.popFront(); foreach (i, line; range.take(4)) //Error: cannot infer argument types { ..etc.. }

Re: Ranges, constantly frustrating

2014-02-11 Thread Ali Çehreli
On 02/11/2014 06:25 AM, Rene Zwanenburg wrote: On Tuesday, 11 February 2014 at 10:10:27 UTC, Regan Heath wrote: foreach (i, line; range.take(4)) //Error: cannot infer argument types { ..etc.. } foreach (i, line; iota(size_t.max).zip(range.take(4))) { } There is also the

Re: Ranges, constantly frustrating

2014-02-11 Thread Steve Teale
On Tuesday, 11 February 2014 at 10:52:40 UTC, Tobias Pankrath wrote: The second naive solution would be to use readText and splitLines. That's the sort of thing I always do because then I understand what's going on, and when there's a bug I can find it easily! But then I'm not writing

Re: Ranges, constantly frustrating

2014-02-11 Thread thedeemon
On Tuesday, 11 February 2014 at 19:48:41 UTC, Jesse Phillips wrote: In case the other replies weren't clear enough. A range does not have an index. What do you expect 'i' to be? In case of foreach(i, x; range) I would expect it to be iteration number of this particular foreach. I miss it

std.copy (to multiple output ranges),

2014-01-27 Thread Robert Schadek
I'm searching the docs for something similar to: copy(someInputRange, firstOutputRange, secondOutputRange, ); I know how to write it by hand, but I'm suspecting that something like this is already in phobos. And secondly, is there some function that gives me a forward range to some input

Re: std.copy (to multiple output ranges),

2014-01-27 Thread Ilya Yaroshenko
On Monday, 27 January 2014 at 17:26:35 UTC, Robert Schadek wrote: I'm searching the docs for something similar to: copy(someInputRange, firstOutputRange, secondOutputRange, ); I know how to write it by hand, but I'm suspecting that something like this is already in phobos. Hi, I think

Re: std.copy (to multiple output ranges),

2014-01-27 Thread Justin Whear
, is there some function that gives me a forward range to some input range? Curiously, copy doesn't implement multiple output ranges. I don't think there's any reason it couldn't. I think an enhancement request is in order. Turning an InputRange into a ForwardRange implies buffering, std.array.array

Cartesian product of immutable ranges

2014-01-26 Thread matovitch
I posted a question on stackoverflow about the cartesian product of immutable ranges : http://stackoverflow.com/questions/21368501/cartesian-product-of-immutable-ranges There are a lot of various thing I don't understand. This code compiles and run : import std.stdio; import std.range

Re: Cartesian product of immutable ranges

2014-01-26 Thread Stanislav Blinov
of two immutable ranges ? Short answer: with std.algorithm - you can't. Because internally it (Zip, actually) performs assignments, and you can't assign to immutable(T). Why?

Re: Cartesian product of immutable ranges

2014-01-26 Thread matovitch
); // will print immutable(int)[] You mean that two input ranges are created from the immutable arrays when I call the function ? Zip doesn't compiles while zip compile. :/ Here is the implementation of zip : auto zip(Ranges...)(Ranges ranges) if (Ranges.length allSatisfy!(isInputRange, Ranges

Re: Cartesian product of immutable ranges

2014-01-26 Thread Jakob Ovrum
On Sunday, 26 January 2014 at 22:32:32 UTC, matovitch wrote: You mean that two input ranges are created from the immutable arrays when I call the function ? Zip doesn't compiles while zip compile. :/ `Zip` must be explicitly instantiated, which allows you to attempt to instantiate

Re: Cartesian product of immutable ranges

2014-01-26 Thread matovitch
On Sunday, 26 January 2014 at 22:52:56 UTC, Jakob Ovrum wrote: On Sunday, 26 January 2014 at 22:32:32 UTC, matovitch wrote: You mean that two input ranges are created from the immutable arrays when I call the function ? Zip doesn't compiles while zip compile. :/ `Zip` must be explicitly

<    1   2   3   4   5   6   7   8   9   10   >