用C++编写K/APL样式?

ano*_*non 9 c++ apl k

我正在用C++编写代码,但我真的很喜欢K/APL的面向数组的风格.

有没有人知道一组好的运算符重载技巧/宏/ ...允许在C++中进行一些K/APL风格的编程?

谢谢!

chr*_*ock 5

对于数学,Blitz ++是最大的阵列编程库.以下是文档中的一些示例:

#include <blitz/array.h>

using namespace blitz;

Array<int, 1> x(10);     // one-dimensional array of 10 int's
firstIndex i;            // place holder index
x = 10 * i;              // x == 0, 10, 20, 30...
x = 10 * tensor::i;      // a short form of the above two expressions

// another example, with array-level assignments and arithmetic
Array<int, 1> a(4), b(4), c(4);
a = 1, 2, 3, 4;
b = 5, 6, 7, 8;
c = a + b;
Run Code Online (Sandbox Code Playgroud)

Blitz ++使用表达模板,一种类似于懒惰评估的模板元编程技术.因此编译器生成的代码不使用任何不必要的临时变量,并且应该与手写循环一样快.

这是相同的k代码,感兴趣的:

  x:10*!10
  x
0 10 20 30 40 50 60 70 80 90

  a:1 2 3 4
  b:5 6 7 8
  c:a+b
  c
6 8 10 12
Run Code Online (Sandbox Code Playgroud)


Jer*_*fin 2

我没有专门研究 K/APL,但根据您的观点,您可能会认为 提供的一些运算符重载std::valarray与 APL 有点相似。凭借其对通用字符名称的支持,您甚至可以(至少在理论上)为其中一些提供类似 APL 的名称。

这仍然留下了一些与 APL 完全不同的特征,例如 C++ 中的运算符具有优先级和结合性,而 APL 运算符根本没有(至少如果没记错的话)。