www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Range format specifiers in other languages?

reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Oct 11 2020
next sibling parent reply Max Haughton <maxhaton gmail.com> writes:
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)
Oct 11 2020
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Oct 11 2020
prev sibling parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
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?
Oct 11 2020
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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.
Oct 11 2020
parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
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
Oct 11 2020
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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 -- Век живи - век учись. А дураком помрёшь.
Oct 12 2020