“在程序运行时"编辑程序?为什么? [英] Editing programs "while they are running"? Why?

查看:25
本文介绍了“在程序运行时"编辑程序?为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近对 ​​Lisp 和 Lispy 语言有了更多的了解,我发现它们非常强大.

我一直在网上读到的一件事是,用 Lisp、Clojure 等编写的一个好处是,您可以在程序运行时"编辑程序.

也许我遗漏了一些东西,但有什么意义?

当然,它可能会节省几秒钟,但仅此而已吗?每当我对我的程序进行更改时,我都会停止它,然后重新启动它,而且这几十年来一直运行良好.

除了节省时间之外,肯定还有其他原因——是什么?

谁能给我一个好的案例研究,让我对这个功能垂涎三尺?:)

期待流口水!

解决方案

有一些非常酷的用例.一个例子是在 GUI 编程中 - 我在实时开发一个 GUI 应用程序时看到了这一点,因为它在我的 Emacs 旁边运行:我为一个新按钮添加了代码并点击Cc Cc"来编译那个单一的功能,按钮只是 出现在窗口中!不必关闭并重新打开应用程序.然后我开始调整小部件并操纵布局,打开的窗口会立即重新排列 - 按钮会四处移动,新的文本字段会弹出等等.只要我执行我所做的每一个小改动.

另一个例子是关于 Clojure OpenGL 库Penumbra"的精彩截屏视频,程序员实时创建了一个 3D 俄罗斯方块游戏.他从 emacs 旁边的一个空的 OpenGL 窗口开始.他定义了一个立方体对象 - C-M-x - 并且它在屏幕上.运行一个命令来旋转它,它立即开始旋转.运行一个循环,在不同位置定义另外 5 个立方体,它们出现 pop-pop-pop-pop-pop.这一切都是立即响应的,完整的 OpenGL 工具包就在那里.为您的立方体添加一个新的表面纹理,并立即看到它出现.它变成了一个可塑的 3d 世界 - 代码动态修改现有世界,而不是每次更改时关闭和重新打开 3d 画布.

Penumbra Livecoding Screencast - 下载高清版以获得​​最佳体验.

还有关于 Clojure 的音频库Overtone"的精彩演示/截屏视频.该库是一个合成器工具包,您可以在其中使用一组合成器函数来操纵声波.在演示过程中,开发人员编写了一些代码来开始播放音调.然后他花了 10 秒钟写了一个循环,将那个声音播放 10 次,但每次都提高频率,再次 C-M-x,你听到它,音符升高.在 20 分钟的实时时间内,他播放了一首歌曲.看起来很有趣.

泛音演示链接

其他用途例如:网络爬行/数据挖掘 - 开发和改进实时提取信息的算法,查看每一步返回的数据;机器人编程 - 在机器人活动时向机器人发送命令;面部/图像识别 - 使用 OpenCV 之类的库,在您开发代码时,可以立即更新库在图像/视频中识别的内容;数学作业(Clojure 有Incanter"用于统计);以及您想立即查看您的更改对您正在处理的数据产生什么影响的任何环境.

所以这是在您面前拥有 REPL 最有趣的方面.那些不是有形的、可塑的、互动的东西,开始成为.GUI 设计、3D 图形、程序化的声音制作、提取和转换数据,这些事情通常都是在一定距离内完成的.但是使用 Clojure(在某种程度上也使用其他动态语言),它变得非常有形和直接;编写代码后,您会立即看到每个更改,如果某些地方不起作用或没有得到您预期的结果,您只需更改您错过的内容并立即重新执行.

Clojure 非常倾向于这样做.疯狂的事情是您可以以相同的方式实时使用 Java 库 - 尽管 Java 本身不能!因此 Overtone 正在实时使用 Java 合成库,尽管您在 Java 中永远无法做到,Penumbra 正在使用 Java OpenGL 绑定等.这是因为 Rich Hickey 设计了 ​​Clojure,因此它可以即时编译为 JVM 字节码.这是一种了不起的语言 - Clojure 为编程变得如此有趣和高效做出了巨大贡献.

I've been getting more into Lisp and Lispy languages lately, and I'm finding them quite powerful.

One thing I've been reading all over the net is that a benefit of writing in Lisp, Clojure, etc, is that you can edit your program "while it's running".

Perhaps I'm missing something, but what is the point?

Sure, it might save a few seconds, but is that all? Whenever I make a change to my program I just stop it then start it again, and that has been working fine for decades.

There must be a reason other than just saving time -- what is it?

Can someone give me a good case study that will make me drool over this feature? :)

Looking forward to drooling!

解决方案

There are some extremely cool use cases. One example is in GUI programming - I saw this while developing a GUI app in real time as it was running beside my Emacs: I added code for a new button and hit "C-c C-c" to compile that single function, and the button just appeared in the window! Didn't have to close and reopen the app. Then I began tweaking widgets and manipulating the layout, and the open window would instantly rearrange itself - buttons would move around, new text fields would just pop into being, etc. as soon as I executed each little change I'd made.

Another example is an excellent screencast about the Clojure OpenGL library "Penumbra" where the programmer creates a 3D tetris game in real time. He starts with an empty OpenGL window next to his emacs. He defines a cube object - C-M-x - and it's on the screen. Runs a command to rotate it, immediately it starts spinning. Runs a loop defining 5 more cubes in different locations, pop-pop-pop-pop-pop they appear. It's all immediately responsive, the full OpenGL toolkit right there to play with. Add a new surface texture to your cube and see it appear right away. It becomes a malleable 3d world - the code dynamically modifies the existing world instead of closing and reopening the 3d canvas with every change.

Penumbra Livecoding Screencast - download HD version for best experience.

There is also a great presentation/screencast about the audio library "Overtone" for Clojure. The library is a synthesizer toolkit where you have a set of synth functions to manipulate the soundwave. During the presentation, the developer writes a bit of code that starts a tone playing. He then spends ten seconds writing a loop that plays that sound 10 times but makes the frequency higher each time, and again C-M-x and you hear it, notes ascending higher. Over the space of 20 minutes in real time he gets a song going. It looks like a ton of fun.

Overtone Presentation Link

Other uses would be, for example: Web crawling/data mining - develop and refine algorithms for extracting information in real time, seeing the data returned at each step; Robotics programming - send commands to a robot while it's live; Facial/image recognition - with a library like OpenCV watch your changes instantly update what the library recognizes in an image/video as you're developing the code; Mathematics work (Clojure has "Incanter" for statistics); and any environment where you want to immediately see what effect your changes have had on the data you're working with.

So that's the most fun aspect of having a REPL in front of you. Things that weren't tangible, malleable, interactive, start to be. GUI design, 3D graphics, programmatic sound production, extracting and transforming data, these things normally have been done at arm's length. But with Clojure (and to some extent with other dynamic languages too) it's made to be really tangible and immediate; you see each change as soon as you write the code, and if something doesn't work or you don't get back the result you expected, you just change what you missed and re-execute it immediately.

Clojure is very aligned towards doing this. The wild thing is you can use Java libraries in real-time the same way - despite the fact that Java itself can't! So Overtone is using a Java synth library in realtime despite the fact you never could in Java, Penumbra is using the Java OpenGL bindings, etc. This is because Rich Hickey designed Clojure so it could compile to JVM bytecode on the fly. It's an amazing language - Clojure has made a huge contribution to how incredibly fun and productive programming can be.

这篇关于“在程序运行时"编辑程序?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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