Is the proposed fold syntax for c++17 too limited?

After reading this paper, and this cppreference page, and then posting a stack overflow question about it. I went ahead and built the latest clang from source to start messing around with fold expressions. Writing a sum() function is straight forward.

template <typename... Ts>
auto sum(Ts&&... args) {
  return (args + ...);
}

The syntax is nice, but imo a bit limited. The next thing I wanted was a variadic min() function. This is where the proposed syntax starts to seem weak. Obviously the outline of the function should be easy:

template <typename... Ts>
auto min(Ts&&... args) {
  return // something
}

but there’s no builtin operator in C++ that takes two things and returns the lesser of the two. It’s a pretty easy function to write, but it doesn’t work with the fold syntax. It surprises me that folds would be proposed without a way to have them work with functions. They’re functional in origin, after all.

Here is my workaround

template <typename T>
struct MinWrapper {
  const T& obj;
};

template <typename T, typename U, typename V=std::common_type_t<T,U>>
MinWrapper<V> operator%(const MinWrapper<T>& lhs, const MinWrapper<U>& rhs) {
  return {lhs.obj < rhs.obj ? lhs.obj : rhs.obj};
}  

template <typename... Ts>
auto min(Ts&&... args) {
  return (MinWrapper<Ts>{args} % ...).obj;
}

Okay, the MinWrapper is just a struct that binds a reference. This just gives me a type to define an operator on. After that I define operator% just because modulus seems to be the thing people use when they’re doing something terrible. The mod operator compares the items in each wrapper, and returns a wrapper with their common type. This actually does work in clang’s current head believe it or not.

My question is: if I thought of (nearly) this monstrosity before I even had a compiler in front of me to run it with, is this going to be a pattern that emerges once folds make it into the language for real? Probably.

The solution would be to extend the syntax to allow not only operators, but binary functions as well, this would let me write

template <typename... Ts>
auto min(Ts&&... args) {
  auto sel_min =
    [](auto&& lhs, auto&& rhs) -> auto&& 
      {return lhs < rhs ? lhs : rhs;};
  return (args sel_min ...);
}

not the prettiest lambda either, but imo, much better than what I’m doing with the current syntax.

Thoughts on TAs biggest mistakes as a TA and as a Student.

I’m by no means an education expert and not the greatest TA, but I try to be pragmatic in how I approach students, grading, everything. I’ve noticed there are some pretty common faults that I’ve really hated and many TAs don’t seem to care about. A lot of this may appear to be just about grading, but the grading aspects hint at deeper issues. I’ve never seen a good TA who was bad at grading.

-5 for everything

It seems that there’s a huge issue with TAs not realizing that they can take off points in anything other than 5 point increments. You forgot to capitalize your last name in the submission file? -5. Your answer is 1000 words when I asked for a sentence? -5. You missed one specific detail on a test question but were otherwise right? -5. Your presentation consisted of you reading words off of your slides for 10 minutes? -5. You turned in your homework assignment 2 days late? eh, -5.

The motivation for the minor points is this: if the submissions instructions for an assignment say to submit “lastname_firstname_proj1.txt”, and 5 kids submit “proj1.txt”, another submits “Untitled.txt”, and the last gives me “smith_john.pdf”, there’s a bunch of stuff that I need to address here. It’s frustrating. I hate having to take off points for stupid submission mistakes but every TA you talk to will tell you “If you tell them they messed up, but don’t take off points, they’ll just do it again.” Trust me, we all wish we could just inform students of their mistakes on these things and that be the end of it, but 10/10 it’ll happen again unless there’s some point loss.

What I’ve found, however, is that it doesn’t matter too much how many points are taken off, as long as it’s not 0. In this regard, -5, -10, -1, are all equally effective. You might not expect it, but students do respond to -1. What I usually do is take off -1 for each error unrelated to the actual body of the project. -1 per error allows the losses to have little-to-no effect on the students actual grade, but it also lets me be fair. If one student gives me “smith_proj1.txt” and the other gives me “proj1.pdf” clearly these are not both -5 violations. The first student might get -1 for missing their first name (a fair deduction imo), and the second could lose -3 for no firstname, no lastname, and the wrong file type.

Not talking

I would think this should be obvious, but I’ve had TAs who are silent in lab sections. There’s a less-common problem with some international students whose spoken English skills are shaky, where they seem very hesitant to speak to students at all for fear of sounding bad. I understand where they’re coming from (je parle fran├žais comme une vache espagnol), but I don’t know how to address the problem other than asking, please try.

(Nearly) all or nothing grading

This isn’t as common, but I see a lot of TAs who don’t actually understand what partial credit means. If there’s a 20 point question, I know TAs who will automatically subtract 15 for it not being exactly the correct answer. Somehow they think this is much better than grading without partial credit, but when people get in this habit, scores are either complete, partially complete, 0. This sucks.

