Giter Site home page Giter Site logo

helins / dsim.cljc Goto Github PK

View Code? Open in Web Editor NEW
118.0 7.0 5.0 342 KB

Idiomatic and purely functional discrete event-simulation

License: Mozilla Public License 2.0

Clojure 98.38% Shell 1.13% HTML 0.50%
clojure discrete-event-simulation animation transition

dsim.cljc's People

Contributors

helins avatar rainbow-bamboo avatar shark8me avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

dsim.cljc's Issues

Example of DSim + Quil integration

I've been trying to learn DSim to do animation using Quil. While going through the docs, there is a mention of Quil integration and how animation is one of the things DSim can help with:

When it comes to the huge topic of any form of animation, offline or online ones, the Clojure community is well endowed by having the Quil library

Now, as a newbie there is quite a lot to grok in Dsim, and the docs show useful code snippets. However, it still is quite difficult to jump from an abstract concept to a simple toy example (might just be that I'm also quite new to Clojure). It would be great if there was one or several runnable examples available.

In particular, it could be really helpful to see a simple example of how exactly an animation can be done using DSim + Quil combination. Even just giving an actual runnable example from the docs:

For instance, suppose this simple animation: an object moving on its X axis from pixel 200 to pixel 800 during 2000 milliseconds, in a universe where time units are indeed milliseconds. Using our infrastructure, it is a matter of barely a few lines of code to write a flow that can move anything from A to B.

The main issue I can see people like myself encountering is how DSim's ctx would play with Quil's fun-mode - and whether you even need it when one is using DSim.

P.S. I watched the wonderful referenced animation, and am really excited to try using DSim for animation!

P.P.S. I understand that asking for examples and docs improvement can be a big ask for maintainers. If someone was able to provide some samples for Quil, I'd be happy to turn them into a runnable example as a PR.

No working queue at the moment

Perhaps the documentation needs to be adjusted, or it's clearly outlined somewhere, but I'm not sure where yet, but I'm trying to follow along and run the readme examples and I keep getting this exception:

Execution error (ExceptionInfo) at dvlopt.dsim/e-update$safe-f (dsim.cljc:727).
No working queue at the moment

My REPL output of just following along with the readme:

(defn feeling-happy
  [ctx]
  (assoc-in ctx
    (conj (dsim/path ctx)
      :mood)
    :happy))
=> #'testing-dsim.core/feeling-happy
(def ctx
  (dsim/e-conj {}
    [42
       [:characters :little-prince]
       feeling-happy]))
Execution error (ExceptionInfo) at dvlopt.dsim/e-update$safe-f (dsim.cljc:727).
No working queue at the moment
(def ctx
  (dsim/e-conj {}
    [42
       [:characters :little-prince]
       feeling-happy]))
Execution error (ExceptionInfo) at dvlopt.dsim/e-update$safe-f (dsim.cljc:727).
No working queue at the moment
(dsim/ptime-engine)
=> #object[dvlopt.dsim$ptime_engine$run_ptime__3317 0x678a8f01 "dvlopt.dsim$ptime_engine$run_ptime__3317@678a8f01"]
(def ctx
  (dsim/e-conj {}
    [42
       [:characters :little-prince]
       feeling-happy]))
Execution error (ExceptionInfo) at dvlopt.dsim/e-update$safe-f (dsim.cljc:727).
No working queue at the moment
(def run
  (dsim/ptime-engine))
=> #'testing-dsim.core/run
(def ctx
  (run (dsim/e-conj {}
         [42
            [:me]
            feeling-happy])))
Execution error (ExceptionInfo) at dvlopt.dsim/e-update$safe-f (dsim.cljc:727).
No working queue at the moment
(def sun-activity
  (dsim/queue :sunrise
    (dsim/wq-delay (dsim/rank+ 12)
       :sunset)))
Execution error (NullPointerException) at dvlopt.dsim/eval3223$rank+ (dsim.cljc:331).
null

What am I missing? I tried running the engine to see if that helped, but no luck. Searching the source for queue didn't show me any obvious functions. The library looks interesting and perhaps when I have some time I'll try and dig into it, but it would be nicer if the examples ran =)...


Update

It would be helpful to know what a basic event should look like:

(defn watch-sunset [ctx] ctx)

You give an example of feeling-lucky, but it would be nice to explicitly say a basic event looks like the above, looking back over the REPL output I pasted above, I made sunrise and sunset into keywords because I didn't know what an event looked like.

The below was a copying error on my part, parinfer repositioned the parens without me realising ๐Ÿ˜Š... I've subsequently double checked a few others where errors were also caused by pasting/parinfer mistakes.

(def ctx
  (dsim/e-conj {}
    [42]
    [:characters :little-prince]
    feeling-happy))

Still trying to figure out how to "Run" the engine. Do I recursively call peek on it for example?

Nevermind, I need to repeatedly call run:

(def run
  (dsim/ptime-engine))

(run
  (dsim/e-conj {}
    [42]
    [:me]
    feeling-happy-2))

contains? not supported on type: clojure.lang.PersistentQueue

Not sure if this is a bug or if my mental model isn't quite right, but this code raises an exception when a and b are equal, which is surprising to me:

(ns core
  (:require [dvlopt.dsim :as dsim]))

(def run
  (dsim/historic (dsim/ptime-engine)))

(defn held-note
  [duration]
  (fn [ctx]
    (dsim/e-conj ctx (dsim/queue (fn [ctx] (assoc-in ctx (dsim/path ctx) "on"))
                                 (dsim/wq-delay (dsim/rank+ duration))
                                 (fn [ctx] (assoc-in ctx (dsim/path ctx) "off"))))))

(defn midi-note [duration]
  (fn [ctx] (dsim/e-conj ctx
                         (dsim/ranks ctx)
                         (conj (dsim/path ctx) :note)
                         (held-note duration))))

(def a 10)
(def b 10)

(map #(dissoc % :dvlopt.dsim/events)
     (run (-> {}
              (dsim/e-conj [0] nil (midi-note a))
              (dsim/e-conj [b] nil (midi-note 20)))))

evaluating the final form results in:

1. Caused by java.lang.IllegalArgumentException
   contains? not supported on type: clojure.lang.PersistentQueue

However, if a and b are different (e.g. (def a 9)), it runs without failing. Any idea what I'm doing wrong? I'm wondering if part of my confusion stems from a poor understanding of the difference between e-conj, e-assoc, etc. It isn't entirely clear from the readme when I should be using the various e- fns.

Suggestions for triggering events on state change?

Many systems (e.g. the keyboard visualizer linked from the readme) are well-represented as functions of state. However, other systems require you to trigger effects as events change, e.g. sending midi note on/note off events. I've spent a few hours playing around with dsim, with the hope that it might form a suitable foundation for a realtime audio event system. Unfortunately, I haven't figured out a good way to think about "triggering" things as state changes over time. Do you have any suggestions for how one might think about using dsim in this manner? Does this seem like a reasonable use-case for dsim?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.