Showing posts with label D-language. Show all posts
Showing posts with label D-language. Show all posts

Saturday, April 15, 2017

Andrei Alexandrescu's AMA on IAmA reddit (C++ and D are some topics)

By Vasudev Ram

Just saw this today via a link while browsing articles about DLang (the D Language):

Andrei Alexandrescu's AMA on IAmA reddit

It starts off like this:

[ I am a member of Facebook's HHVM team, a C++ and D pundit, and a Machine Learning guy. Ask me anything!

Background:
My name is Andrei Alexandrescu. I've been a Research Scientist at Facebook for almost four years, during which I've worked on various projects mainly around Machine Learning and systems programming. In January I joined the HHVM project, which aims at improving the productivity and performance of PHP on Facebook's server infrastructure.
I've also written a couple of books: Modern C++ Design, C++ Coding Standards (together with Herb Sutter), and The D Programming Language, as well as some papers and articles. Some of my talks are on video (this is the most recent).
Looking forward to answering your questions! ]

I read part of the thread (so far); there are some interesting questions and answers in it, about both C++ and D (and maybe some other stuff I haven't seen yet). One such interesting point was this, where, in reply to a question, he says he likes to brag about inventing D's scope statement :-)

Some excerpts below:

[
chefox 17 points 3 years ago
What's the biggest lesson learned from D that you wish other languages would apply?

andralex 36 points 3 years ago
The scope statement. It's rare that I enjoy bragging about something, but I do like to brag that I invented a new control flow statement (which is awesome because they're so few!).
I think many languages implement some form of deferred execution, but most miss the point - Lisp's with-open-file, Java's try/finally, Go's defer, C#'s using are all sorely wanting.
]

Another one:

