Contents
Lambda
-abstractions in C++
<iostream>
#include
[Announcements and descriptions of various software packages: refer to an appropriate document describing the package]
A paper and a talk presented at MacHack'96, The 11th Annual Conference for Leading Edge Developers.
| |
Summary | |
---|---|
composing safe, fast, and beautiful code in C++ and C. With an unusual slant on safety and performance, the paper discusses serial (vs. random) access to a collection, pointers vs. references, function classes, incorporation, transient object and classes. The paper introduces a number of efficient and powerful (yet obscure) patterns that make code safe and neat: precision-targeted pointers, nested functions, lazy objects, stealing of a body, natural iterators. A few good and bad code snippets show off the benefits of well-crafted code.
| |
Keywords | |
style, C++, iterator, lazy object, patterns | |
References | |
the paper [ The talk itself |
This technique requires some discipline in building a hierarchy, but in return guarantees that only the top-most class needs to be recompiled to take into account modifications made to a base class. The rest of the hierarchy (which may have been compiled to a set of separate libraries) may be used as it is.
See it for yourself, by building, "upgrading", and not recompiling sample "libraries" and targets in FBCSolution.tar.gz
. Check out the README file for more details.
June 27, 1997
Lambda
-abstractions in C++
The following code snippet speaks for itself. It is not a toy: the snippet is taken from an actual working code, a part of LinAlg, a Linear Algebra and Optimization classlib. This code is to verify that my implementation of the Brent root finder really works:
main(void) { printf("\n\n\t\tTesting Brent's root finder\n\n"); MakeTestFunction("cos(x)-x", Lambda((const double x),double,return cos(x)-x)) fcos; fcos.run(-1.0,3.0,0.739085133); MakeTestFunction("x^3 - 2*x - 5, from the Forsythe book", Lambda((const double x),double, return (sqr(x)-2)*x - 5))().run(2.0,3.0,2.0945514815); }This code is C++ standard-compliant. It compiles with gcc 2.8.1 and Visual C++ 5.0, and runs successfully. The last three lines of the code constitute a single expression, which creates a few "anonymous" objects, uses them and destroys afterwards. The first argument of the The Compare the above C++ code with an equivalent Scheme or e-lisp code | |
References | |
---|---|
The original source code [ Basic Linear Algebra and Optimization classlib Functional Style in C++: Closures, Late Binding, and Lambda Abstractions Lambda abstractions in C++ vs. Scheme |
There is a deep connection between C pointers on one hand, and Scheme
closures that respond to the messages The article referenced below demonstrates the connection by re-writing -- almost verbatim -- two C code fragments into Scheme. The C samples rely on pointers to let invoked functions share and mutate the variables local to the caller. For example, the following C fragment void modify(int ** p, int * py) { **p = *py * *py; *p = py; }becomes in Scheme (define (modify p py) ((*= (* p)) (* (* py) (* py))) ((*= p) py) )The technique can easily be generalized to permit pointer arithmetics as well. | |
References | |
---|---|
A USENET article
C pointer <-> Scheme closure: how to emulate & in Scheme [plain text file] A Usenet discussion
Why can't mutation be expressed mathematically?? |
<iostream>
iostream
facility: even a simple { cout << "Hello, world" << endl; }
code builds to a 64K+ executable.
This "iostream.h"
remedies the situation: this single file implements the bulk of the commonly used iostream functionality: That is, your code may contain something like
int d; double f; cout << "Enter two numbers" << endl; cin >> d >> f; cout << "The numbers are " << d << " and " << f << endl;and it will compile and work (with this
iostream.h
included), without any
changes in your code whatsoever.In much more detail, and in a much broader context, this code is described in the article Speaking in Iostreams-ese, published in C/C++ Users Journal, v.15, No. 5, May 1997, pp. 47-55.
The article which is referred to below talks about implementing keyword function arguments in several languages, including C++. A "dictionary" of C++ function's arguments can be constructed and searched
either at run time, or at compile time. The first solution is rather straightforward, and is mentioned rather briefly. The article then gives a complete example, which shows how construction of this "argument environment" and variable lookups can be
done entirely at compile time (see the middle of the article).
| |
References | |
---|---|
A USENET article explaining the technique [plain text file] Advanced i/o and Arithmetic Compression classlib Database as-a (OO)P language | |
From other archives | |
|
With no intent of starting a holy war, this paper lists several annoying C++ snags seem inherent in GUI C++ programming. I encountered these problems while wrapping XVT's platform-independent graphical toolkit in a C++ class library. It appears that the pitfalls and workaround kludges are fairly common, to many event- and callback-based systems. | |
References | |
---|---|
Paper and the presentation MacHack'95 brochure: Proc. MacHack'95, Expotech Inc., Grosse Pointe Park, MI, 1995, p.68-70. |
This article will show that any standard C++ compiler can deal with algebraic data types, perform term comprehension, and do type arithmetic. C++ compiler acts as a term-rewriting engine -- an interpreter of an untyped lambda-calculus over type identifiers. It is the compiler that finds an attempt to divide by zero and aborts the compilation -- before an executable is built, let alone run. We rely extensively on a partial template instantiations. It is a standard C++ feature that offers term comprehension and resolution of polymorphic types. The code in this article compiled with egcs-2.91.66 and g++ 2.95.2 compilers, on FreeBSD and Linux. This term re-writing engine is somewhat similar to expression
templates. There are however important differences. Expression
templates deal with terms that represent C++ expressions, e.g.,
Of course term-rewriting is not unique to C++. Some may argue that
other languages like Prolog, ML, or Erlang are more suitable to this
task. One has to keep in mind however that it is a C++ compiler
that does term-rewriting, of type expressions. Prolog and Scheme are
(dynamically-typed) languages with latent types; they do not apply in
the present context of static type inference. ML and Haskell do
sophisticated type inference; yet their type systems are
decidable. The ultimately static typing illustrated in this article
requires a more expressive type system. Beside C++, Cayenne and other
languages with dependent types can accomplish this task. Yet among
major contemporary programming languages only C++ appears to be able
to implement this ultimately static type system, and perform a very
general term rewriting of types at compile-time.
| |
Version | |
---|---|
The current version is 2.5, Aug 26, 2000. | |
References | |
A self-contained source code [ Type Arithmetics:
Computation based on the theory of types Functional Style in C++:
Closures, Late Binding, and Lambda Abstractions
|
#include
#include
different .h
files depending on a user-#define
d symbol, whose all possible values are
generally unknown at the time the code is written. Foo
for Bar
inside#include "Foo.h"
#define
d symbols
inside C strings (let alone parts of C strings, let alone #include
strings)
tournament-sched.c is the complete code mentioned in the article. This code -- running on a lowly PDP-11 clone -- had actually scheduled several tournaments, in real time.
Phil Troy has kindly pointed out that the scheduling problem is actually that of minimum-weight perfect matching in a general graph, for which efficient exact solutions are known. These solutions are the variations and improvements of Edmond's maximum-weight matching (aka blossom) algorithm. A good description can be found in ``Computing Minimum-Weight Perfect Matchings'' by William Cook, Andre' Rohe in INFORMS Journal on Computing, as well as in many books on combinatorial optimization.
This is a simple self-contained C code for a primitive
lynx-like formatting of HTML documents. The notable feature of the code is an
extensive use of a Finite State Machine with
byte-compiled actions and an option to
call a new state instead of just move to it, with an
opportunity to return to the caller-state. The code is
commented, even excessively so at the beginning.
| |
Version | |
---|---|
The current version is 1.1, May 1995. | |
References | |
The README file [plain text file] Complete source code
[ Advanced Finite Automaton as a
simple HTML formatter [Crazy Example] |
An illustration that such an abstract thing as a Finite State Machine can actually be used for mundane programming tasks.
This C code is a simple filter that prints out words from the input stream one by one, ignoring C-style comments. This sounds immensely trivial, until you actually try to write the code. Keep in mind that it should be able to handle empty lines, leading and trailing spaces, and such catches as
| |
Version | |
---|---|
The current version is 1.2, Oct 25, 2002. | |
References | |
Transparent incorporation of MacOS data structures into C++
(char *)
to a Pascal string in C++.
Plus a few other tricks how to encapsulate some QuickDraw data structures into classes and get native QuickDraw functions to take the embellished objects as their own (that's the magic)
Opaque bases/variables in a class: kludges, suggestions, and dreams
Pointers to class methods: taking, storing, using [Hack]
Cool way of iterating in a local context; nested functions in C++
(void *)
and fro,
without causing name conflicts, and without exposing private parts.
Local procedures and natural iterators in C++ [absurd example]
Virtual circling around in clouds (w/ C++ source)
C++ is good for writing OS kernels. Here's an example.
oleg-at-okmij.org