What is your favorite D feature?

qznc via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 22 01:52:27 PDT 2017


On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
> Hi,
>
> I am currently trying to modernize the D code example roulette 
> on the dlang.org front page [1]. Hence, I would love to hear 
> about your favorite feature(s) in D.
> Ideas:
> - favorite language construct
> - favorite code sample
> - "only possible in D"
>
> Before you ask, yes - I want to add a couple of cool examples 
> to dlang.org (and yep the roulette rotation is currently broken 
> [2]).
>
> [1] 
> https://github.com/dlang/dlang.org/pulls?q=is%3Apr+is%3Aopen+label%3A%22Frontpage+example%22
> [2] https://github.com/dlang/dlang.org/pull/1757

H. S. Teoh calendar: 
https://wiki.dlang.org/Component_programming_with_ranges

/**
  * Formats a year.
  * Parameters:
  *  year = Year to display calendar for.
  *  monthsPerRow = How many months to fit into a row in the 
output.
  * Returns: A range of strings representing the formatted year.
  */
auto formatYear(int year, int monthsPerRow)
{
     enum colSpacing = 1;
     return
         datesInYear(year) // Start by generating all dates for 
the given year
         .byMonth() // Group them by month
         .chunks(monthsPerRow) // Group the months into horizontal 
rows
         // Format each row
         .map!(r =>
                 r.formatMonths() // By formatting each month
                  .array() // Storing each month's formatting in a 
row buffer
                  // Horizontally pasting each respective month's 
lines together
                  .pasteBlocks(colSpacing)
                  .join("\n"))
         // Insert a blank line between each row
         .join("\n\n");
}


More information about the Digitalmars-d mailing list