If you have 20 points to work with, use the full range. A lot of it has to be relative.

“Does everyone understand this?” / “Does anyone have any questions”

This is the hardest thing to get around, and honestly, I commonly can’t get around this problem myself. If you say anything like this to a class, 95% of the students who would have questions won’t ask them. The only time I will flat out ask students if they have any questions is if I’m teaching a new concept for the first time, and I want to get feedback early (I aim for at least every 5 minutes). Like I said, it’s hard to get around this problem, but here’s what I’ve found works really well instead:

Let’s say I’m conducting an exam review session and the students have had some sample problems to go over (last semester’s exam). If I start by saying “does anyone want me to go over problem 1?” or “does anyone have any questions on problem 1?” No one will say a word. I know because I’ve done this without thinking. If I say instead (or even right after) “Should I go over problem 1?” then EUREKA I’ve got half the class saying “yes!” and nodding. Something about me forfeiting control, asking the class for direction rather than their requests, gets them talking. Once students start talking they’re more likely to ask more solid questions. Even if people don’t speak, you can look around the room after asking a question like this and see them nodding or shaking their heads.

You can’t ask students to demonstrate what they don’t know.

Another, admittedly less effective means, is to invert the question. What I mean is that instead of asking “who doesn’t understand?” say “raise your hand if you understand this.” Now, this requires care because if you overuse this technique everyone will just raise their hand right away for everything. Even before that habit forms, they’ll raise their hands without really understanding. Hope is not lost though. You have to watch carefully after you say this, see how quickly their hands go up. Look for the students raising their hands slowly with their eyes wincing because they feel like they’re lying to blend in. You won’t get any questions out of this, but you can get a feel for how well the class is following and whether you need to go over something in more depth.

IMO, getting students to ask questions in a classroom is one of the most impressive skills a teacher can have. It’s really really hard.

Assigning grades the first time through

All of my grading is relative in the sense that I don’t start with a rubric. I always get through at least 50% of the exams/programs/homeworks/whatever before I get an idea of what point values certain things should be worth. If a lot of people have the same error, it might be a big mistake in my eyes, but it hints that there’s a greater misunderstanding in the class that needs addressing (unless it’s because of cheating of course).

Another problem is that I might see one submission and say “whoa this is definitely no higher than a 50,” but then later see one that’s better, but still isn’t even half-way complete. It’s really difficult to assign grades without having the class in general in mind.

Regrading, disregarding other students for the one in the room

All too often, students go to a TA to complain about their grades and are given points back when they don’t deserve a higher grade. Grading is imperfect, often a TA will accidentally deduct a point or two more than they should, it’s just the nature of the volume of things being dealt with and human error. Students will go through the points they lost with a fine-toothed comb and will be enraged easily when they feel deductions are unjust. I get it, and I have given points back (rarely) for a genuine screw-up on my part. I’m not too proud to admit I made a grading error.

Giving students points back to get them to stop complaining is a wide-spread problem. The people who suffer from this are the students who don’t complain about their grades. If half of the class complains to a crappy TA, and the TA gives them all 10+ points back, then suddenly the curve has jumped. The students who accepted responsibility for their mistakes now have lower grades and that’s not fair.

The problem is that there are other parts where I may have missed a deduction of a point or two somewhere else in the assignment or test. I’ll get students who aren’t emailing me over something obvious, but asking about a specific deduction in their work. I’ve always given students the option “if you’d like, I can regrade the whole thing, but that means the grade can go up or down, because I need to look at everything more closely.” I know others who do the same, students rarely take anyone up on this option. Grades typically go slightly lower in these cases if they change at all. It’s not a vengeful or prideful thing, but if the student wants their grade to be a 100% accurate reflection of their work, it’s gonna need a closer look. Closer looks generally reveal more mistakes.

super generic functions in c++14

Since C++14 lambdas now allow auto parameters, function objects can be pretty damn generic.

[](auto&& arg1, auto&& arg2) { return arg1 < arg2; }

Which got me thinking, what’s to stop me from defining all of my functions as lambdas. For example, this function which takes an iterable and prints it to stdout.

auto print_iterable = [](auto&& iterable) {
    for (auto&& e : iterable) {
        std::cout << e << '\n';
    }
};

int main() {
    std::vector<int> v{1,7,2,0};
    print_iterable(v);
}

This function can deduce its argument types and return type. There’s no return here of course, but there’s nothing stopping me from making this function more complex.

I haven’t thought too much about the implications of this, but it seems like I could get away without naming variable types or using templates. I’m not saying this a good idea, but it’s an interesting one.