|
// Generated by LiveScript 1.2.0 |
|
(function(){ |
|
var ref$, map, zipAll, sortBy, split, id, take, alphabet, width, height, svg, update, shuffle; |
|
ref$ = require('prelude-ls'), map = ref$.map, zipAll = ref$.zipAll, sortBy = ref$.sortBy, split = ref$.split, id = ref$.id, take = ref$.take; |
|
alphabet = split('', 'abcdefghijklmnopqrstuvwxyz'); |
|
width = 960; |
|
height = 500; |
|
svg = d3.select('body').append('svg').attr('width', width).attr('height', height).append('g').attr('transform', "translate(32, " + height / 2 + ")"); |
|
update = function(data){ |
|
var x$, text, y$, z$; |
|
x$ = text = svg.selectAll('text').data(data, id); |
|
x$.attr('class', 'update').transition().duration(750).attr('x', function(_, i){ |
|
return i * 32; |
|
}); |
|
y$ = x$.enter().append('text').attr('class', 'enter').attr('dy', '.35em').attr('y', -60).attr('x', function(_, i){ |
|
return i * 32; |
|
}).style('fill-opacity', 1e-6).text(id).transition(); |
|
y$.duration(750).attr('y', 0).style('fill-opacity', 1); |
|
z$ = x$.exit().attr('class', 'exit').transition(); |
|
z$.duration(750).attr('y', 60).style('fill-opacity', 1e-6).remove(); |
|
return x$; |
|
}; |
|
update(alphabet); |
|
setInterval(function(){ |
|
return function(){ |
|
return update(function(){ |
|
return take(Math.floor.apply(this, arguments)); |
|
}(Math.random() * (alphabet.length + 1))(shuffle.apply(this, arguments))); |
|
}(alphabet); |
|
}, 1500); |
|
shuffle = function(arr){ |
|
var length, _; |
|
length = arr.length; |
|
return map(function(arg$){ |
|
var v, _; |
|
v = arg$[0], _ = arg$[1]; |
|
return v; |
|
})( |
|
sortBy(function(arg$){ |
|
var _, r; |
|
_ = arg$[0], r = arg$[1]; |
|
return r; |
|
})( |
|
zipAll(arr, (function(){ |
|
var i$, to$, results$ = []; |
|
for (i$ = 0, to$ = length; i$ <= to$; ++i$) { |
|
_ = i$; |
|
results$.push(Math.random()); |
|
} |
|
return results$; |
|
}())))); |
|
}; |
|
}).call(this); |
I like your simple shuffle. Note it can be written even more compactly as:
Now I think about it your code doesn't actually do what is claimed in the comments. The letters are not kept in alphabetical order. To do so quite simply you can use:
or if like me you don't like parameters hanging about after call-backs: