[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[sc-dev] [commit] obtain / instill
In cases where arrays and singular objects are used together to
express properies (like in quant values) I found that an easy way of
dealing with this is needed.
I've added these methods:
instill(index, item, default)
if reciever is an array, the item is inserted in the array,
which is, if needed, extended with default elements.
if receiver is not an array and the index is 0, the item is returned.
otherwise an array with the receiver as first element and the item
at the given index is returned.
obtain(index, default)
if receiver is an array, return either object at the index
or the default (if none exists).
if receiver is not an array, return either the receiver
itself (index 0)
or the default value
// examples
[1, 2, 3].obtain(1); // 2
[1].obtain(1); // nil
[1].obtain(1, \foo); // \foo
45.obtain(0); // 45
45.obtain(1); // nil
45.obtain(1, \foo); // \foo
[1, 2, 3, 4].instill(2, 9.0); // [ 1, 2, 9, 4 ]
[1].instill(2, 9.0); // [ 1, nil, 9 ]
[1].instill(2, 9.0, \foo); // [ 1, \foo, 9 ]
45.instill(2, 9.0); // [ 45, nil, 9 ]
45.instill(2, 9.0, \foo); // [ 45, \foo, 9 ]
// to obtain the offset value from quant:
quant.obtain(1)
// set the offset value in quant
quant = quant.instill(1, val)
// setting the quant value in quant will not necessarily change it to an array:
quant = quant.instill(0, val)
4.instill(0, 2) // changes quant from 4 to 2
[4, 0.5] // changes quant from [4, 0.5] to [2, 0.5]
Index: Object.sc
===================================================================
RCS file:
/cvsroot/supercollider/SuperCollider3/build/SCClassLibrary/Common/Core/Object.sc,v
retrieving revision 1.65
diff -p -b -B -r1.65 Object.sc
*** Object.sc 19 Dec 2004 11:00:02 -0000 1.65
--- Object.sc 19 Dec 2004 17:44:47 -0000
*************** Object {
*** 295,300 ****
--- 295,310 ----
^[this.bubble(depth,levels-1)]
}
+ // compatibility with sequenceable collection
+
+ obtain { arg index, default; ^if(index == 0) { this } { default } }
+
+ instill { arg index, item, default;
+ ^if(index == 0) { item } {
+ this.asArray.instill(index, item, default)
+ }
+ }
+
diff -p -b -B -r1.34 SequenceableCollection.sc
*** SequenceableCollection.sc 19 Dec 2004 11:01:24 -0000 1.34
--- SequenceableCollection.sc 19 Dec 2004 17:47:59 -0000
*************** SequenceableCollection : Collection {
*** 206,211 ****
--- 206,222 ----
putFirst { arg obj; if (this.size > 0, { ^this.put(0, obj) }) }
putLast { arg obj; if (this.size > 0, { ^this.put(this.size -
1, obj) }) }
+
+ // compatibility with isolated objects
+
+ obtain { arg index, default; ^this[index] ? default }
+
+ instill { arg index, item, default;
+ var res;
+ res = if(index >= this.size) { this.extend(index + 1,
default) } { this }
+ ^res.put(index, item)
+ }
+
--
.