为什么在一个数据结构上运行100个功能比10个数据结构上的10个功能更好 [英] Why is it better to have 100 functions operate on one data structure than 10 functions on 10 data structure

查看:105
本文介绍了为什么在一个数据结构上运行100个功能比10个数据结构上的10个功能更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在很多地方看到这个引用:

I have seen this quoted in a lot of places:


最好在一个数据结构上运行100个函数10个数据结构的功能。 -Alan Perlis

"It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures." —Alan Perlis

但我从来没有看到它解释了为什么这应该是真的。是否只是想要从第一个方面推导其他9个数据结构,以避免重复数据?我觉得我缺少一些上下文。

But I have never seen it explained why this should be true. Is it just the idea that you should try to derive the other 9 data structures from the first to avoid duplicating the data? I feel like I'm missing some context.

推荐答案

引用来自Alan Perlis的编程方法,于1982年发布。

The quote is from Alan Perlis' Epigrams on Programming, which was published in 1982.

该引用的含义体现在 Lisp ,其中有许多操作和专门处理列表的功能 ,您可以通过列表和各种列表操作的功能完成许多工作,这使得它们比任何单一数据结构更强大。

The meaning of this quote is embodied well in Lisp, where there are multitudes of functions that operate and deal specifically with lists, and you could accomplish a lot just with lists and the assortment of functions that operate on lists, which makes them much more powerful than any single-purpose data structure.

<一个href =http://en.wikipedia.org/wiki/Lua_%28programming_language%29> Lua ,另一个例子是使用表来模拟类。为什么使用表来创建对象,而不是创建语言级的类和对象,如面向对象的语言呢?由于您的对象现在是一个表,您可以免费使用任何数量的定义在对象上的表的函数!更好的是,我们没有必要用类特定的语法来混淆语言,并且必须从表中重新定义我们需要的类的函数。

Lua, as another example, uses tables to simulate classes. Why use a table to make objects instead of creating language-level classes and objects like object-oriented languages do? Since your object is a table now, you can use any number of the functions defined for tables on your object, free of charge! Better yet, we didn't have to clutter the language with class-specific syntax and have to redefine functions from table that we want for our class.

什么Perlis说绝对是Lisp的一个突出的思维模式,一般来说,在功能编程中。您的一个数据结构上的100个功能可以以许多独特的方式组合在一起,因为它们都在相同的数据结构上运行,但是您也不能真正将10个功能混合在10个数据结构上,因为它们仅被定义为了处理其特定的数据结构。

What Perlis said is definitely a prominent mode of thought in Lisp and in functional programming in general. Those 100 functions on your one data structure can be composed together in lots of unique ways, since they all operate on the same data structure, but you can't really mix the 10 functions on 10 data structures as well, since they were defined only to work on their particular data structure.

这个更现代化和更简单的变化就是考虑抽象。如果我们使用Java进行编码,您是否希望在列表接口,或同一组十个功能,一次为ArrayList,一次为LinkedList,一次为....

A more modern and simpler variation of this is thinking in terms of abstractions. If we were coding in Java, would you rather write a 100 functions on the List interface, or the same set of ten functions, once for ArrayList, once for LinkedList, once for....

这篇关于为什么在一个数据结构上运行100个功能比10个数据结构上的10个功能更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