Zelkova

  • based on Elm
  • a reasonable language for talking about time
  • signals are central

Signals

  • values transformed by pure functions
  • shared freely
  • synchronous and deterministic
  • think of signals representing values over time

Example

Folling the mouse

(defn steering
    [state mouse-pos]
    (assoc-in state [:flyter :position] mouse-pos))

(def app-signal
    [init-state]
    (z/reductions steering
                  init-state
                  mouse/position)) ; mouse/position is a signal

Steering

(defn steering
    [state mouse-pos]
    (let [flyer-pos (-> state :flyer :position)
          diff      (v/subtract mouse-pos flyer-pos)
          angle     (v/vector->radians diff)])
    (assoc-in state [:flyter :angle] angle))

Thrust

(defn inertia
    [state time-delta]
    (let [velocity (-> state :flyer :velocity)
          translation (v/multiply velocity time-delta 0.2)]
        (update-in state
            [:flyer :position]
            (partial v/add translation))))

(defn acceleration
    [state toggle]
    (if toggle
        (let [angle (-> state :flyer :angle)]
            (update-in state
                       [:flyer :velocity]
                       (fn [velocity]
                            (->> angle
                                (v/radians->vector)
                                (v/with-magnitude 0.2)
                                (v/add velocity)
                                (v/limit-magnitude 1)))))
    state))

(def app-signal
    [init-state]
    (let [time-deltas (time/fps 30)
          inputs      (->> (z/map vector 
                             time-deltas
                             keyboard/space
                             mouse/position)
                            (z/sample-on time-deltas))]
        (z/reductions (fn [state [time-delta spacebar? mouse-pos]]
                        (-> state
                            (inertia time-delta)
                            (acceleration spacebar?)
                            (steering mouse-pos)))
                      init-state
                      mouse/position))

Signal graph with Om