Having trouble with resize from wslib

Hello there,

I am at the very beginning of my SuperCollider journey so forgive me if the answer is trivial. I am looking to resize an array using the resize method from wslib. I have tried it with the following set up

(
var arr = [1, 2, 3, 4];
arr.resize(10, 'linear');
arr.postln;
)

But the array doesn’t resize. I have looked at the documentation at docs.sccode in addition to the wslib github, but I still can’t figure out how to get my code to work. Any suggestions?

Welcome to the forum @MidsummerNateDream!

resize doesn’t do anything to change the array you use it on, but rather returns a new array which has been resized the way you want. This will achieve what you want:

(
var arr = [1, 2, 3, 4];
arr = arr.resize(10, 'linear');
arr.postln;
)

This kind of operation in technical terms is called “non-mutating” because it doesn’t change the receiver. A method which instead changes arr when you call it is “mutating.” One example of a mutating method in SC would be takeAt. The wslib documentation for resize unfortunately doesn’t make clear which kind of operation it is, but usually you can tell operations that are mutating by the words “copy” or “new” in the description, often in phrases like “Returns a new Array”, “Returns a copy of the receiver”, or “Answer a new collection.”

1 Like