Skip to content

Instantly share code, notes, and snippets.

View st98's full-sized avatar

st98 st98

View GitHub Profile
@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#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 / regexp-literal.rb
Created June 6, 2014 23:28
Ruby の正規表現リテラルで遊んでみた。
p %r...source
@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 / 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 / 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 / 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 / n文字ごとに文字列を挿入するヤツ.js
Last active August 29, 2015 14:02
n 文字ごとに文字列を挿入するヤツ。
String.prototype.n文字ごとに文字列を挿入する = function (n, str) {
if (n == null || str == null) {
return String(this);
}
return this.replace(new RegExp('.{' + n + '}', 'g'), function (m) {
return m + str;
});
};
@st98
st98 / String#title.js
Last active August 29, 2015 14:03
Python の str#title みたいな感じで。
String.prototype.title = function () {
return this.replace(/[A-Za-z]+/g, function (m) {
return m[0].toUpperCase() + m.slice(1).toLowerCase();
});
};
@st98
st98 / gist:9f98e808d4aadd2af5fa
Created June 29, 2014 16:40
'hoge'.charAt(n) と 'hoge'[n] の違い。
'hoge'.charAt(3); // => 'e'
'hoge'[3]; // => 'e'
'hoge'.charAt(4); // => ''
'hoge'[4]; // => undefined