Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Sometimes I have to implement a feature like customized drag & drop. The IP(imperative programming) style code may goes like this:
C++
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
  // ...
}

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.
Posted
Updated 17-Jan-12 0:28am
v2
Comments
Sergey Alexandrovich Kryukov 16-Jan-12 22:12pm    
Is it pseudo-code or what? What's the language of the sample. Who told you that you need to do "separated procedures"? You can use anonymous methods. What is the signs of FP in your question? Why do you think it's a problem in FP? You have shown a very non-functional sample, that's it.
--SA
paladin_t 17-Jan-12 6:26am    
The sample is NOT FP, it's IP (languages without anonymous method/lambda support, you can treat it as C code simply). My question is how to do this in FP

1 solution

See this post[^]) at 'the competitors' website for some insights.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 17-Jan-12 7:27am    
A great find with good links! 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900