Created
April 5, 2014 06:18
-
-
Save higuma/9988082 to your computer and use it in GitHub Desktop.
CoffeeScriptワンポイント - 値を返さない関数にはreturnを付ける ref: http://qiita.com/higuma/items/ff329775f3a91a6c6fb3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = for x in [0..5] | |
x * x | |
console.log a # => [ 0, 1, 4, 9, 16, 25 ] | |
b = while a.length > 0 | |
a.pop() | |
console.log b # => [ 25, 16, 9, 4, 1, 0 ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = (x * x for x in [0..5]) # (...)が必要 | |
console.log a # => [ 0, 1, 4, 9, 16, 25 ] | |
b = (a.pop() while a.length > 0) # これも(...)が必要 | |
console.log b # => [ 25, 16, 9, 4, 1, 0 ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sprintf("%#{w}.#{f}f", n) のCoffeeScript版 | |
sprintf = (n, w, f) -> | |
buf = n.toFixed f | |
buf = ' ' + buf while w > buf.length | |
buf | |
printNumbers = (numbers, w, f) -> # CoffeeScriptならたった一行(なのに...) | |
console.log sprintf n, w, f for n in numbers | |
printNumbers [1.5, 7.3, -4.1], 5, 1 | |
### 出力は次の通り | |
1.5 | |
7.3 | |
-4.1 | |
### |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by CoffeeScript 1.7.1 | |
(function() { | |
var printNumbers, sprintf; | |
// 中略 | |
// 何でこんなに長くなるのか? 実は理由がある | |
printNumbers = function(numbers, w, f) { | |
var n, _i, _len, _results; | |
_results = []; // 値を返すための(無駄な)配列 | |
for (_i = 0, _len = numbers.length; _i < _len; _i++) { | |
n = numbers[_i]; | |
_results.push(console.log(sprintf(n, w, f))); | |
// console.log(...)の戻り値(undefined)を追加 | |
} | |
return _results; // => [ undefined, undefined, ... ] | |
}; | |
// 以下略 | |
}).call(this); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
printNumbers = (numbers, w, f) -> | |
console.log sprintf n, w, f for n in numbers | |
return # <= !!! 追加 !!! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by CoffeeScript 1.7.1 | |
(function() { | |
// 中略 | |
// この出力ならリーズナブル(修正不要) | |
printNumbers = function(numbers, w, f) { | |
var n, _i, _len; | |
for (_i = 0, _len = numbers.length; _i < _len; _i++) { | |
n = numbers[_i]; | |
console.log(sprintf(n, w, f)); | |
} | |
}; | |
// 以下略 | |
}).call(this); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
log = (x) -> # console.logの短縮形 | |
console.log x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by CoffeeScript 1.7.1 | |
(function() { | |
var log; | |
log = function(x) { | |
return console.log(x); // このreturnは不要 | |
}; | |
}).call(this); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
log = (x) -> | |
console.log x | |
return # <= 追加 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by CoffeeScript 1.7.1 | |
(function() { | |
var log; | |
log = function(x) { | |
console.log(x); // returnは付かない | |
}; | |
}).call(this); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sprintf("%#{w}.#{f}f", n) のCoffeeScript版 | |
sprintf = (n, w, f) -> | |
buf = n.toFixed f | |
buf = ' ' + buf while w > buf.length | |
buf | |
printNumbers = (numbers, w, f) -> # CoffeeScriptならたった一行(なのに...) | |
console.log sprintf n, w, f for n in numbers | |
printNumbers [1.5, 7.3, -4.1], 5, 1 | |
### 出力は次の通り | |
1.5 | |
7.3 | |
-4.1 | |
### |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by CoffeeScript 1.7.1 | |
(function() { | |
var printNumbers, sprintf; | |
// 中略 | |
// 何でこんなに長くなるのか? 実は理由がある | |
printNumbers = function(numbers, w, f) { | |
var n, _i, _len, _results; | |
_results = []; // 値を返すための(無駄な)配列 | |
for (_i = 0, _len = numbers.length; _i < _len; _i++) { | |
n = numbers[_i]; | |
_results.push(console.log(sprintf(n, w, f))); | |
// console.log(...)の戻り値(undefined)を追加 | |
} | |
return _results; // => [ undefined, undefined, ... ] | |
}; | |
// 以下略 | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment