Skip to content

Commit

Permalink
Merge branch 'tidalcycles:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
edcrub authored Jan 5, 2025
2 parents fb1dde3 + 1e3bbc5 commit bbe0036
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
40 changes: 37 additions & 3 deletions packages/core/repl.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ export function repl({
let pPatterns = {};
let anonymousIndex = 0;
let allTransform;
let eachTransform;

const hush = function () {
pPatterns = {};
anonymousIndex = 0;
allTransform = undefined;
eachTransform = undefined;
return silence;
};

Expand All @@ -84,10 +86,37 @@ export function repl({
const toggle = () => scheduler.toggle();
const setCps = (cps) => scheduler.setCps(cps);
const setCpm = (cpm) => scheduler.setCps(cpm / 60);

// TODO - not documented as jsdoc examples as the test framework doesn't simulate enough context for `each` and `all`..

/** Applies a function to all the running patterns. Note that the patterns are groups together into a single `stack` before the function is applied. This is probably what you want, but see `each` for
* a version that applies the function to each pattern separately.
* ```
* $: sound("bd - cp sd")
* $: sound("hh*8")
* all(fast("<2 3>"))
* ```
* ```
* $: sound("bd - cp sd")
* $: sound("hh*8")
* all(x => x.pianoroll())
* ```
*/
const all = function (transform) {
allTransform = transform;
return silence;
};
/** Applies a function to each of the running patterns separately. This is intended for future use with upcoming 'stepwise' features. See `all` for a version that applies the function to all the patterns stacked together into a single pattern.
* ```
* $: sound("bd - cp sd")
* $: sound("hh*8")
* each(fast("<2 3>"))
* ```
*/
const each = function (transform) {
eachTransform = transform;
return silence;
};

// set pattern methods that use this repl via closure
const injectPatternMethods = () => {
Expand Down Expand Up @@ -131,6 +160,7 @@ export function repl({
});
return evalScope({
all,
each,
hush,
cpm,
setCps,
Expand All @@ -153,11 +183,15 @@ export function repl({
let { pattern, meta } = await _evaluate(code, transpiler, transpilerOptions);
if (Object.keys(pPatterns).length) {
let patterns = Object.values(pPatterns);
if (allTransform) {
patterns = patterns.map(allTransform);
if (eachTransform) {
// Explicit lambda so only element (not index and array) are passed
patterns = patterns.map((x) => eachTransform(x));
}
pattern = stack(...patterns);
} else if (allTransform) {
} else if (eachTransform) {
pattern = eachTransform(pattern);
}
if (allTransform) {
pattern = allTransform(pattern);
}
if (!isPattern(pattern)) {
Expand Down
4 changes: 2 additions & 2 deletions website/src/repl/components/panel/Reference.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export function Reference() {
<div className="prose dark:prose-invert min-w-full px-1 ">
<h2>API Reference</h2>
<p>
This is the long list functions you can use! Remember that you don't need to remember all of those and that
you can already make music with a small set of functions!
This is the long list of functions you can use. Remember that you don't need to remember all of those and
that you can already make music with a small set of functions!
</p>
{visibleFunctions.map((entry, i) => (
<section key={i}>
Expand Down

0 comments on commit bbe0036

Please sign in to comment.