« Earlier3 items total Later »

need help

Can any one have any idea ..........about mouse programming.....in MATLAB.................


In 3 Dimensions.......means 3D point generation/line/curve etc.......using mouse.

Reactive Programming In Ruby

In reactive programming a set of variables with dependencies are created. When a variable is modified it notifies all other variables that it depends on to update their values.

The below simple class Reactive implements one such solution to this problem.

a = react 10
b = react 20
c = react(a,b){ |x,y| x + y }
d = react(b,c){ |x,y| x + y }


vars = {:a=>a,:b=>b,:c=>c,:d=>d}
vk = [:a, :b, :c, :d]
report = proc do
    vk.each do |k|
       puts "#{k} #{vars[k].value}"
    end
end

puts " Initial value of variables "
report[]

a.value = 40
puts " New value of variables "
report[]


generates

 Initial value of variables
a 10
b 20
c 30
d 50
 New value of variables
a 40
b 20
c 60
d 50


Implementation


class Reactive

   # On change of value dependants are notified
   # of updates
   def value=(val)
      @value=val
      @depends.each do |d|
         d.notify
      end
   end

   # Add d as a listener
   def notify_me d
      @depends << d
   end

   # Get the cached value unless the
   # dirty flag has been set then
   # recalc the value from the block
   def value
      if @block && @dirty
         argv = []
         @args.each do |a|
            if a.respond_to? :value
               argv << a.value
            else
               argv << a
            end
         end
         @value = @block.call *argv
      end
      @dirty=false
      @value
   end

   # Notify this object that at
   # least one dependant has changed.
   def notify
      @dirty=true
   end

   # Init the class with the dependant
   # reative variables and a block to
   # evaluate to compute the value
   # of this object.
   def initialize *args, &block
      @depends = []
      if block_given?
         @args = args
         @block = block
         @args.each do |a|
            a.notify_me self
         end
      else
         # This is a literal
         @value = *args
      end
      @dirty=true
   end

end

def react *args, &block
   Reactive.new *args, &block
end

LP.org is another Matlab free code repository

feel free to go to:
http://en.literateprograms.org/Category:Programming_language:Matlab

to comment and participate, it's a wikipedia-like literate-programming oriented web site,

garzol

« Earlier3 items total Later »




Sponsored by

Sole Central

Your one stop shop for Birkenstock and Crocs shoes and sandles.