The object oriented programming paradigm is widely used in science and engineering. Many open and commercial libraries are written in C++ and increasingly provide bindings to Python, which is much easier to learn, but still partly encourages the use of object oriented programming. However, scientific ideas are much more directly and meaningfully expressed in the purely functional programming paradigm. Here, we take a best practice example, CERNs Python binding for its ROOT library, designed to handle the enormous amounts of data generated by the worlds largest particle accelerator, and translate a simple segment of its tutorial into Clojure, a functional language from the Lisp family. The code examples demonstrate how a purely functional language straightforwardly expresses scientific ideas. Subsequently, we develop a compiled Lisp-C++ interoperation layer to access the ROOT library exclusively via functional code. To preserve the expressivity of the Lisp code, the type hints necessary for C++ code generation are stored in a separate file. The interop system presented here is a generic framework that, when provided with a suitable file of type hints, facilitates access to methods of arbitrary C++ libraries and platforms like real-time microcontrollers.
翻译:面向对象编程范式在科学和工程领域得到广泛应用。众多开源和商业库采用C++编写,并越来越多地提供Python绑定——后者更易学习,但仍在一定程度上鼓励面向对象编程的使用。然而,科学思想在纯函数式编程范式中能得到更直接、更有意义的表达。本文以CERN为其ROOT库编写的Python绑定(该库旨在处理世界最大粒子加速器产生的海量数据)这一最佳实践范例为对象,将其教程中的简单片段翻译为Clojure(一种Lisp家族的函数式语言)。代码示例展示了纯函数式语言如何直接表达科学思想。随后,我们开发了一个编译型Lisp-C++互操作层,通过纯函数式代码访问ROOT库。为保留Lisp代码的表达能力,C++代码生成所需的类型提示存储在单独文件中。本文提出的互操作框架是一个通用框架,当提供适当的类型提示文件时,可促进对任意C++库和平台(如实时微控制器)方法的访问。