Skip to content

Instantly share code, notes, and snippets.

@eleses
Created June 13, 2020 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eleses/8a34ef285134c406482f569036e04d7f to your computer and use it in GitHub Desktop.
Save eleses/8a34ef285134c406482f569036e04d7f to your computer and use it in GitHub Desktop.
// When you want to Plazy a Pfunc but still want to get resetFunc called
// use this instead. See https://bit.ly/37t90WA for more
PfuncLazy : Pattern {
var <>func;
*new { |func| ^super.newCopyArgs(func) }
asStream { |inval|
var nextFunc, resetFunc;
// don't need it for my use(s) to have inval passed to func
// but nice to have maybe
# nextFunc, resetFunc = func.value(inval);
^FuncStream.new(nextFunc, resetFunc)
}
embedInStream { |inval|
// only overridden because Pattern.embedInStream doesn't pass
// an inval to asStream, otherwise would not be needed
^this.asStream(inval).embedInStream(inval)
}
storeArgs { ^[func] }
}
(
p = PfuncLazy {
var arrs = [[1, 2], [3, 4]], apos = 0, ipos = -1;
[
{ arrs @@ apos @@ (ipos = ipos + 1) }, // nextFunc
{ "inca".postln; apos = apos + 1 } // resetFunc
]
}
)
r = p.asStream
r.next // 1
r.reset
r.next // 4; ok, behaves as with mere closure around Pfunc
// and behaves like a true Pattern otherwise
q = p.asStream // is really a new one
q.next // 1; not linked to the other stream
r.next // 3; ok
q.next // 2; ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment