| Thread overview | |||||
|---|---|---|---|---|---|
|
January 13, 2014 In-Place Ordering of Elements | ||||
|---|---|---|---|---|
| ||||
Does Phobos have some variadic algorithm to order l-value reference arguments in place? Something like
int a=3;
int b=2;
int c=1;
orderInPlace(a,b,c);
// a is now 1
// b is now 2
// c is now 3
Also a functional variant, say `order(a, b, c)`, that returns a tuple would also be nice.
See also https://stackoverflow.com/questions/21102646/in-place-ordering-of-elements.
| ||||
January 14, 2014 Re: In-Place Ordering of Elements | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Monday, 13 January 2014 at 22:28:23 UTC, Nordlöw wrote:
> Does Phobos have some variadic algorithm to order l-value reference arguments in place? Something like
>
> int a=3;
> int b=2;
> int c=1;
>
> orderInPlace(a,b,c);
>
> // a is now 1
> // b is now 2
> // c is now 3
import std.algorithm: map, sort;
import std.range: only;
int a=3;
int b=2;
int c=1;
static ref int deref(int* p) {return *p;}
sort(only(&a, &b, &c).map!deref);
assert(a == 1);
assert(b == 2);
assert(c == 3);
| |||
January 14, 2014 Re: In-Place Ordering of Elements | ||||
|---|---|---|---|---|
| ||||
Posted in reply to anonymous | anonymous:
> static ref int deref(int* p) {return *p;}
> sort(only(&a, &b, &c).map!deref);
Despite some holes, std.algorithm and std.range are quite powerful. Sometimes using them feels like playing a puzzle game :-)
Bye,
bearophile
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply