Phobos runnable examples - ideas request
nazriel
spam at dzfl.pl
Thu Mar 28 10:09:55 PDT 2013
On 03/28/2013 09:53 AM, 1100110 wrote:
>
> I'll work on 1. and see if we can't come up with something better!
>
> The code snippets are pretty much necessary IMO. They just need to be
> fixed.
> ----
> int[] a = ...;
> static bool greater(int a, int b)
> {
> return a > b;
> }
> sort!(greater)(a); // predicate as alias
> sort!("a > b")(a); // predicate as string
> // (no ambiguity with array name)
> sort(a); // no predicate, "a < b" is implicit
> ----
>
>
> So what about something like this then?
> It's not *great*, but it actually runs now.
>
> ---- void main() { import std.algorithm;
>
> int[] a = [7,5,6];
>
> bool greater(int a, int b)
> {
> return a > b;
> }
> // Here are a few different methods,
> // Please note I'm doing the same work 3 times.
> sort!(greater)(a); // predicate as alias
> sort!("a > b")(a); // predicate as string (no ambiguity with array
> name)
> sort(a); // no predicate, "a < b" is implicit
> assert(isSorted(a));// Proof that it works as expected.
>
> ---- }
>
>
> I was gonna come up with a short script to grab all the examples, but
> apparently github is very protective of out code, and I really don't
> wanna do html...
>
> It'd be great if the js stuck the "void main()" and the imports in for
> us at least.
Yeah, option 1) may work.
Although in most cases import moduleWeAreIn; isn't enough.
So I guess stabbing default pack of imports maybe necessary.
We can work out this way: Adding JS hash-map that will map Phobos
modules to imports pack. Something like this:
imports["std.algorithm"] = "std.algorithm; std.file: write; std.process:
shell";
What ya think? It will also allow to resolve symbol conflicts like
std.stdio.write vs std.file.write
The default view of example could stay in tact (of course validity of
code needs to be fixed anyways), but when you Click edit, script would
wrap example in void main() {} and append import pack I mentioned above.
However I think that option number 2) may be more scalable.
Think about like this:
By default all examples are parsed and every snippet is wrapped in void
main(){}.
Script then fetches data for module we are in. Let's say: std.algorithm;
What script gets is:
- imports table - default set of imports that is appended to every
snippet in module.
- special cases - snippets marked explicitly may have additional options
like: don't append main() block, add additional imports to certain
snippet, replace module imports table with fetched ones, add default
standard input, add default standard arguments, don't make this snippet
runnable etc.
Why it's better than option number 1?
Maintaining this data set is way much more easy because you don't need
to edit java script file by hand, compute md5sum, make all necessary
changes and then make pull request that may wait a bit in pull request
queue. You just enter (for example) http://paste.dlang.org/examples
search for certain example in database, if it doesn't exist you just add
it and make all changes you need. Then depending on what way we pick,
request is added to queue and moderator accepts it or rejects OR it just
works wiki-like, so change is visible after you submit changes.
What you think?
Anyways, if you have any questions feel free to mail me at nazriel at dzfl.pl
--
Best regards
Damian Ziemba
More information about the Digitalmars-d
mailing list