简体   繁体   English

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

[英]K/APL style programming in C++?

I'm writing code in C++, but I really like K/APL's array-oriented style. 我正在用C ++编写代码,但我真的很喜欢K / APL的面向数组的风格。

Does anyone know of a good set of operator overloading tricks / macros / ... to allow some K/APL -style programming in C++? 有没有人知道一组好的运算符重载技巧/宏/ ...允许在C ++中进行一些K / APL风格的编程?

Thanks! 谢谢!

For mathematics, Blitz++ is the biggest library for array programming. 对于数学, Blitz ++是最大的阵列编程库。 Here are some examples from the documentation: 以下是文档中的一些示例:

#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;

Blitz++ uses expression templates , a template metaprogramming technique similar to lazy evaluation. Blitz ++使用表达模板 ,一种类似于懒惰评估的模板元编程技术。 So the compiler-generated code doesn't use any unnecessary temporary variables, and should be as fast as hand-written loops. 因此编译器生成的代码不使用任何不必要的临时变量,并且应该与手写循环一样快。

Here's the equivalent k code, for the interested: 这是相同的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

I haven't looked specifically at K/APL, but depending on your viewpoint, you could argue that some of the operator overloads provided by std::valarray are vaguely similar to APL. 我没有特别关注K / APL,但根据你的观点,你可以说std::valarray提供的一些运算符重载与APL模糊地相似。 With its support for Universal Character names, you could (at least in theory) even provide APL-like names for some of them. 由于它支持通用字符名称,您(至少在理论上)甚至可以为其中一些提供类似APL的名称。

That still leaves some characteristics that aren't like APL at all, such as operators in C++ having precedence and associativity, which APL operators don't at all (at least if memory serves). 这仍然留下一些与APL完全不同的特性,例如C ++中具有优先级和关联性的运算符,APL运算符根本不具备这些特性(至少在内存服务时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM