Skip to content

Instantly share code, notes, and snippets.

View st98's full-sized avatar

st98 st98

View GitHub Profile
@st98
st98 / Array#longest.coffee
Created June 21, 2014 17:39
CoffeeScript で、配列の中で一番長い要素の長さを返す関数。参照: http://stackoverflow.com/a/6521513
# ref. http://stackoverflow.com/a/6521513
Array::longest = ->
return @reduce ((prev, curr) -> return if prev > curr.length then prev else curr.length), 0
@st98
st98 / String#ljust.coffee
Created June 21, 2014 16:57
CoffeeScript で String#ljust。
String::ljust = (len = @length, ch = ' ') ->
result = this
while result.length < len
result += ch
return result
@st98
st98 / randint.coffee
Created June 21, 2014 13:55
CoffeeScript で randint(max = 2)。
randint = (max = 2) ->
return Math.floor Math.random() * max
[s, i] = [[], 10]
s.push randint() while i-- > 0
console.log s.join ', '
@st98
st98 / initialize-array.c
Last active August 29, 2015 14:02
指示付き初期化子 (Designated Initializer) を使ったりして配列とか構造体を初期化してみた。 参照: http://d.hatena.ne.jp/iww/20090424/struct
#include <stdio.h>
int main(void) {
int a[5] = {
1, 0, 5
};
int b[5] = {
[0] = 1,
[2] = 5
@st98
st98 / regexp-literal.rb
Created June 6, 2014 23:28
Ruby の正規表現リテラルで遊んでみた。
p %r...source
@st98
st98 / Number#step.js
Created June 5, 2014 05:13
Number#step (Ruby の Numeric#step の劣化版)。
Number.prototype.step = function (limit, step, callback) {
var n = Number(this);
if (typeof step === 'function') {
callback = step;
step = 1;
}
while (n <= limit) {
callback(n);
@st98
st98 / abs.c
Last active August 29, 2015 14:01
絶対値。
#include <stdio.h>
int _abs(int n) {
return n & (1 << (sizeof(int) << 3 - 1)) ? ~n + 1 : n;
}
int main(void) {
printf("_abs(%d) = %d\n", -12, _abs(-12));
return 0;
}
@st98
st98 / Number#….js
Last active August 29, 2015 14:01
2..pow(4) === 16 とか 2.4.round() === 2 とかそういうの。
'abs acos asin atan atan2 ceil cos exp floor log pow round sin sqrt tan'.split(' ').forEach(function (name) {
var slice = [].slice;
Number.prototype[name] = function () {
return Math[name].apply(null, [this].concat(slice.call(arguments)));
};
});
@st98
st98 / Number#times.js
Created May 27, 2014 01:20
ちょっと曲芸な感じの Number#times。リソースの無駄遣い。
Number.prototype.times = function (fn) {
Array.apply(null, { length: this }).forEach(fn);
};
5..times(function (_, n) {
console.log(n);
});
@st98
st98 / test1.js
Last active August 29, 2015 14:01
コンソールで実行すると楽しい。
} if (1) { console.log('hoge'); // => 'hoge'