Re: How come a count of a range becomes 0 before a foreach?

2023-04-08 Thread ikelaiah via Digitalmars-d-learn
On Sunday, 9 April 2023 at 03:39:52 UTC, Steven Schveighoffer 
wrote:

On 4/8/23 9:38 PM, ikelaiah wrote:
// Get files in specified inputPath variable with a specific 
extension
     auto rmdFiles = file.dirEntries(inputPath, 
file.SpanMode.shallow)

     .filter!(f => f.isFile)
     .filter!(f => f.name.endsWith(fileEndsWith));

     // LINE 72 -- WARNING -- If we count the range here, 
later it will become 0 in line 82
     writeln(programName ~ ": number of files found " ~ 
to!string(rmdFiles.walkLength));


dirEntries returns an *input range*, not a *forward range*. 
This means that once it's iterated, it's done.


If you want to iterate it twice, you'll have to construct it 
twice.


-Steve


Steve,

You're absolutely right. I did not read the manual correctly.

It is clearly written 
[here](https://dlang.org/library/std/file/dir_entries.html) that 
`dirEntry` is an `input range`.


I will modify the code to construct it twice.
Many thanks!

-ikelaiah


Re: How come a count of a range becomes 0 before a foreach?

2023-04-08 Thread Steven Schveighoffer via Digitalmars-d-learn

On 4/8/23 9:38 PM, ikelaiah wrote:

// Get files in specified inputPath variable with a specific extension
     auto rmdFiles = file.dirEntries(inputPath, file.SpanMode.shallow)
     .filter!(f => f.isFile)
     .filter!(f => f.name.endsWith(fileEndsWith));

     // LINE 72 -- WARNING -- If we count the range here, later it will 
become 0 in line 82
     writeln(programName ~ ": number of files found " ~ 
to!string(rmdFiles.walkLength));


dirEntries returns an *input range*, not a *forward range*. This means 
that once it's iterated, it's done.


If you want to iterate it twice, you'll have to construct it twice.

-Steve


How come a count of a range becomes 0 before a foreach?

2023-04-08 Thread ikelaiah via Digitalmars-d-learn

Hi,

I've written a file that converts Rmd (R Markdown file), to a 
MarkDown file.


All works well if and only if line 73 is commented out.

I marked line 72 in the code below.

If line 73 is not commented out, `foreach` does not execute as 
the `rmdFiles.walklength` in line 82 becomes `0`.


How does `rmdFiles.walkLength` becomes `0` before the `foreach`? 
I'm a but confused.


Can someone clarify? Thank you.


```d
module rmd2md;

import std.algorithm;
import std.stdio;
import file = std.file;
import std.conv;
import std.regex;
import std.getopt;
import std.path;
import std.datetime;
import std.parallelism;
import std.range;

void main(string[] args)
{
// Set variables for the main program
string programName = "Rmd2md";

// Setup Regex for capturing Rmd code snippet header
Regex!char re = regex(r"`{3}\{r[a-zA-Z0-9= ]*\}", "g");

// Set default values for the arguments
string inputPath = file.getcwd();
string fileEndsWith = ".Rmd";
string outputPath = file.getcwd();

// Set GetOpt variables
auto helpInformation = getopt(
args,
"path|p", "Path of Rmd files. Default: current working 
directory.", &inputPath,
"fext|e", "Extension of Rmd files. Default: `.Rmd`", 
&fileEndsWith,
"fout|o", "Output folder to save the MD files. Default: 
current working directory.", &outputPath

);

if (helpInformation.helpWanted)
{
defaultGetoptPrinter("Rmd to Markdown (md) file 
converter.",

helpInformation.options);
return;
}

// is the path valid?
if (!std.path.isValidPath(inputPath))
{
writeln(programName ~ ": invalid input path");
return;
}

// is output path valid?
if (!std.path.isValidPath(outputPath))
{
writeln(programName ~ ": invalid output path");
return;
}

// is file extension valid?
if (!startsWith(fileEndsWith, "."))
{
writeln(programName ~ ": invalid extension given");
return;
}

writeln(programName ~ ": input directory is " ~ inputPath);
writeln(programName ~ ": output directory is " ~ outputPath);
writeln(programName ~ ": ...");

// Get files in specified inputPath variable with a specific 
extension
auto rmdFiles = file.dirEntries(inputPath, 
file.SpanMode.shallow)

.filter!(f => f.isFile)
.filter!(f => f.name.endsWith(fileEndsWith));

// LINE 72 -- WARNING -- If we count the range here, later it 
will become 0 in line 82
writeln(programName ~ ": number of files found " ~ 
to!string(rmdFiles.walkLength));


// Get start time
auto stattime = Clock.currTime();

// Process each Rmd file
int fileWrittenCount = 0;

// LINE 81 -- WARNING -- if line 73 is not commented out, the 
walkLength returns 0
writeln(programName ~ ": number of files found " ~ 
to!string(rmdFiles.walkLength));


foreach (file.DirEntry item; parallel(rmdFiles))
{
writeln(programName ~ ": processing " ~ item.name);

try
{
// Read content as string
string content = file.readText(item.name);
// Replace ```{r} or ```{r option1=value} with ```R
string modified = replaceAll(content, re, "```R");
// Set the Markdown output file
string outputFile = replaceAll(baseName(item.name), 
regex(r".Rmd"), ".md");
// Build an output path, using output path and 
baseName(item.name)
string outputFilenamePath = buildPath(outputPath, 
outputFile);

// Save output Markdown file
file.write(outputFilenamePath, modified);
writeln(programName ~ ": written " ~ 
outputFilenamePath);
// Increase counter to indicate number of files 
processed

fileWrittenCount++;
}
catch (file.FileException e)
{
writeln(programName ~ ": " ~ e.msg);
}
}

writeln(programName ~ ": ...");

// Gett end clock
auto endttime = Clock.currTime();
auto duration = endttime - stattime;
writeln("Duration: ", duration);

// Console output a summary
writeln(programName ~ ": written " ~ 
to!string(fileWrittenCount) ~ " files");

}
```


For testing, you can create a text file, save as `.Rmd` in the 
same folder as the D file. Run the script as:


```bash
rdmd rmd2md.d
```

It will find the `.Rmd` file in current path and save it in the 
current path.


Re: How to setup D with SFML? (using bindbc-sfml)

2023-04-08 Thread Salih Dincer via Digitalmars-d-learn

On Saturday, 8 April 2023 at 23:40:32 UTC, Mike Parker wrote:
Not without error messages. The first thing you should do is 
use the error API in bindbc.loader to print them out. That 
should tell you what the problem is.


I installed it on my Linux system without using a loader and with 
static SFML. I just used apt-get and dub. I did the following in 
order:


1. sudo apt-get install libsfml-dev libcsfml-dev
2. dub init dsfml bindbc-sfml
3. cd dsfml
4. edit dub.json and add:
```java
  "libs": [
"csfml-audio",
"csfml-graphics"
],
  "subConfigurations": {
"bindbc-sfml": "staticBC"
},
  "versions": [
"SFML_Audio",
"SFML_Graphics"
],
```
5. dub

**app.d**
```d
import bindbc.sfml;

