Skip to content

Instantly share code, notes, and snippets.

@telephon
Last active July 6, 2017 19:58
Show Gist options
  • Save telephon/9703b662f234ff9be6fc1497c4554001 to your computer and use it in GitHub Desktop.
Save telephon/9703b662f234ff9be6fc1497c4554001 to your computer and use it in GitHub Desktop.
These functions are possible ways of stitching one array into another. This models variants of a possible future Pbindef behaviour
// the groups of inserted items are kept as arrays for easier reading
// eventually we use something like the insertAll function to embed them instead
(
var insertAll;
insertAll = { |i, list, insertedList|
j = (i - 1).clip(0, list.size - 1);
list[0..j] ++ insertedList ++ list[j+1..]
};
f = { |x, y|
var toDo = [], foundOneAlready = false;
var res = x.copy;
y.reverseDo { |toInsert|
var i = x.indexOf(toInsert);
if(i.isNil) {
toDo = toDo.addFirst(toInsert)
} {
if(foundOneAlready) {
//res = insertAll.(i + 1, res, toDo);
res = res.insert(i + 1, toDo);
toDo = [];
foundOneAlready = false;
} {
if(toDo.notEmpty) {
res = res ++ toDo;
toDo = [];
};
foundOneAlready = true
}
}
};
if(toDo.notEmpty) {
res = toDo ++ res;
};
res
};
g = { |x, y|
var toDo = [], foundOneAlready = false;
var res = x.copy;
y.do { |toInsert|
var i = x.indexOf(toInsert);
if(i.isNil) {
toDo = toDo.add(toInsert)
} {
if(foundOneAlready) {
//res = insertAll.(i, res, toDo);
if(toDo.notEmpty) { res = res.insert(i, toDo) };
toDo = [];
foundOneAlready = false;
} {
foundOneAlready = true
}
}
};
if(toDo.notEmpty) {
res = res ++ toDo;
};
res
};
h = { |x, y|
var toDo = [], foundOneAlready = false;
var res = y.copy;
x.do { |toInsert|
var i = y.indexOf(toInsert);
if(i.isNil) {
toDo = toDo.add(toInsert)
} {
if(foundOneAlready) {
//res = insertAll.(i, res, toDo);
if(toDo.notEmpty) { res = res.insert(i, toDo) };
toDo = [];
foundOneAlready = false;
} {
foundOneAlready = true
}
}
};
if(toDo.notEmpty) {
res = res ++ toDo;
};
res
}
)
// in f, conflicts are resolved in favour of the order of the original list
f.([\a, \b, \c], []); // [ a, b, c ]
f.([], [\a, \b, \c]); // [ a, b, c ]
f.([\a, \b, \c], [\a, \b, \c]); // [ a, b, [ ], c ]
f.([\a, \b, \c], [\a, \X, \Y, \c, \Z]); // [ a, [ X, Y ], b, c, Z ]
// this is still incorrect:
f.([\a, \b, \c], [\a, \X, \b, \Y, \c, \Z]); // [ a, b, [ Y ], c, Z, X ]
// in g, conflicts are resolved in favour of the order of the inserted list
g.([\a, \b, \c], []); // [ a, b, c ]
g.([], [\a, \b, \c]); // [ a, b, c ]
g.([\a, \b, \c], [\a, \b, \c]); // [ a, b, c ]
g.([\a, \b, \c], [\a, \X, \Y, \c, \Z]); // [ a, b, [ X, Y ], c, Z ]
g.([\a, \b, \c], [\a, \X, \b, \Y, \c, \Z]); // [ a, [ X ], b, c, Y, Z ]
g.([\a, \b, \c], [\c, \b, \a]); // should be [c b a] !
g.([\a, \X, \Y, \c, \Z], [\a, \b, \c]);
// h uses the inserted list as a basis. This fits best, in my view.
h.([\a, \b, \c], []); // [ a, b, c ]
h.([], [\a, \b, \c]); // [ a, b, c ]
h.([\a, \b, \c], [\a, \b, \c]); // [ a, b, c ]
h.([\a, \b, \c], [\a, \X, \Y, \c, \Z]); // [ a, X, Y, [ b ], c, Z ]
h.([\a, \b, \c], [\a, \X, \b, \Y, \c, \Z]); // [ a, X, b, Y, c, Z ]
h.([\a, \b, \c], [\c, \b, \a]); // [ c, b, a ]
h.([\a, \b, \c], [\c, \X, \b, \a]); // [ c, X, b, a ]
h.([\a, \b, \y, \c], [\c, \X, \b, \a]); // [ c, X, b, a, y ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment