Skip to content

Conversation

@Makinami
Copy link
Owner

One function of a larger object
We can assume that defined lambdas will not be necessary anywhere else in any foreseeable future
The main question is: is it ok, or should I declare proper methods and such?
Sam widzę kilka problemów z łapaniem lokalnych zmiennych, ale to jeszcze powinienem być wstanie ogarnąć lepiej

@Makinami Makinami requested a review from jantosi February 21, 2017 14:49
Copy link
Collaborator

@jantosi jantosi left a comment

Choose a reason for hiding this comment

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

I like how you wanted to create some atomic functions and then compose them into larger operations. Please work a bit more on expressing your intent with naming and conventions so that another developer could pick these atoms and create a new composition with ease.


auto get = [&heightmap, sizeX = mInfo.HeightmapWidth, sizeY = mInfo.HeightmapHeight](int x, int y) -> decltype(mHeightmap)::value_type
{
if (x < 0 || x >= sizeX || y < 0 || y >= sizeY)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perfect use-case of ? : operator


auto& heightmap = mHeightmap;

auto get = [&heightmap, sizeX = mInfo.HeightmapWidth, sizeY = mInfo.HeightmapHeight](int x, int y) -> decltype(mHeightmap)::value_type
Copy link
Collaborator

Choose a reason for hiding this comment

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

All of these auto variablename = ... lambdas could be refactored into declared methods on some util class. You don't need lambdas here, since these aren't brief, aren't used ad hoc and finally, variables holding these should be constants instead.

I can see the value of learning how to write proper lambdas. I would, however, write normal methods here.

auto average = [](std::initializer_list<float> list)
{
float sum = std::accumulate(std::cbegin(list), std::cend(list), 0.0,
[](auto&& a, auto&& b) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a perfect lambda: small, inline, extraction into a method would decrease simplicity. I would go even further and make it a one-liner

return a + (b != -1 ? b : 0);
});

int total = std::count_if(std::cbegin(list), std::cend(list), [](auto&& val) { return val != -1; });
Copy link
Collaborator

Choose a reason for hiding this comment

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

Filtering! Great use of lambdas 👍

std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(-1, 1);

auto random = [&dis, &gen]() { return dis(gen); };
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks familiar, function composition? I would extract composeFunctions(fn1, fn2)


auto random = [&dis, &gen]() { return dis(gen); };

std::function<void(int)> divide = [&divide, &random, &square, &diamond, sizeX = mInfo.HeightmapWidth - 1, sizeY = mInfo.HeightmapHeight - 1](int size)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I realise why capture lists are needed in C++, but this makes the perfect case against them ;) Sooo long

Also, naming of this lambda is quite confusing; dividing what into what?

divide(half);
};

set(0, 0, 0.0);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are these following lines a sort of unit-test?

{
mImmediateContext->PSSetShaderResources(2, 1, &mLayerMapArraySRV);
return;
//return;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Committing commented-out code is a no-no

{
float sum = std::accumulate(std::cbegin(list), std::cend(list), 0.0,
[](auto&& a, auto&& b) {
return a + (b != -1 ? b : 0);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this supposed to assume value 0 of -1-elements? If so, this needs to be expressed somewhere. Right now this looks like magic and could introduce hard-to-debug logic.

return heightmap[y*sizeX + x];
};

auto set = [&heightmap, sizeX = mInfo.HeightmapWidth, sizeY = mInfo.HeightmapHeight](int x, int y, auto val) -> void
Copy link
Collaborator

Choose a reason for hiding this comment

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

You're not using sizeY from capture list anywhere in this lambda

Makinami added 2 commits February 24, 2017 18:45
- update VisualCppTools
- deprecate LoadShader function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants