Nice connection. The satire still applies.
Which programming languages are Lagadonian?
"trying to change human excretion back into food and trying to extract sunbeams out of cucumbers or teaching mathematics to pupils by writing propositions on wafers and consuming them with "cephalick tincture".[2] The Academy is home to The Engine, a fictitious device resembling a modern computer. "Nice connection. The satire still applies.
Which programming languages are Lagadonian?
“... Every one knew how laborious the usual method is of attaining to arts and sciences; whereas, by his contrivance, the most ignorant person, at a reasonable charge, and with a little bodily labour, might write books in philosophy, poetry, politics, laws, mathematics, and theology, without the least assistance from genius or study.” He then led me to the frame, about the sides, whereof all his pupils stood in ranks. It was twenty feet square, placed in the middle of the room. The superfices was composed of several bits of wood, about the bigness of a die, but some larger than others. They were all linked together by slender wires. These bits of wood were covered, on every square, with paper pasted on them; and on these papers were written all the words of their language, in their several moods, tenses, and declensions; but without any order. The professor then desired me “to observe; for he was going to set his engine at work.” The pupils, at his command, took each of them hold of an iron handle, whereof there were forty fixed round the edges of the frame; and giving them a sudden turn, the whole disposition of the words was entirely changed. He then commanded six-and-thirty of the lads, to read the several lines softly, as they appeared upon the frame; and where they found three or four words together that might make part of a sentence, they dictated to the four remaining boys, who were scribes. This work was repeated three or four times, and at every turn, the engine was so contrived, that the words shifted into new places, as the square bits of wood moved upside down."[2]
Thank you!!!Video C++17/C++20
https://www.datasim.nl/blogs/30/videos
Thank you!!!Video C++17/C++20
https://www.datasim.nl/blogs/30/videos
I am looking to this. Thank you @Cuchullain, nice to have a C++ refresher !
Do you want to try my quizzes and exercises? :-0I am looking to this. Thank you @Cuchullain, nice to have a C++ refresher !
// TestGenericVisitorProtean.cpp
// DJD 2022-10-9 version 0.9 #6
// (C) Datasim Education BV 2022
/*
A really modern implementation of a generic Visitor pattern that is an improvement on other approaches in C++:
1. std::visit() and std::variant()
2. Traditional Acyclic Visitor using subtype polymorphism, dynamic casting and
possibly multiple inheritance.
The proposed solution is more maintainable than solutions 1 and 2 while at the same time having better performance and reliability properties than solution 2. Our solution uses a combination of
template methods and C++ Concepts.
We take the well-known example of 2d shapes (Point, Line, Circle) that can be visited in different ways.
The two visitors represent new functionality for drawing and scaling these shapes.
We note that we do not have a class hierarchy nor virtual functions. All behaviour is compile-time driven as it were.
C++ Concepts is a game changer.
This code contains:
1. Visitor pattern based on C++20 Concepts (provide-requires contracts).
2. Multimethods (the Holy Grail) .. what C++ tried for 30 years.
3. Multimethods with variadic parameters, a) object with multiple visitors,
b) a visitor with multiple objects.
WE GIVE A STRIPPED DOWN VERSION TO FIT ON PAGE
More later.
*/
#include <iostream>
// Visitor with templates,V2
struct Point;
// Using C++20 Concepts to define contracts/protocols
// 1. The baseline contract between GOF Context and Visitor
template <typename Visitor, typename Context>
concept IVisitor = requires (Visitor & v, Context & c)
{
v.visit(c);
};
template <typename Visitor, typename Context>
concept IAccept = requires (Visitor & v, Context & c)
{
c.accept(v);
};
template <typename Visitor, typename Context>
concept IAcceptVisitor = IVisitor<Visitor, Context> && IAccept<Visitor, Context>;
// Specialisations of Concept interfaces (from 2 to 1 template parameter)
template <typename Visitor>
concept IPointVisitor = IVisitor<Visitor, Point>;
/* End of Protocol definitions*/
struct Point
{
double x;
double y;
Point() : x(0.0), y(0.0) {}
Point(double X, double Y) : x(X), y(Y) {}
template <typename T> requires IPointVisitor<T>
void accept(T& t)
{
t.visit(*this);
}
};
// Specific visitors (Draw and Scale)
struct Draw
{
void visit(Point& p)
{
std::cout << "("<< p.x << ", " << p.y << ")\n";
}
};
struct Scale
{
double fac;
Scale(double factor) : fac(factor) {}
void visit(Point& p)
{
p.x *= fac;
p.y *= fac;
}
};
// Multimethods, the Holy Grail of C++
template <typename Visitor, typename Context>
void multimethod(Visitor& v, Context& c) requires IAcceptVisitor<Visitor, Context>
{
v.visit(c);
c.accept(v);
}
// Command multipattern ... a list of objects on a single visitor
template <typename T, typename Visitor>
void command(Visitor v, T& c)
{
c.accept(v);
}
template <typename T, typename Visitor, typename ... Types>
void command(Visitor v , T& arg, Types& ... args)
{ // 1 visitor, multiple contexts
command(v, arg);
command(v, args...);
}
// Command multipattern ... a list of visitors on a single object
template <typename T, typename Visitor>
void command2(T& c, Visitor v)
{
// c.accept(v);
v.visit(c);
}
template <typename T, typename Visitor, typename ... Types>
void command2(T& arg, Visitor& v, Types& ... args)
{ // 1 context, multiple visitors
command2(arg, v);
command2(arg, args...);
}
int main()
{
// Contract with C++ 20 Concepts
{
std::cout << "Contracts, points and lines\n";
Point p1(2.0, -3.0);
Draw draw;
Scale mod(0.5);
p1.accept(mod);
p1.accept(draw);
mod.visit(p1);
draw.visit(p1);
p1.accept(mod);
draw.visit(p1);
}
{
// multimethods
std::cout << "Multimethod\n";
Point p(2.0, 4.0); Point p2(20.0, 41.0);
Point p3(-2.0, -4.0); Point p4(21.0, 42.0);
Draw draw;
Scale mod(0.5);
// Magic
multimethod(draw, p);
multimethod(mod, p);
multimethod(draw, p);
std::cout << "variadics\n";
command(draw, p);
// Multimethods with variadic parameters
command(draw, p, p2, p3, p4);
// Object with multiple visitors
command2(p, draw, mod, draw, mod, draw, mod, draw);
command2(p2, draw, mod, draw);
}
}
Umm - synaptic connections at work: “plastics - the Great Pacific Garbage Patch!”I declare that this work was composed by myself, that the work contained herein is my own except where explicitly stated otherwise in the text..
A student exercise: port GOF patterns to C++ Concepts.
- I just want to say one word to you.
- Just one word.
- Yes, sir.
- Are you listening?
- Yes, I am.
- Concepts.
- Exactly how do you mean?
- There's a great future in C++ Concepts.
- Think about it.
- Will you think about it?