Skip to content

Instantly share code, notes, and snippets.

@mohayonao
mohayonao / tiny-promise.js
Created May 17, 2013 22:46
小さいPromise
var Deferred = (function() {
function Deferred() {
var _pendings = [];
var _isResolved = false;
var _resolveValue;
this.resolve = function(resolveValue) {
if (!_isResolved) {
_isResolved = true;
_resolveValue = resolveValue;
_pendings.forEach(function(callback) {
@mohayonao
mohayonao / overloadable.coffee
Last active December 17, 2015 22:29
CoffeeScriptで演算子のオーバーロードっぽいやつ
overloadable = do ->
operator = (op, b)->
a = @value()
b = b.value?() ? b
fn = switch op
when '+' then (a, b)-> a + b
when '-' then (a, b)-> a - b
when '*' then (a, b)-> a * b
when '/' then (a, b)-> a / b
if Array.isArray(a)
SinOsc.ar(440)
// => SinOsc.ar(440)
SinOsc.ar([440, 880])
// => [ SinOsc.ar(440), SinOsc.ar(880) ]
SinOsc.ar([440, 880], mul:(1..4), add:[0, 10])
// => [ SinOsc.ar(440, mul:1, add:0), SinOsc.ar(880, mul:2, add:10), SinOsc.ar(440, mul:3, add:0), SinOsc.ar(880, mul:4, add:10) ]
SinOsc.ar(440, mul:0.5)
@mohayonao
mohayonao / defaults_for.js
Created July 6, 2013 23:16
デフォルト引数をつける
var isDictionary = function(obj) {
return obj && obj.constructor === Object;
};
var args_resolution = function(keys, vals, given) {
var dict, args = vals.slice();
if (isDictionary(given[given.length - 1])) {
dict = given.pop();
for (var key in dict) {
var index = keys.indexOf(key);
@mohayonao
mohayonao / fn.js
Last active December 19, 2015 10:59
デフォルト引数をつける 2
var fn = (function() {
function Fn(func) {
this.func = func;
this.def = [];
}
Fn.prototype.defaults = function(def) {
this.def = def;
return this;
};
Fn.prototype.build = function() {
grunt.registerTask("swf", function() {
var done = this.async();
var child = grunt.util.spawn({
cmd : "mxmlc",
args: ["-o", "coffee-collider-fallback.swf", "./src/fallback.as"]
}, function(err, result) {
if (result.code !== 0) {
grunt.fail.fatal("Compile failed. See the above error message.");
}
done();
module.exports = function(grunt) {
"use strict";
grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.loadNpmTasks("grunt-este-watch");
var testFailed = [];
grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
s = Synth.def (freq=440)->
Out.ar(0, SinOsc.ar([freq, freq*1.05]))
.play()
Task.loop ->
freq = Math.random() * 880 + 220
for i in [2, 3, 4, 3]
s.set freq:freq * i
@wait 250
s.set freq:freq * i * 2

Literate CoffeeScript

  • list
  • list
  • list
    • sublist
    • sublist

header2

s = Synth.def (freq)->
Out.ar(0, SinOsc.ar(freq))
.play()
Task.loop ->
s.set freq:440
@wait 1000
t = Task.each [1,2,3], (i)->
s.set freq:(660 * i)