You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a way to insert an item in a sorted array:
import std.stdio: writeln;
import std.range: assumeSorted;
import std.array: insertInPlace;
void main() {
int[] arr = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90];
int x = 35;
arr.insertInPlace(arr.assumeSorted.lowerBound(x).length, x);
arr.writeln;
}
Haskell has the insert/insertBy functions (for lists):
http://zvon.org/other/haskell/Outputlist/insertBy_f.htmlhttp://zvon.org/other/haskell/Outputlist/insert_f.html
It's an operation not too much rare, and implementing it correctly like I have shown is not immediate, there is a little risk of introducing a bug. So is it worth adding such small function std.array.insertOrdered() to std.array?
The text was updated successfully, but these errors were encountered:
bearophile_hugs reported this on 2013-04-05T16:19:24Z
Transfered from https://issues.dlang.org/show_bug.cgi?id=9887
Description
This is a way to insert an item in a sorted array: import std.stdio: writeln; import std.range: assumeSorted; import std.array: insertInPlace; void main() { int[] arr = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]; int x = 35; arr.insertInPlace(arr.assumeSorted.lowerBound(x).length, x); arr.writeln; } Haskell has the insert/insertBy functions (for lists): http://zvon.org/other/haskell/Outputlist/insertBy_f.html http://zvon.org/other/haskell/Outputlist/insert_f.html It's an operation not too much rare, and implementing it correctly like I have shown is not immediate, there is a little risk of introducing a bug. So is it worth adding such small function std.array.insertOrdered() to std.array?The text was updated successfully, but these errors were encountered: