May 19, 2011 19:06
Мне приходится писать некоторый треш на попсовых языках и кажется, что этот треш становится ну очень трешевым и мой код не видевший функциональщины человек может запросто обзывать говнокодом. Вот вам кусок на хаксе, что скажете?
public static function forceMerge(acc, data) {
var empty = StringUtils.emptyString;
var nub = function (s1: Array, s2: Array) {
for (i in 0...s1.length)
s1[i] = StringTools.trim(s1[i]);
for (i in 0...s2.length)
s2[i] = StringTools.trim(s2[i]);
for (x in s1)
s2.remove(x);
return s2.concat(s1);
}
var toString = function(s) {
return (empty(s)) ? "" : s;
}
var mergeCat = function(a, b) {
return toString(a) + toString(b);
}
var last = function(a, b) {
return (b != null) ? b : a;
}
var mergeUniq = function(s1, s2) {
s1 = toString(s1);
s2 = toString(s2);
var e = new EReg("[.,;]", "g");
var arr1 = e.split(s1),
arr2 = e.split(s2),
ans = nub(arr1, arr2);
return ans.join(", ");
}
var attribs: Array = [];
Lambda.iter(["priority", "completion", "smiley", "flag", "star", "arrow"],
function (name) {
var a = getAttributeValue(data, name),
b = getAttributeValue(acc, name);
attribs.push( { name: name, value: last(a, b) } );
});
var features = unifyIcons(acc.features, data.features);
var dateHelper = function (a, b, last) {
if (empty(a) && empty(b))
return last;
if (empty(a))
return b;
if (empty(b))
return a;
return last;
}
var mergeTasks = function(t1: Types.Task, t2: Types.Task): Types.Task {
if (t1 == null)
return t2;
if (t2 == null)
return t1;
var start = dateHelper(t1.start, t2.start, if (t1.start < t2.start) t1.start else t2.start);
var dead = dateHelper(t1.deadline, t2.deadline,
if (t1.deadline < t2.deadline) t2.deadline else t1.deadline);
return { responsible: mergeUniq(t1.responsible, t2.responsible),
start: start, deadline: dead,
estimate: last(t1.estimate, t2.estimate) };
}
features.push( { name: "task",
value: mergeTasks(getFeatureValue(acc, "task"),
getFeatureValue(data, "task") ) });
var note1 = getFeatureValue(acc, "note"),
note2 = getFeatureValue(data, "note");
var note = mergeCat( (note1 == null) ? "" : note1.text,
(note2 == null) ? "" : note2.text );
features.push( { name: "note", value: { text: note } } );
return {
id: acc.id,
text: mergeCat(acc.text, data.text),
attributes: attribs,
features: features
};
}
haxe codestyle