Parallel Programming with OmniThreadLibrary
printed book: 25% off (use coupon BLACKFRIDAY25)
CompareValue function is incredibly practical when you are writing comparers (functions that determine how some data structure is ordered). System.Math and System.StrUtils define a bunch of functions that can be used to compare integers, doubles, strings … There’s, however, no CompareValue for booleans.
A CompareValue function compares two parameters, traditionally named left and right, and returns 0 if they are the same, –1 if left is smaller and 1 if right is smaller.
If we use the usual ordering of false < true, we can write the missing function as follows:
function CompareValue(left, right: boolean): integer; overload; begin if left < right then Result := -1 else if left > right then Result := 1 else Result := 0; end;
Your task for today – if you choose to accept it – is: Write this function without any if statements.
On September 26th I’ll talk about RAD Studio Rio in Ljubljana. We’ll discuss 10.3 a bit and 10.3.1/10.3.2 updates in more detail. We’ll also look into the future to see what 10.4 might bring.
This will also be a good opportunity to see my latest book, Design patterns with Delphi, or get your own copy signed.
Participation is free, but you should register here so we can prepare enough food for everyone.
Writeln ( True); Magic;Writeln ( True);
TRUE FALSE
case a of
0: Writeln(0);
else
Writeln('else');
Writeln(a);
end;
This code indeed compiles and works exactly as the following fragment.case a of
0: Writeln(0);
else begin
Writeln('else');
Writeln(a);
end;
end;
I personally would never drop begin/end inside a case/else statement, but at least someone must disagree. I found such example in a very (VERY!) old code (it was written for Delphi 2) and I was quite surprised that it compiles at all.case a of
0: Writeln(0);
else
Writeln('else');
Writeln(a);
end;
I guess this makes more sense (but just an itsy bitsy teenie weenie bit more).Slides and code for the Spring4D conference are now published on the conference page.
Slides and code for my two sessions – Design patterns with Spring4D and Interception and dynamic proxy – are also available on my presentations page.
TLThere were, however, other interesting results that my simple benchmark pointed out. More on that in a moment, but first…; DR Yes, allocating from the top is slower. No, the difference is not big and in most cases you’ll not even notice it.
While writing Design Patterns with Delphi, I spent quite some time researching existing literature on design patterns implementation in Delphi, design patterns in other languages, other types of patterns, design principles and so on …
In case you would like to dig deeper than the book takes you, here is my reading list.
“Bee there Orr Bee A Rectangular Thyng
”
- The Band With Rocks In *
Slovenian company BASS is looking for a Delphi developer (on-site in Celje, Slovenia).
(I’m not affiliated with them; they just asked me to spread a word around. If you have any questions, contact them directly.)
Hurrah, hurray, my third book is here! It’s called Hands-On Design Patterns with Delphi and (just like my first book) I wrote it for Packt Publishing. (The second book was self-published and I expect the fourth one to be, too.)
As the name says, “Design Patterns with Delphi” deals with design patterns. It is a bit different from most of design pattern books and websites you will find on the Internet. Case in point A: There are no UML diagrams. I don‘t speak UML. Tried to learn it few times but for some reason the whole concept doesn‘t agree with me. If you like diagrams, don’t fear though. Any book on design patterns - and most websites covering that topic - will gladly show how any design pattern can be diagrammed. That, however, is not important and should not govern your decision to buy the book.
More important is case in point B: This book speaks Delphi. All the examples are written in Delphi and language features are used to the full. I also covered few less known Delphi idioms in separate sections. You’ll still be able to follow the discussion even though you may program in a different Pascal dialect.
There’s also case in point C: Examples make sense. I deeply dislike classical design pattern examples of the “And then we want to write this program for different toolkits and it should also be able to draw circles, not only squares” kind. Euch! I tried to find a good example for each design pattern. Admittedly, I ended with few examples that draw triangles and squares on screen (mostly because some patterns were designed specifically for solving such problems), but most of them are of a more practical nature.
This book covers all three classical design pattern categories - Creational patterns, Structural patterns, and Behavioral patterns. It also discusses patterns from the newer Concurrency patterns category. At the end I threw in some borderline-pattern(ish) topics and ended with a discussion of few patterns that cannot be strictly classified as “design” patterns.
In this book you’ll find:
I hope you will like this book and learn a lot from it. I know I did during the nine months I spent writing it. And if you find any bug in the code, let me know so I can correct it in the second release!
New OmniThreadLibrary is out! Get it while it’s hot!
Version 3.07.7 is mostly a bugfix release. It fixes a stupid mistake introduced in version 3.07.6 plus some other minor bugs.
You can get it now on git, download the ZIP archive, install it with Delphinus or with GetIt.
For more information, visit OmniThreadLibrary home page or write your question on the forum.