Re: Is there a nicer way to get the first element or typeof(element).init from a range?

2021-05-31 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 30 May 2021 at 12:16:19 UTC, realhet wrote: Is there a prettier way to do this? Thanks in advance. ```d import std.range; auto getFirst(R)(R range) if (isInputRange!R) { if (range.empty) return ElementType!Range.init; else return range.front; } ```

Re: Is there a nicer way to get the first element or typeof(element).init from a range?

2021-05-30 Thread realhet via Digitalmars-d-learn
On Sunday, 30 May 2021 at 12:16:19 UTC, realhet wrote: presets.keys.sort.take(1).get(0); <- Oups: after fixing an error and making it compile the solution is even uglier: presets.keys.sort.take(1).array.get(0);

Is there a nicer way to get the first element or typeof(element).init from a range?

2021-05-30 Thread realhet via Digitalmars-d-learn
Hello, This is my current solution but there must be a better way to do it in D T get(T)(T[] arr, size_t idx, T def = T.init){ return idx