Skip to content

Conversation

@PetarKirov
Copy link
Member

@PetarKirov PetarKirov commented Jun 15, 2017

The primary motivation is that we need to have a simple example for first time visitors.

For comparison: before and after.

Copy link
Contributor

@wilzbach wilzbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this example. How about adding it to the example code roulette as well?

index.dd Outdated
lines ? cast(double) sumLength / lines : 0.0);
import std.range, std.stdio;
auto sum = 0.0;
auto count = stdin.byLine().tee!(l => sum += l.length).walkLength;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: maybe removing the parentheses here to be more in the D-style?

@PetarKirov
Copy link
Member Author

PetarKirov commented Jun 15, 2017

I really like this example. How about adding it to the example code roulette as well?

Is there a way to share the source code between the different examples? Or perhaps we should swap this one with the rounding one?

@wilzbach
Copy link
Contributor

wilzbach commented Jun 15, 2017

Btw I just realized that we probably need to update the MD5 hash:

https://github.com/dlang/dlang.org/blob/master/js/run-main-website.js

Imho this is a really annoying approach and we get rid of this approach entirely and store the STDIN in the DOM as well. This manual hashing approach wouldn't scale anyways if we go with #1709

Is there a way to share the source code between the different examples?

From js/dlang.js:

        // [your code here] rotation for index.html
        var $examples = $('.your-code-here-extra > pre');
        if ($examples.length) {
            var n = Math.floor(Math.random() * ($examples.length+1));
            if (n)
                $('#your-code-here-default > pre').replaceWith($examples[n-1]);
        }

So in theory just adding another $(EXTRA_EXAMPLE should work...

Or perhaps we should swap this one with the rounding one?

I would leave it in the roulette for now as we only have two examples at the moment.
Maybe we can "steal" a couple from the DTour (or other)?

@wilzbach
Copy link
Contributor

Btw I just realized that we probably need to update the MD5 hash:
Imho this is a really annoying approach and we get rid of this approach entirely and store the STDIN in the DOM as well. This manual hashing approach wouldn't scale anyways if we go with #1709

And a lot of chunk is gone: 44cbc93 (#1709)

@PetarKirov
Copy link
Member Author

So what's left for me to do? Copy & paste the example to the top?

@wilzbach
Copy link
Contributor

So what's left for me to do? Copy & paste the example to the top?

Yep, but better do this as a separate PR, so that this one isn't blocked
(I don't know how long it will take for #1709 to get in ...)

@wilzbach
Copy link
Contributor

So what's left for me to do? Copy & paste the example to the top?

Once you have rebased your PR, you should be able to do this :)

@PetarKirov PetarKirov changed the title Make average line length example more concise Make average line length example more concise and move it to the top Jun 17, 2017
@PetarKirov
Copy link
Member Author

@wilzbach done. @CyberShadow @aG0aep6G what do you think?

And also make it the default. The primary motivation is that
we need to have a simple example for first time visitors.

auto sum = 0.0;
auto count = stdin.byLine
.tee!(l => sum += l.length).walkLength;
Copy link
Member

@CyberShadow CyberShadow Jun 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even shorter would be:

size_t count = 0;
double sum = stdin.byLine.tee!(l => count++).sum;

but I guess that demonstrates fewer range features.

Copy link
Member Author

@PetarKirov PetarKirov Jun 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that would compile, unless you add a map!(l => l.length) between the tee and the sum.

/d912/f975.d(7): Error: template std.algorithm.iteration.sum cannot deduce function from argument types !()(Result), candidates are:
/opt/compilers/dmd2/include/std/algorithm/iteration.d(4663): std.algorithm.iteration.sum(R)(R r) if (isInputRange!R && !isInfinite!R && is(typeof(r.front + r.front)))
/opt/compilers/dmd2/include/std/algorithm/iteration.d(4674): std.algorithm.iteration.sum(R, E)(R r, E seed) if (isInputRange!R && !isInfinite!R && is(typeof(seed = seed + r.front)))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, you're right :)

@wilzbach
Copy link
Contributor

I would leave it in the roulette for now as we only have two examples at the moment.
Maybe we can "steal" a couple from the DTour (or other sources)?

#1755

@PetarKirov
Copy link
Member Author

Ping @wilzbach @CyberShadow is this good to go?

@dlang-bot dlang-bot merged commit b5ae51c into dlang:master Jun 21, 2017
@PetarKirov
Copy link
Member Author

Thanks @CyberShadow!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants