作为 C 程序员,使用闭包(或块)进行设计的方法
我的 2 个主要开发环境是 C 和 Objective-C。借助 Clang 和 LLVM,使用闭包已经或正在成为完全的现实,称为“块”……但它们仍然是闭包。我不关心技术“如何”
我想知道的是,设计使用闭包的软件的最佳方法是什么?
我知道您可以使用它们对事物进行很好的排序,但是还存在其他可能性吗?我已经看到了简化程序结构的方法,但是这个主题很容易搜索。我从未见过任何详尽的列表或好的资源。
悬赏:悬赏答案将给出 C 和 Objective-C 中块的使用的完整列表和具体示例
My 2 main development environments are C, and Objective-C. With Clang and LLVM, using closures has become or is becoming a complete reality, called Blocks.. but they're still closures. I'm not concerned with the technical "how to"
What I want to know is, what are the best ways to design software to use Closures?
I know that you can do nice sorting things using them, but what other possibilities exist? I've seen ways to simplify program structure, but the topic is very heard to search on. I've never seen any exhaustive list or a good resource.
Bounty placed: a bounty answer will give a thorough list and specific examples for uses of blocks in C and Objective-C
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 Objective-C 或 C 中的块:
有关 Closure/Blocks/Lambdas 的更多信息:
For Blocks in Objective-C or C:
More on Closure/Blocks/Lambdas :
可能不是您正在寻找的答案,但它可能会有所帮助...
闭包并不新鲜,它们自编程诞生以来就一直存在(Lisp、Algol-60)。了解它们如何使用的最好方法是查看它们一直存在的成熟语言,看看人们如何使用它们。
函数式语言和闭包是同义词,通常被称为“高阶”编程——函数(方法)将其他函数作为参数,并且可以动态创建函数(块)。所以看一本关于 Haskell 编程的书不是一个坏选择,或者也许是 F#。
如果这看起来太激进,您可以看看 Ada 95,一种具有内联函数的“传统”语言,尽管它是一种(有利可图的)小众语言,并且这些功能可能没有广泛使用。
如果您想要一些相对较新的东西,请查看 C#,其中在 v2 中添加了内联函数。 C# 的内联函数与 Obj-C 的内联函数有许多相同的陷阱,人们带着不知情的喜悦而陷入其中 - 因此,当它们的使用更加成熟时,请查找最近的 C# 书籍。
最后,不要过度使用块。它们是一个非常强大的工具,适用时可以产生非常好的解决方案。然而,如果使用不当……这里就是龙;-)
Probably not the answer you're looking for, but it may help...
Closures are not new, they've been around since the dawn of programming (Lisp, Algol-60). The best way to learn about how they are used is to look at a mature language where they've been around forever and see how people use them.
Functional languages and closures are synonymous, usually referred to as "higher order" programming - where functions (methods) take other functions as arguments and functions can be created on the fly (blocks). So looking at a book on programming in Haskell is not a bad choice, or maybe F#.
If that seems too radical you could look at Ada 95, a "conventional" language with inline functions, though it is a (profitable) niche language and the features are probably not widely used.
If you want something relatively new look at C# where inline functions were added in v2. C#'s inline functions have many of the same pitfalls as Obj-C ones, and people fell into them with uninformed glee - so look for a recent C# book when their use was more mature.
And in closing, don't overuse blocks. They are a very powerful tool, and when applicable can produce very good solutions. When used inappropriately however... here be dragons ;-)
我没有使用过 Objective-C,但在 C、C++、Object Pascal、Delphi、Javascript 中使用过闭包。 “闭包”是一个非常模糊的术语,它在代码中的实现不同于一种编程语言与另一种编程语言,也不同于数学术语。
这就是为什么我们有“块”、“回调”、“函数指针”、“方法引用”等等。
基本上,“闭包”是用作数据的函数。程序员将函数引用存储在变量或参数中,而不是立即调用,而是在需要时使用。由于我们可以拥有“函数变量”,因此同一个变量可以用于执行不同的闭包。
既然你提到了“Objective C”,它也是面向对象的,那么如果你想对方法使用闭包或对全局函数使用闭包,那么使用“闭包”的方式是不同的。
以“Plain C”为例:
I haven't work with Objective-C, but have use closures in C, C++, Object Pascal, Delphi, Javascript. "Closure" is a very ambigous term, and it's implementation in code, differs from one programming language from another, as well as a math term.
That's why we have "blocks", "callbacks", "function pointers", "method references", blah.
Basically, a "closure" is a function that is used as a data. Programmers store the function reference in a variable or parameter, and instead of been call inmediatly, is used when required. Since we can have "function variables", the same variable can be used to execute different closures.
Since you mention "Objective C", who is also Object Oriented, the way to use "closure" is different if you want to use closures for methods or closures for global functions.
Example with "Plain C":