在功能编程中如何处理鼠标事件? [英] How to do mouse events handling in functional-programming?

查看:68
本文介绍了在功能编程中如何处理鼠标事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,我必须实现一个自定义的拖放功能,例如降低. IP(命令式编程)样式代码可能如下所示:

Sometimes I have to implement a feature like customized drag & drop. The IP(imperative programming) style code may goes like this:

bool mouse_down = false;
Vec2 mouse_pos;
void on_mouse_down() {
  mouse_down = true;
  mouse_pos = cursor_pos();
}
void on_mouse_move() {
  if(mouse_down) {
    Vec2f c = cursor_pos();
    Vec2f d = c - mouse_pos;
    // dragging. make objects tracing the cursor.
    // ...
  }
}
void on_mouse_up() {
  mouse_down = false;
  // dropped
  // ...
}


我知道这很好,但是我根本不喜欢冗余变量和类似这样的分离过程.上面的代码只是最简单的示例.如果我向整个系统添加更多功能和限制,例如左,右,中间,滚轮,双击,光标在不同对象上的不同操作,则变量帐户将会爆炸……在IP中,这也许是不可避免的;我的问题是,在FP(函数式编程)中是否有更好的方法来处理此类问题,它的外观如何,如何保持一致性?无论使用哪种编程语言,Lisp,Scheme,F#,Ruby等,或者采用IP进行此操作的更好方法,任何想法和建议都会受到赞赏?
您可以将示例视为类似于C代码.


I know this works fine, but I donnot like redundant variables and separated procedures for something like this at all. The code above is just a simplest sample. Variables account would explode if I added more functions and limitations to the entire system, like left, right, middle, wheel, double clicking, different operations while cursor going over different objects... In IP maybe it''s unavoidable; my question is, is there a better way to deal with this kinda thing in FP(functional programming) and how it looks like, how to make concinnity? Any ideas and suggestions are appreciated, no matter whatever programming languages, Lisp, Scheme, F#, Ruby, etc. or some better way to do this in IP?
Edited: You can treat the sample as something like C code.

推荐答案

请参见
See this post[^]) at ''the competitors'' website for some insights.


这篇关于在功能编程中如何处理鼠标事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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