Thread overview
Range format specifiers in other languages?
Oct 11, 2020
Ali Çehreli
Oct 12, 2020
Max Haughton
Oct 12, 2020
Ali Çehreli
Oct 12, 2020
Imperatorn
Oct 12, 2020
Adam D. Ruppe
Oct 12, 2020
Imperatorn
Oct 12, 2020
H. S. Teoh
October 11, 2020
I find D's %( and %) range format specifiers very useful:

import std.stdio;
import std.range;

void main() {
  5.iota.writefln!"%(%s, %)";  // Prints 0, 1, 2, 3, 4
}

Are there similar features in other languages?

Thank you,
Ali
October 12, 2020
On Sunday, 11 October 2020 at 23:57:31 UTC, Ali Çehreli wrote:
> I find D's %( and %) range format specifiers very useful:
>
> import std.stdio;
> import std.range;
>
> void main() {
>   5.iota.writefln!"%(%s, %)";  // Prints 0, 1, 2, 3, 4
> }
>
> Are there similar features in other languages?
>
> Thank you,
> Ali

I think rust can do something similar with struct pretty printing. The syntax has curly braces in it but I can't recall it right now.

Possibly worth showing off (especially given that some people at first don't even know the templated format string exists)
October 12, 2020
On Sunday, 11 October 2020 at 23:57:31 UTC, Ali Çehreli wrote:
> I find D's %( and %) range format specifiers very useful:
>
> import std.stdio;
> import std.range;
>
> void main() {
>   5.iota.writefln!"%(%s, %)";  // Prints 0, 1, 2, 3, 4
> }
>
> Are there similar features in other languages?
>
> Thank you,
> Ali

To people trying to learn, why is that % before ( needed in the format string?
October 12, 2020
On Monday, 12 October 2020 at 00:46:37 UTC, Imperatorn wrote:
> To people trying to learn, why is that % before ( needed in the format string?

The %( ... %) stuff is expanded and repeated for each element inside the given array.
October 11, 2020
On 10/11/20 5:44 PM, Max Haughton wrote:

> Possibly worth showing off (especially given that some people at first
> don't even know the templated format string exists)

This feature is already among my slides for an upcoming conference. ;)

Ali

October 12, 2020
On Monday, 12 October 2020 at 00:59:33 UTC, Adam D. Ruppe wrote:
> On Monday, 12 October 2020 at 00:46:37 UTC, Imperatorn wrote:
>> To people trying to learn, why is that % before ( needed in the format string?
>
> The %( ... %) stuff is expanded and repeated for each element inside the given array.

Thanks, it seems there are some pretty powerful formatting options:

https://dlang.org/phobos/std_format.html
October 12, 2020
On Mon, Oct 12, 2020 at 05:51:21AM +0000, Imperatorn via Digitalmars-d-learn wrote:
> On Monday, 12 October 2020 at 00:59:33 UTC, Adam D. Ruppe wrote:
> > On Monday, 12 October 2020 at 00:46:37 UTC, Imperatorn wrote:
> > > To people trying to learn, why is that % before ( needed in the format string?
> > 
> > The %( ... %) stuff is expanded and repeated for each element inside
> > the given array.
> 
> Thanks, it seems there are some pretty powerful formatting options:
> 
> https://dlang.org/phobos/std_format.html

Indeed.

%(...%) is one of my favorite because it can be nested, so it's a very useful quick-n-dirty tool for debugging ranges. With .chunks and .map, you can format just about any range-based data in a one-liner for dumping debug info.

Another cool one is the `,` digit-grouper:

	import std;
	void main() {
		writefln("%,2d", 1234567890);
		writefln("%,3d", 1234567890);
		writefln("%,4d", 1234567890);
		writefln("%,3?d", '_', 1234567890);
		writefln("%,4?d", '\'', 1234567890);
		writefln("%,4?.2f", '\'', 1234567890.123);
	}

Output:
	12,34,56,78,90
	1,234,567,890
	12,3456,7890
	1_234_567_890
	12'3456'7890
	12'3456'7890.12


T

-- 
Век живи - век учись. А дураком помрёшь.