함수 본문을 콜백으로 바꾸기 리팩터링을 적용해 본다.
function arraySet(array, idx, value) {
var copy = array.slice(); // 앞부분
copy[idx] = value; // 본문
return copy; // 뒷부분
}
function push(array, elem) {
var copy = array.slice();
copy.push(elem);
return copy;
}
function drop_last(array) {
var copy = array.slice();
copy.pop();
return copy;
}
function drop_shift(array) {
var copy = array.slice();
copy.shift();
return copy;
}
function arraySet(array, idx, value) {
var copy = array.slice();
copy[idx] = value;
return copy;
}
function arraySet(array, idx, value) {
return withArrayCopy(array);
}
function withArrayCopy(array) {
var copy = array.slice();
copy[idx] = value;
return copy;
}
function arraySet(array, idx, value) {
return withArrayCopy(array, function(copy) { copy[idx] = value; };);
}
function withArrayCopy(array, modify) {
var copy = array.slice();
modify(copy);
return copy;
}
function push(array, elem) {
var copy = array.slice();
copy.push(elem);
return copy;
}
function push(array, elem) {
return withArrayCopy(array, function(copy) {
copy.push(elem);
});
}
// =========
function drop_last(array) {
var array_copy = array.slice();
array_copy.pop();
return array_copy;
}
function drop_last(array) {
return withArrayCopy(array, function(copy) {
copy.pop();
});
}