July 25, 2013
This D entry uses Tango, but it should also show a version for Phobos:

http://rosettacode.org/wiki/Rosetta_Code/Count_examples#D

Bye,
bearophile
July 25, 2013
> This D entry uses Tango, but it should also show a version for Phobos:
>
> http://rosettacode.org/wiki/Rosetta_Code/Count_examples#D

Two versions


The Mathematica solution is short:

TaskList = Flatten[
   Import["http://rosettacode.org/wiki/Category:Programming_Tasks", "Data"][[1, 1]]];
Print["Task \"", StringReplace[#, "_" -> " "], "\" has ",
  Length@Select[Import["http://rosettacode.org/wiki/" <> #, "Data"][[1,2]],
  StringFreeQ[#, __ ~~ "Programming Task" | __ ~~ "Omit"]& ], " example(s)"]&
  ~Map~ StringReplace[TaskList, " " -> "_"]


This Perl solution is compact:

use v5.10;
use Mojo::UserAgent;

my $site = "http://rosettacode.org";
my $list_url = "/mw/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml";

my $ua = Mojo::UserAgent->new;
$ua->get("$site$list_url")->res->dom->find('cm')->each(sub {
    (my $slug = $_->{title}) =~ tr/ /_/;
    my $count = $ua->get("$site/wiki/$slug")->res->dom->find("#toc .toclevel-1")->size;
    say "$_->{title}: $count examples";
});



The F# solution performs downloads concurrently and it's said to be fast:

#r "System.Xml.Linq.dll"

let uri1 = "http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml"
let uri2 task = sprintf "http://www.rosettacode.org/w/index.php?title=%s&action=raw" task

[|for xml in (System.Xml.Linq.XDocument.Load uri1).Root.Descendants() do
    for attrib in xml.Attributes() do
      if attrib.Name.LocalName = "title" then
        yield async {
          let uri = uri2 (attrib.Value.Replace(" ", "_") |> System.Web.HttpUtility.UrlEncode)
          use client = new System.Net.WebClient()
          let! html = client.AsyncDownloadString(System.Uri uri)
          let sols' = html.Split([|"{{header|"|], System.StringSplitOptions.None).Length - 1
          lock stdout (fun () -> printfn "%s: %d examples" attrib.Value sols')
          return sols' }|]
|> Async.Parallel
|> Async.RunSynchronously
|> fun xs -> printfn "Total: %d examples" (Seq.sum xs)


Bye,
bearophile
August 31, 2013
I have added a D entry for the "Go Fish" game:

http://rosettacode.org/wiki/Go_Fish#D

I don't know the Go Fish game, so I am not sure this code is correct. Is some of you able and willing to test its play a bit?

(This D entry is very Python-style because it's a translation of the Python entry, so it's not very strongly typed. Generally in D I prefer stronger typing, but in this case I think it's acceptable).

Bye,
bearophile
August 31, 2013
the entry :
http://rosettacode.org/wiki/File_IO
is wrong because as stated by the asignment : "In this task, the job is to
create a file called "output.txt", and place in it the contents of the file
"input.txt", *via an intermediate variable.**"*
*
*
there is no intermediate variable; I don't know if this is the right place
to post but you seem to do a lot of work for rosetta...


2013/8/31 bearophile <bearophileHUGS@lycos.com>

> I have added a D entry for the "Go Fish" game:
>
> http://rosettacode.org/wiki/**Go_Fish#D<http://rosettacode.org/wiki/Go_Fish#D>
>
> I don't know the Go Fish game, so I am not sure this code is correct. Is some of you able and willing to test its play a bit?
>
> (This D entry is very Python-style because it's a translation of the Python entry, so it's not very strongly typed. Generally in D I prefer stronger typing, but in this case I think it's acceptable).
>
> Bye,
> bearophile
>


August 31, 2013
On Saturday, 31 August 2013 at 01:42:43 UTC, bearophile wrote:
> I have added a D entry for the "Go Fish" game:

hmm there's too much text output, it makes following the game hard, and seeing what the computer drew means you can cheat!

But I think it plays correctly, I was able to finish a game.

--

Speaking of card games, did you know Windows comes with a cards.dll that can draw a set of playing cards? It is used for built in games like Solitare.

And you can use it too! Here's an example of how:

http://arsdnet.net/dcode/wincards.d

See the main function at the bottom of the file. The main() depends on (the newer version of) my simpledisplay.d and color.d (also shows how to use some of the input event improvements I've made over the last couple weeks!)

https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff


The sample program puts two cards on the screen and lets you drag them around with the mouse. Only works on MS Windows.
August 31, 2013
On 31-8-2013 4:08, maarten van damme wrote:
> the entry :
> http://rosettacode.org/wiki/File_IO
> is wrong because as stated by the asignment : "In this task, the job is to create a file called "output.txt", and place in it the contents of the file "input.txt", /via an intermediate variable.//"/
> /
> /
> there is no intermediate variable; I don't know if this is the right place to post but you seem to do a lot of work for rosetta...

It's an old task (from 2007). The task description was changed after the D entries were made.

http://rosettacode.org/mw/index.php?title=File_IO&diff=25166&oldid=21823

So it needs to be updated. Perhaps you will do the honors? :)



August 31, 2013
Jos van Uden:

> It's an old task (from 2007). The task description was changed after the D entries were made.

Yes, there are about 63 Rosettacode tasks that I have not yet updated:

accumulator_factory.d
address_of_a_variable.d
animation.d
boolean_values.d
call_a_function_in_a_shared_library.d
collections.d
concurrent_computing.d
create_an_object_at_a_given_address.d
create_a_file.d
date_format.d
delete_a_file.d
distributed_programming.d
echo_server.d
environment_variables.d
execute_a_system_command.d
execute_snusp.d
file_io.d
first_class_functions_use_numbers_analogously.d
flow_control_structures.d
formal_power_series.d
fractal_tree2.d
globally_replace_text_in_several_files.d
hello_world_graphical.d
http.d
image_noise.d
include_a_file.d
input_loop.d
json.d
literals_floating_point.d
literals_string.d
memory_layout_of_a_data_structure.d
metered_concurrency.d
multiple_distinct_objects.d
mutex.d
object_serialization.d
opengl.d
parallel_calculations1.d
pointers_and_references.d
pragmatic_directives.d
quine.d
rc_24_game.d
rename_a_file.d
rosetta_code_count_examples.d
scripted_main.d
secure_temporary_file.d
shell_one_liner.d
simple_windowed_application.d
singleton.d
sockets.d
synchronous_concurrency.d
system_time.d
test_a_function.d
user_input_text.d
variables.d
variable_size_get.d
variable_size_set.d
walk_a_directory_non_recursively.d
walk_a_directory_recursively.d
window_creation.d
xml_dom_serialization.d
xml_input.d
xml_output.d
xml_xpath.d

Bye,
bearophile
November 25, 2013
This D1 entry needs an update:
http://rosettacode.org/wiki/Metered_concurrency#D

Is someone willing to update it?


import std.stdio, core.thread, std.c.time;

class Semaphore {
    private int lockCnt, maxCnt;

    this(in int count) {
        maxCnt = lockCnt = count;
    }

    void acquire() {
        if (lockCnt < 0 || maxCnt <= 0)
            throw new Exception("Negative Lock or Zero init. Lock");
        while(lockCnt == 0)
            Thread.getThis.yield; // Let other threads release lock.
        synchronized lockCnt--;
    }

    void release() {
        synchronized
            if (lockCnt < maxCnt)
                lockCnt++;
            else
                throw new Exception("Release lock before acquire");
    }

    int getCnt() {
        synchronized return lockCnt;
    }
}

class Worker : Thread {
    private static int id = 0;
    private Semaphore lock;
    private int myId;

    this (Semaphore l) {
        super();
        lock = l;
        myId = id++;
    }

    int run() {
        lock.acquire;
        writefln("Worker %d got a lock(%d left).", myId, lock.getCnt);
        msleep(2_000); // Wait 2.0 seconds.
        lock.release;
        writefln("Worker %d released a lock(%d left).",
                 myId, lock.getCnt);
        return 0;
    }
}

void main() {
    Worker[10] crew;
    Semaphore lock = new Semaphore(4);

    foreach (ref c; crew)
        (c = new Worker(lock)).start;
    foreach (ref c; crew)
        c.wait;
}


Bye,
bearophile
January 15, 2014
I just made some scripts [0] to download and compile all D examples from Rosettacode. From 186 of 716 examples fail to compile [1]. Some for trivial reasons like not wrapped into a main function or a missing import. Some require SDL or Tango or other third-party libraries.

My ultimate goal was to use this for regression testing dmd. Anyways if people try code examples they should compile out of the box for good PR.

If you are looking for a low-barrier way to support D a little, feel free to check out the fail list [1] and fix some. :)

[0] https://bitbucket.org/qznc/rosetta/src/da12e3673b0d/compile_all/?at=master
[1] https://gist.github.com/qznc/9ba4b0e78abfc35d4694
January 16, 2014
qznc:

> I just made some scripts [0] to download and compile all D examples from Rosettacode. From 186 of 716 examples fail to compile [1]. Some for trivial reasons like not wrapped into a main function or a missing import. Some require SDL or Tango or other third-party libraries.
>
> My ultimate goal was to use this for regression testing dmd. Anyways if people try code examples they should compile out of the box for good PR.
>
> If you are looking for a low-barrier way to support D a little, feel free to check out the fail list [1] and fix some. :)
>
> [0] https://bitbucket.org/qznc/rosetta/src/da12e3673b0d/compile_all/?at=master
> [1] https://gist.github.com/qznc/9ba4b0e78abfc35d4694

I am using similar scripts written in Python since years.

Currently there are around 760-770 D programs in Rosettacode.

What version of the D compiler are you using? I am assuming Rosettacode to be compilable with the latest "bleeding edge" compiler. So if you use the latest released compiler some of the entries will not compile. Such entries should not be "fixed" at all.

Your list of failing to compile just say "fail", but there are several reasons for a program to fail.

Some programs need a "modulename_main" version to be compiled, because D lacks a built-in way to tell apart the main module of a program from the other modules. (In Python you use the "if __name__ == '__main__':" for this purpose).

Some programs don't compile because require Tango. I have not removed them all because some Tango programmer has written them and I guess such person doesn't like to see their work removed from the pages.

Some entries don't compile because they are not yet updated, or dmd has had some regressions. Thankfully Kenji usually fixes such regressions in just few days when I find them.

Bye,
bearophile