[ ... We have had an incredible time with compile-time evaluation in D for years, and I'm glad C++ "stole" that idea. ... ]

Another:

[
miotatsu 29 points 3 years ago
what has been your happiest moment with programming?

andralex 54 points 3 years ago

I remember moments when I'd run a little program again and again with slightly different inputs just to revel in the joy of having done the proper incantations that make the machine do this and that and the other, like a clumsy but loyal genie. I mentioned I wrote this floppy disk formatter - it gave me a lot of joy to be able to tell it the complicated sequence of things I wanted to get done, to see how it ended up carving magnetized trenches into the physical world.

]

Comment about the floppy driver formatter:
[
My first application was a keyboard driver in assembler. It used only 782 bytes of memory as opposed to the default of 4KB. I wrote it because I only had little RAM available.
My second application was a floppy disk formatter written in C++. It formatted 5.25'' floppies to 1.6 MB. Floppies were expensive and I couldn't afford many. Of course, they didn't retain information very well, but long-term retention wasn't at the top of my list :o).
]

About functional programming:
[
amzeratul 11 points 3 years ago
Lots of functional programming features are now available in languages such as C++ and D - what do you think are some of the functional features that we'll see incorporated in languages such as those over the next decade or two?

andralex[S] 17 points 3 years ago
There's talk about purity in C++, but beyond that I'm not sure whether there are plans for major FP-related additions.
Of the usual suspects present in FP languages, D notably misses pattern matching. It is in tension with OOP-style (first match vs. best match), and I'm not sure whether or not it's a fundamental feature of functional style. There are no plans to add such at this time.
]

[
p0nce 9 points 3 years ago
How much more productive do you think D can really be if adopted at C++ scale? Me and other enthusiasts I know are heavily biased by our positive solo experiences with it.

andralex[S] 10 points 3 years ago
Productivity and its variations are difficult to measure. Build speeds alone, at one order of magnitude speedup, are dramatic enough to exert a change of paradigm. For example, many people say dynamic languages are productive because they have the "right" execution model - save file, hit Refresh. If actual times for a compiled language drop to the point of offering the same model, I think a whole category of perceptions would change.
One thing I noticed with D is its "plasticity". Once you have a body of code that works in C++, the natural tendency is to be conservative about changing it: unit testing is tenuous, subtle failure scenarios upon changes are legion, not to mention build times etc. In D, it's a lot easier to mold and remold designs are you go because you know you wont be penalized for it.
]

[
Ingrater 10 points 3 years ago
Following the developement of D you get the feeling that way to many developement resources are spend on new features instead of finishing existing ones. For example a lot of work has been put into user defined attributes while other features are unfinished, unstable or not even usable (alias this, shared, export, structs). Whats your opinion on this?

andralex[S] 14 points 3 years ago
I agree that we should focus more on completing, streamlining, and using what we've got. This is happening already - it's been a while since quality has been at the top of our list, and the positive PR has been visibly improving.
Now that we have more resources there is some amount of parallel work we could do, and the mixed blessing with volunteer work is people work on what they find interesting, not necessarily what's best to do at the moment. This has been a challenge, but at the same time a good problem to have.
]

[
jfernand 10 points 3 years ago
Andralex,
No question to ask, but the comment that I carry the D language book with me at all times, and re-read it like it is a novel; I love every line of code I write in D, and the language and its design choices make me a better programmer the more I understand them. D simply makes sense, and it is a shame that it does not have more traction... I am an seasoned C/C++/Perl programmer, and D is in many ways the epitome of the history of languages, in my opinion.

andralex[S] 9 points 3 years ago
Thank you! No worry, we'll figure out the traction issue :o).
]

There's a lot more to the thread, check it out. Here it is again:

Andrei Alexandrescu's AMA on IAmA reddit



- Vasudev Ram - Online Python training and consulting

Get updates (via Gumroad) on my forthcoming apps and content.

Jump to posts: Python * DLang * xtopdf

Subscribe to my blog by email

My ActiveState Code recipes

Follow me on: LinkedIn * Twitter

Are you a blogger with some traffic? Get Convertkit:

Email marketing for professional bloggers



Monday, April 10, 2017

Porting the text pager from Python to D (DLang)

By Vasudev Ram

$ tp file.txt
$ dir /s | tp


A few weeks ago, I had written this post:

tp, a simple text pager in Python

Some days ago, I thought of porting the pager from Python to D, a.k.a. the D language, and did so. The Python version was called tp.py (tp for Text Pager). So the D version is called tp.d.

The Wikipedia article about D says:

[ Though it originated as a re-engineering of C++, D is a distinct language, having redesigned some core C++ features while also taking inspiration from other languages, notably Java, Python, Ruby, C#, and Eiffel. ]

Here is the code for tp.d.
/*
File: tp.d
Purpose: A simple text pager.
Version: 0.1
Platform: Windows-only.
May be adaptable for Unix using tty / termios calls.
Only the use of getch() may need to be changed.
Author: Vasudev Ram
Copyright 2017 Vasudev Ram
Web site: https://vasudevram.github.io
Blog: https://jugad2.blogspot.com
Product store: https://gumroad.com/vasudevram
Twitter: https://mobile.twitter.com/vasudevram
*/

import std.stdio;
import std.string;
import std.file;

extern (C) int getch();

void usage(string[] args) {
    stderr.writeln("Usage: ", args[0], " text_file");
    stderr.writeln("or");
    stderr.writeln("command_generating_text | ", args[0]);
}

void pager(File in_fil, int lines_per_page=12, char quit_key='q')
{
    assert (lines_per_page > 1);
    int line_count = 0;
    int c;
    foreach (line; in_fil.byLine()) {
        writeln(line);
        line_count++;
        if ( (line_count % lines_per_page) == 0) {
            stdout.flush();
            c = getch();
            if (c == 'q')
                break;
            else
                line_count = 0;
        }
    }
}

int main(string[] args) {

    int lines_per_page = 20;
    File in_fil;

    try {
        if (args.length > 2) {
            usage(args);
            return 1;
        }
        else {
            if (args.length == 2) {
                in_fil = File(args[1]);
            }
            else {
                in_fil = stdin;
            }
            pager(in_fil, lines_per_page, 'q');
        }
    } catch (FileException fe) {
        stderr.writeln("Caught FileException: msg = ", fe.msg);
    } catch (Exception e) {
        stderr.writeln("Caught Exception: msg = ", e.msg);
    }
    finally {
        in_fil.close();
    }
    return 0;
}
Compile it the usual way with:
dmd tp.d
which will create tp.exe.
It can be run in either of two ways (paging a file or stdin), similar to the Python version. Here are two runs, dogfooding it:
tp tp.d
or
type tp.d | tp
As in the case of the Python pager (when run on itself), the output is the same as the source program itself, so I will not show it.

So, now I can say that tp is less than less :) [1] [2]

[1] Like Less is more
[2] Less in two senses of the word: it has less features, and a shorter name.

After writing this D pager, I did a brief visual comparison of it with the Python version, and was interested to see that the overall structure of the code was roughly the same, in the Python and D versions. Opening the file (or using stdin), reading lines from it, closing the file, getting characters from the keyboard, all were roughly similar in behavior. My guess that this is at least partly because both Python and D share a C heritage.

In fact, even the D line:

foreach (line; in_fil.byLine()) {

can, if you squint a bit, be read as the Python line:

for lin in fil.byLine()

which, while not valid Python, could be so, if you had a byLine method on file objects :)

For those interested, there was a good discussion about D on HN recently, here:

The reference D compiler is now open source (dlang.org)

Despite the thread title, it is more about the D language features than the licensing aspects.

Enjoy.

- Vasudev Ram - Online Python training and consulting

Get updates (via Gumroad) on my forthcoming apps and content.

Jump to posts: Python * DLang * xtopdf

Subscribe to my blog by email

My ActiveState Code recipes

Follow me on: LinkedIn * Twitter

Are you a blogger with some traffic? Get Convertkit:

Email marketing for professional bloggers



Saturday, April 8, 2017

The reference D compiler is now open source (dlang.org)

By Vasudev Ram


Just saw this discussion on HN:

DMD, the reference D compiler is now open source (dlang.org)

It is about this news about the D language:

dmd Backend converted to Boost License

I read part of the discussion, and also commented. As often happens, though the original post is about the change in the licensing of the back=end of the DMD compiler (Digital Mars D), the thread devolved (partly at least) into a discussion of the pros and cons of D versus some other languages, and that kind of thread is often interesting.

Updating the post to add:

- Link to this post on programming.reddit.com about the news:

The Official D Compiler Is Now Free (as in "freedom")

- As user bionsuba says there:

[ bionsuba[S] 30 points 4 hours ago
The front end as been Boost licensed for several years now. But now the backend has been relicensed thanks to Symantec giving their permission.
This has big implications for the language as a whole, as the compiler can now be included in Linux package managers. Meaning, D programs can be distributed on those same package managers. There's also the possibility that DMD can become part of the default packages on a distro! ]

And Andrei Alexandrescu (of Modern C++ Design fame), who now works with Walter Bright on the D language, adds:

[ andralex 16 points 3 hours ago
We will discuss more details and implications of the impact of open sourcing the compiler at DConf in Berlin in May. Registration is open! ]

Update 2 to add:

I'm putting below, an Ask HN thread that I had started some months ago. It got some interesting replies:

Ask HN: What are you using D (language) for?

The replies give some examples of non-trivial things that D can be used for.

Update 3 to add:

As Walter Bright say here, it's the #1 story on HN at the moment (and for a while now).

- Vasudev Ram - Online Python training and consulting

Get updates (via Gumroad) on my forthcoming apps and content.

Jump to posts: Python * DLang * xtopdf

Subscribe to my blog by email

My ActiveState Code recipes

Follow me on: LinkedIn * Twitter

Are you a blogger with some traffic? Get Convertkit:

Email marketing for professional bloggers



Wednesday, December 21, 2016

[Video] Interview: All Things D (programming language) - A Conversation with Andrei Alexandrescu

By Vasudev Ram


This is a video about the D language that I watched recently, which I found informative and interesting:

All Things D (programming language) - A Conversation with Andrei Alexandrescu

It's an interview of Andrei, I think by the same interviewer (who is not seen in this video) who did the interview of people from the D, Rust, Go and C++ teams, at the LangNext '14 event, the video of which I posted about here earlier:

Video: C++, Rust, D and Go: Panel at LangNext '14

Andrei talks about the past, features and future of D. The video is also embedded below.



- Vasudev Ram - Online Python training and consulting

Get updates (via Gumroad) on my forthcoming apps and content.

Jump to posts: Python * DLang * xtopdf

Subscribe to my blog by email

My ActiveState Code recipes

Follow me on: LinkedIn * Twitter

Managed WordPress Hosting by FlyWheel



Tuesday, December 20, 2016

Simple parallel processing in D with std.parallelism

By Vasudev Ram



Phobos image attribution

The D language has a module for parallel processing support. It is in Phobos, D's standard library (guess why it is called Phobos :). [1]
The module is called std.parallelism.

From the Phobos page:

[ Generally, the std namespace is used for the main modules in the Phobos standard library. The etc namespace is used for external C/C++ library bindings. The core namespace is used for low-level D runtime functions. ]

Here are a couple of simple D programs that together show the speedup that can be obtained by using the parallelism module. Note that this is for tasks that involve the CPU, not I/O.

The first one, student_work_sequential.d, does not use the std.parallelism module, so it does 4 tasks sequentially, i.e. one after another.

The second one, student_work_parallel.d, does use it. It does the same 4 tasks as the first. But it uses the convenience function called parallel from std.parallelism, to run the 4 tasks in parallel.
I timed the results of running each program a few times, and the parallel one was consistently over 3 times faster than the sequential one.

Each of the D programs below can be compiled with the command:
dmd program_name.d
Here is the code for student_work_sequential.d:
/*
student_work_sequential.d
Author: Vasudev Ram
Copyright 2016 Vasudev Ram
Web site: https://vasudevram.github.io
Blog: http://jugad2.blogspot.com
Product Store: https://gumroad.com/vasudevram
Twitter: https://mobile.twitter.com/vasudevram
*/

import std.stdio;
import core.thread;

struct Student {
    int number;
    void doSlowOperation() {
        writefln("The work of student %s has begun", number);
        // Wait for a while to simulate a long-lasting operation
        Thread.sleep(1.seconds);
        writefln("The work of student %s has ended", number);
    }
}

void main() {
    auto students =
        [ Student(1), Student(2), Student(3), Student(4) ];
    foreach (student; students) {
        student.doSlowOperation();
    }
}
Here is the output from running it, under the control of a command timing program written in Python:
$ python c:\util\time_command.py student_work_sequential
The work of student 1 has begun
The work of student 1 has ended
The work of student 2 has begun
The work of student 2 has ended
The work of student 3 has begun
The work of student 3 has ended
The work of student 4 has begun
The work of student 4 has ended
Command: student_work_sequential
Time taken: 4.07 seconds
Return code: 0

And here is the code for student_work_parallel.d:
/*
student_work_parallel.d
Author: Vasudev Ram
Copyright 2016 Vasudev Ram
Web site: https://vasudevram.github.io
Blog: http://jugad2.blogspot.com
Product Store: https://gumroad.com/vasudevram
Twitter: https://mobile.twitter.com/vasudevram
*/

import std.stdio;
import core.thread;
import std.parallelism;

struct Student {
    int number;
    void doSlowOperation() {
        writefln("The work of student %s has begun", number);
        // Wait for a while to simulate a long-lasting operation
        Thread.sleep(1.seconds);
        writefln("The work of student %s has ended", number);
    }
}

void main() {
    auto students =
        [ Student(1), Student(2), Student(3), Student(4) ];
    foreach (student; parallel(students)) {
        student.doSlowOperation();
    }
}
Here is the output from running it, under the control of the same command timing program:
$ python c:\util\time_command.py student_work_parallel
The work of student 1 has begun
The work of student 2 has begun
The work of student 3 has begun
The work of student 4 has begun
The work of student 1 has ended
The work of student 2 has ended
The work of student 3 has ended
The work of student 4 has ended
Command: student_work_parallel
Time taken: 1.09 seconds
Return code: 0
We can see that the parallel version runs 3 times faster than the sequential one. And though there was some fluctuation in the exact ratio, it was in this range (3:1) over the half-dozen tests that I ran.

Not bad, considering that the only differences between the two programs is that the parallel one imports std.parallelism and uses this line:
foreach (student; parallel(students)) {
instead of this one used by the sequential program:
foreach (student; students) {
Also, messages about tasks started later in the sequence, appear before earlier tasks have ended, which shows that things are running in parallel, in the second program.

So (as the docs say) the parallel function from std.parallelism is a convenient high-level wrapper for some of the lower-level functionality of the parallelism module, and may suffice for some parallel processing use cases, without needing to do anything more. That is useful.

[1] The D standard library is called Phobos because Phobos is a moon of Mars (and hence a satellite of it. And Mars was the original name of the D language, given by its creator, Walter Bright (his company is/was called Digital Mars). But he says that many of his friends started calling it D (as it was like a better C), so he did too, and changed the name to D. So, Phobos (the library) is a "satellite" of D, a.k.a. Mars (the language) :) You can read an interview of Walter Bright (on the D blog) via this post:

Interview: Ruminations on D: Walter Bright, DLang creator

The post also has an HN thread about the interview. It also links to a video about D and other systems programming languages.

The Wikipedia article on Phobos is interesting. It says that the 'orbital motion of Phobos has been intensively studied, making it "the best studied natural satellite in the Solar System" in terms of orbits completed'. It also says where the name Phobos came from (a Greek god), and the source of the names of geographical features on Phobos (the book Gulliver's Travels). Also, Phobos has very low gravity (not even enough to keep it round in shape), so low that:

"A 68 kg (150 lb) person standing on the surface of Phobos would weigh the equivalent to about 60 g (2 oz) on Earth.[29]"

Maybe that is why the astronomers named some of the places on Phobos after Gulliver's Travels - because of Lilliput :)

The image at the top of the post is of Phobos and Deimos orbiting Mars.

- Vasudev Ram - Online Python training and consulting

Get updates on my software products / ebooks / courses.

Jump to posts: Python   DLang   xtopdf

Subscribe to my blog by email

My ActiveState recipes

FlyWheel - Managed WordPress Hosting



Sunday, November 27, 2016

Video: Interview: GoingNative 6: Walter Bright and Andrei Alexandrescu - D Programming Language

By Vasudev Ram


I watched this video (from 2013) a few days ago:

GoingNative 6: Walter Bright and Andrei Alexandrescu - D Programming Language

Going Native [1] is a tech event, and I guess 6 refers to the 6th year or edition of it. I first saw this video on Channel 9, one of Microsoft's technology sites. That site has multiple links to the video, including downloadable, in different qualities, so you may want to check out getting it from there. [2]

[1] There seems to be rising interest in "native" (programming) languages these days (before the rise of "scripting" languages, all languages were native, i.e. they ran, after compilation, on the "metal" (i.e. the CPU), not on a language VM or interpreter, and we just called them languages, no fancy-shmancy names, ha ha :). Part of the reason for the rise in interest, as mentioned in the video, is that we are entering (or have entered) a world of multi-core and concurrency, and due to Moore's law ending (?) Anyway, native languages like D, Go and Rust are picking up these days, whatever the reason. I've used native languages like Pascal and C a lot at work earlier, so I welcome this return of greater interest in native languages, though I'm not stopping using languages like Python anytime soon either.

[2] And to download the video at the command line, check out youtube-dl, a YouTube downloader in Python. Yes, downloading a video about native languages with a tool written in a scripting language - go figure :)

The video shows an interviewer talking to both Walter Bright and Andrei Alexandrescu. Walter, who developed the first native C++ compiler, is the original creator of the D programming language and Andrei, who was a C++ expert and book author, joined him some years later to help in developing the language. The interview was somewhat wide-ranging and a bit long, and quite interesting. Many aspects of the language and the plans and in-progress work for it were talked about.

The video is also embedded below.



Happy holidays to my readers.

- Vasudev Ram - Online Python training and consulting

Get updates on my software products / ebooks / courses.

Jump to posts: Python   DLang   xtopdf

Black Flyday at Flywheel Wordpress Managed Hosting - get 3 months free on the annual plan.

Subscribe to my blog by email

My ActiveState recipes



Friday, October 14, 2016

Command line D utility - find files matching a pattern under a directory

By Vasudev Ram

Hi readers,

I wrote this utility in D recently, to find files matching a pattern (a wildcard) under a specified directory. Here is the program, find_files.d:
/************************************************************************
find_files.d
Author: Vasudev Ram
Compile with Digital Mars D compiler using the command:
dmd find_files.d
Usage: find_files dir patt
Finds files under directory 'dir' that match file wildcard 
pattern 'patt'.
The usual file wildcard patterns that an OS like Windows or 
Linux supports, such as *.txt, budget*.xls, my_files*, etc.,
are supported.
Copyright 2016 Vasudev Ram
Web site: https://vasudevram.github.io
Blog: http://jugad2.blogspot.com
Product updates: https://gumroad.com/vasudevram/follow
************************************************************************/

import std.stdio; 
import std.file;

string __version__ = "0.1";

void usage(string[] args) {
    debug {
        stderr.write("You gave the command: ");
        foreach(arg; args)
            stderr.write(arg, " ");
        stderr.writeln;
    }
    
    stderr.writeln(args[0], " (Find Files) v", __version__);
    stderr.writeln("Copyright 2016 Vasudev Ram" ~
    " - https://vasudevram.github.io");
    stderr.writeln("Usage: ", args[0], " dir pattern");
    stderr.writeln("Recursively find filenames, under directory 'dir', ");
    stderr.writeln("that match the literal or wildcard string 'pattern'.");
}

int main(string[] args) {

    // Check for and get correct command-line arguments.
    if (args.length != 3) {
        usage(args);
        return 1;
    }
    auto top_dir = args[1];
    if (!exists(top_dir) || !isDir(top_dir)) {
        stderr.writeln("The name ", top_dir, 
        " does not exist or is not a directory.");
        usage(args);
        return 1;
    }
    auto pattern = args[2];

    try {
        debug writeln(args[0], ": Finding files under directory: ", 
        top_dir, " that match pattern: ", pattern);
        foreach (de; dirEntries(top_dir, pattern, SpanMode.breadth)) {
            writeln(de.name);
        }
    }
    catch (Exception e) {
        stderr.writeln("Caught Exception: msg = ", e.msg);
        return 1;
    }
    return 0;
}
Compile command:
$ dmd -offf find_files.d
The -of flag is for "output file", and is used to specify the .EXE file to be created. The command creates ff.exe on Windows).

Compile command to create debug version (the statements prefixed 'debug' will also run):
$ dmd -debug -offf find_files.d

Here is the output from a few test runs of the find_files utility:

Run with no arguments - shows usage:
$ ff
ff (Find Files) v0.1
Copyright 2016 Vasudev Ram - https://vasudevram.github.io
Usage: ff dir pattern
Recursively find filenames, under directory 'dir',
that match the literal or wildcard string 'pattern'.
Run with one argument, a non-existent directory - shows usage, since number of arguments is invalid:
$ ff z
ff (Find Files) v0.1
Copyright 2016 Vasudev Ram - https://vasudevram.github.io
Usage: ff dir pattern
Recursively find filenames, under directory 'dir',
that match the literal or wildcard string 'pattern'.
Run with two argument, a non-existent directory and a file wildcard, * - gives message that the directory does not exist, and shows usage:
$ ff z *
The name z does not exist or is not a directory.
ff (Find Files) v0.1
Copyright 2016 Vasudev Ram - https://vasudevram.github.io
Usage: ff dir pattern
Recursively find filenames, under directory 'dir',
that match the literal or wildcard string 'pattern'.
Run with two argument, an existing directory and a file wildcard, * - gives the output of the files matching the wildcard under that directory (recursively):
$ ff . *
.\dir1
.\dir1\a.txt
.\dir1\dir11
.\dir1\dir11\b.txt
.\dir2
.\dir2\c.txt
.\dir2\dir21
.\dir2\dir21\d.txt
.\ff.exe
.\ff.obj
.\ffd.exe
.\find_files.d
.\find_files.d~
Note: It will find hidden files too, i.e. those with a H attribute, as shown by the DOS command DIR /A. If you find any issue with the utility, please describe it, with the error message, in the comments.

You can get the find_files utility (as source code) from here on my Gumroad product store:

https://gum.co/find_files

Compile the find_files.d program with the Digital Mars D compiler using the command:

dmd find_files.d

whhich will create the executable file find_files.exe.

Run:

find_files

to get help on the usage of the utility. The examples above in this post have more details.

- Enjoy.

Drawing of magnifying glass at top of post, by:

Yours truly.

- Vasudev Ram - Online Python training and consulting

Get updates on my software products / ebooks / courses.

Jump to posts: Python   DLang   xtopdf

Subscribe to my blog by email

My ActiveState recipes

Managed WordPress Hosting by FlyWheel



Wednesday, September 14, 2016

Func-y D + Python pipeline to generate PDF

By Vasudev Ram



Hi, readers,

Here is a pipeline that generates some output from a D (language) program and passes it to a Python program that converts that output to PDF. The D program makes use of a bit of (simple) template programming / generics and a bit of functional programming (using the std.functional module from Phobos, D's standard library).


D => Python


I'm showing this as yet another example of the uses of xtopdf, my Python toolkit for PDF creation, as well as for the D part, which is fun (pun intended :), and because those D features are powerful.

The D program is derived, with some modifications, from a program in this post by Gary Willoughby,

More hidden treasure in the D standard library .

That program demonstrates, among other things, the pipe feature of D from the std.functional module.

First, the D program, student_grades.d:
/*
student_grades.d
Author: Vasudev Ram
Web site: https://vasudevram.github.io
Blog: http://jugad2.blogspot.com
Product store: https://gumroad.com/vasudevram
Adapts code from:
http://nomad.so/2015/08/more-hidden-treasure-in-the-d-standard-library/
*/

import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
import std.functional;

// Set up the functional pipeline by composing some functions.
alias sumString = pipe!(split, map!(to!(int)), sum);

void main(string[] args)
{
    // Data to be transformed:
    // Each string has the student name followed by 
    // their grade in 5 subjects.
    auto student_grades_list = 
    [
        "A 1 2 3 4 5",
        "B 2 3 4 5 6",
        "C 3 4 5 6 7",
        "D 4 5 6 7 8",
        "E 5 6 7 8 9",
    ];

    // Transform the data for each student.
    foreach(student_grades; student_grades_list) {
        auto student = student_grades[0]; // name
        auto total = sumString(student_grades[2..$]); // grade total
        writeln("Grade total for student ", student, ": ", total);
    }
}
The initial data (which the D program transforms) is hard-coded, but that can easily be changed to read it from a text file, for instance. The program uses pipe() to compose [1] the functions split, map (to int), and sum (some of which are generic / template functions).

So, when it is run, each string (student_grades) of the input array student_grades_list is split (into an array of smaller strings, by space as the delimiter); then each string in the array (except for the first, which is the name), is mapped (converted) to integer; then all the integers are summed up to get the student's grade total; finally these names and totals are written to standard output. That becomes the input to the next stage of the pipeline, the Python program, which does the conversion to PDF.

Build the D program with:
dmd -o- student_grades.d
which gives us the executable, student_grades(.exe).

The Python part of the pipeline is StdinToPDF.py, one of the apps in the xtopdf toolkit, which is designed to be used in pipelines such as the above - basically, any Unix, Windows or Mac OS X pipeline, that generates text as its final output, can be further terminated by StdinToPDF, resulting in conversion of that text to PDF. It is a small Python app written using the core class of the xtopdf library, PDFWriter. Here is the original post about StdinToPDF:

[xtopdf] PDFWriter can create PDF from standard input

Here is the pipeline:
$ student_grades | python StdinToPDF.py sg.pdf

Below is a cropped screenshot of the generated output, sg.pdf, as seen in Foxit PDF Reader.

[1] Speaking of functional composition, you may like to check out this earlier post by me:

fmap(), "inverse" of Python map() function

It's about a different way of composing functions (for certain cases). It also has an interesting comment exchange between me and a reader, who showed both how to do it in Scala, and another way to do it in Python. Also, there is another way to compose functions in D, using a function called compose; it works like pipe, but the composed functions are written in the reverse order. It's in the same D module as pipe, std.functional.

Finally (before I pipe down - for today :), check out this HN comment by me (on another topic), in which I mention and link to multiple other ways of doing pipe-like stuff in Python:

Comment on Streem – a new programming language from Matz

- Enjoy.

- Vasudev Ram - Online Python training and consulting

Get updates on my software products / ebooks / courses.

Jump to posts: Python   DLang   xtopdf

Subscribe to my blog by email

My ActiveState recipes



Wednesday, August 31, 2016

Interview: Ruminations on D: Walter Bright, DLang creator


By Vasudev Ram

I read this interview yesterday:

Ruminations on D: An Interview with Walter Bright (creator of the D programming language).

(Walter Bright (Wikipedia))

The interview also links to this talk between Walter and Andrei Alexandrescu, of Modern C++ fame, who joined Walter to work on D (after working at Facebook for some time)) about a D project, warp (a C++ preprocessor), that Walter did for Facebook; the talk is about the D benefits Walter found while working on the project (such as the ease of changing the data structures to try different approaches, and how the ranges and the algorithms that operate upon them are somewhat decoupled).

(Andrei Alexandrescu (Wikipedia))

HN thread about the interview.

You can also check out this video, about the creators or key team members of 4 languages, C++, D, Golang and Rust, talking at LangNext '14:

Video: C++, Rust, D and Go: Panel at LangNext '14

- Vasudev Ram - Online Python training and consulting

Get updates on my software products / ebooks / courses.

Jump to posts: Python   DLang   xtopdf

Subscribe to my blog by email

My ActiveState recipes




Sunday, July 31, 2016

deltildefiles: D language utility to recursively delete vim backup files

By Vasudev Ram

~

Here's a small utility I wrote in D (the D programming language). The utility is called deltildefiles (for "delete tilde files"). It lets you delete all files whose names end in "~", the tilde character (Wikipedia), under a specified directory, recursively, i.e. including such files found in sub-directories.

[ BTW, check out the Wikipedia article about tilde, linked above. It has many interesting uses in various domains, not just computer programming, for example, in physics, mathematics, economics, electronics and even juggling :) ]

The vim editor (if the option :se backup is set) names its backups of the files you edit, with the same file name as the original file, plus a tilde at the end. Of course, you can do the same task as this utility deltildefiles, with just a batch file (or two) which uses DEL (with or without /S) on Windows), so I just wrote this for fun and learning. However, if you want to enhance the task with other conditions, etc., then a program (or at least a script, in Python, Perl or other language) may be the better way to go, rather than writing an awkward batch file (even if that is possible) or using PowerShell (haven't used the latter). Another alternative is to use a shell script on Linux, or if you use Cygwin or other Unix emulation on Windows, to use the find and rm commands, that come with it, like:

$ find $dirName -print -name '*~' -exec rm {} \; # or some variation

Be careful with the above command. If you make a typing mistake, like omitting the ~, it could end up deleting the wrong files, and potentially many of them. This is also one reason to make the command into a binary (like I have done) or a shell script, test it thoroughly with dummy / safe data, and from then onward, only use that, instead of typing the find command each time - at least for commands that can delete data or cause other damage.

Anyway, here is the code for deltildefiles.d:
/****************************************************************
File: deltildefiles.d
Purpose: To delete vim backup files, i.e. files ending with ~.
Compile with:
$ dmd deltildefiles.d

Author: Vasudev Ram
Copyright 2016 Vasudev Ram
Web site: https://vasudevram.github.io
Products: https://gumroad.com/vasudevram

Description: To delete all files whose names end with 
the tilde character (~) in the directory subtree 
specified as the command-line argument. 

When you edit a file abc.txt with the vim editor, it will 
first make a backup in the file abc.txt~ (note ~ character
at end of file name). Over time, these files can accumulate.
This utility helps you to delete all such vim backup files 
in a specified directory and its subdirectories.

Use with caution and at your own risk!!!
On most operating systems, once a file is deleted this way
(versus sending to the Recycle Bin on Windows), it is not 
recoverable, unless you have installed some undelete utility.
****************************************************************/

import std.stdio;
import std.file;

void usage(string[] args) {
    stderr.writeln("Usage: ", args[0], " dirName");
    stderr.writeln(
        "Recursively delete files whose names end with ~ under dirName.");
}

int main(string[] args) {
    if (args.length != 2) {
        usage(args);
        return 1;
    }
    string dirName = args[1];
    // Check if dirName exists.
    if (!exists(dirName)) {
        stderr.writeln("Error: ", dirName, " not found. Exiting.");
        return 1;
    }
    // Check if dirName is not the NUL device and is actually a directory.
    if (dirName == "NUL" || !DirEntry(dirName).isDir()) {
        stderr.writeln("Error: ", dirName, " is not a directory. Exiting.");
        return 1;
    }
    try {
        foreach(DirEntry de; dirEntries(args[1], "*~", SpanMode.breadth)) {
            // The isFile() check may be enough, also need to check for
            // Windows vs POSIX behavior.
            if (de.isFile() && !de.isDir()) {
                writeln("Deleting ", de.name());
                remove(de.name());
            }
        }
    } catch (FileException) {
        stderr.writeln("Caught a FileException. Exiting.");
        return 1;
    } catch (Exception) {
        stderr.writeln("Caught an Exception. Exiting.");
        return 1;
    }
    return 0;
}
Compile it with:
$ dmd deltildefiles.d
And you can run it like this:
$ deltildefiles .
which will delete all files ending in ~, in and under the current directory. If you give a file name instead of a directory name, or if you give a non-existent directory name, it gives an error message and exits.

It seems to work as of now, based on some testing I did. May have a few bugs since I haven't tested it exhaustively. May make a few improvements to it over time, such as generalizing from ~ to filename wildcards, more error handling, verbose / quiet flags, etc.

Translate this post into another language with Google Translate
(and, just for kicks, click the speaker icon below the right hand side text box at the above Translate page :).

- Vasudev Ram - Online Python training and consulting

Follow me on Gumroad to get email updates about my products.


My Python posts     Subscribe to my blog by email

My ActiveState recipes