void main()
{
  sfContextSettings* settings;
  sfEvent event;

  auto window = sfRenderWindow_create(
sfVideoMode(750, 500),
"Japanese Flag",
sfWindowStyle.sfDefaultStyle,
settings
  );

  auto flag = sfCircleShape_create();
   flag.sfCircleShape_setRadius(150);
   flag.sfCircleShape_setPosition(sfVector2f(225, 100));
   flag.sfCircleShape_setFillColor(sfRed);

  while(window.sfRenderWindow_isOpen())
  {
while(window.sfRenderWindow_pollEvent(&event))
{
  if(event.type == sfEventType.sfEvtClosed)
  {
window.sfRenderWindow_close();
  }
}

window.sfRenderWindow_clear(sfWhite);
window.sfRenderWindow_drawCircleShape(flag, null);
window.sfRenderWindow_display();
  }
}
```

SDB@79


Re: How to setup D with SFML? (using bindbc-sfml)

2023-04-08 Thread Mike Parker via Digitalmars-d-learn

On Saturday, 8 April 2023 at 11:31:40 UTC, Ki Rill wrote:
How do I set up a D and SFML project using the `bindbc-sfml` 
package?


I tried following the instructions, it builds successfully, but 
fails to load the SFML library at runtime.


In particular, `loadSFML, loadSFMLGraphics, loadSFMLXXX` fails. 
Here is 
[link](https://github.com/rillki/d-sfml-project-template) to 
the repo. I plan to create another how-to video, but I cannot 
find what's causing it to fail.


Do you have any ideas?


Not without error messages. The first thing you should do is use 
the error API in bindbc.loader to print them out. That should 
tell you what the problem is.


How to setup D with SFML? (using bindbc-sfml)

2023-04-08 Thread Ki Rill via Digitalmars-d-learn
How do I set up a D and SFML project using the `bindbc-sfml` 
package?


I tried following the instructions, it builds successfully, but 
fails to load the SFML library at runtime.


In particular, `loadSFML, loadSFMLGraphics, loadSFMLXXX` fails. 
Here is [link](https://github.com/rillki/d-sfml-project-template) 
to the repo. I plan to create another how-to video, but I cannot 
find what's causing it to fail.


Do you have any ideas?