Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make curried function composable #36

Open
Tracked by #1258
felixroos opened this issue Apr 10, 2022 · 7 comments · May be fixed by #1240
Open
Tracked by #1258

make curried function composable #36

felixroos opened this issue Apr 10, 2022 · 7 comments · May be fixed by #1240
Labels
enhancement improves an existing feature

Comments

@felixroos
Copy link
Collaborator

Curried functions are currently not composable:

this works:

"c3".superimpose(
  add(3),
  add(7),
)

this doesn't work:

"c3".superimpose(
  add(3).velocity(.5),
  add(7),
)

error: add(...).velocity is not a function

this works:

"c3".superimpose(
  x => x.add(3).velocity(.5),
  add(7),
)

ideally, all curried functions need to have all pattern methods.
This is what makeComposable is supposed to do, but it does not seem to work

@omicron321
Copy link

omicron321 commented Apr 11, 2022

I suggest priority on rewrite of compose #35, as it would help confine current issue

@felixroos
Copy link
Collaborator Author

#35 is done now. still not sure how to approach this. I thought it would be possible with a custom curry function (with additional overload function), but it's tricky. Some people use Proxy for this https://medium.com/dailyjs/haskell-like-composition-in-javascript-6142a2a82821 . Of course this could be fixed more easily on the eval side, but it then wouldn't work without it.

@felixroos felixroos added the enhancement improves an existing feature label Apr 24, 2022
@felixroos
Copy link
Collaborator Author

Maybe this problem can be solved much easier by flipping it around: https://strudel.tidalcycles.org?fpNMjYivvAhZ

@yaxu
Copy link
Member

yaxu commented Nov 17, 2022

Bringing discussion from #264.. Maybe we should move all the currying inside the patterns.

So add(3) returns a pattern of functions. Then add(3).mul(.5) composes together two patterns of functions, and add(3).mul(.5).fast(2) speeds up that pattern of functions.

This approach would be a challenge for stuff like this though:
s(3).n(2).every(2, add(n(3)).chop(3) )

as chopping implies changing the structure of the outer s(3).n(2) pattern, which I think the chop is too far away from..

But I think s(3).n(2).every(2, add(chop(3).n(3))) would be ok.

@yaxu
Copy link
Member

yaxu commented Feb 17, 2023

This is working now via #390 , as

note("c3").superimpose(
  add.note(3).velocity(.5),
  add.note(7),
)

or

note("c3").superimpose(
  add(note(3)).velocity(.5),
  add(note(7)),
)

@yaxu yaxu closed this as completed Feb 17, 2023
@yaxu yaxu reopened this Jan 11, 2025
@yaxu
Copy link
Member

yaxu commented Jan 11, 2025

#390 was reverted, due to performance issues discussed in #487, so we still don't have composable functions.

One solution could be proxies. The below is proxify, to wrap around functions that take a single pattern as input, so that they can be composed with pattern methods. Applied to id, we get a hitch function that behaves like a lambda:

const handler = {
  get: function (target, prop, receiver) {
    // chain pattern methods
    if (prop in Pattern.prototype) {
      return((...args) => new Proxy((pat) => target(pat)[prop](...args), handler));
    }
    return Reflect.get(target, prop);
  },
};
const proxify = func => new Proxy(func, handler);

const hitch = proxify(id);

$: s("bd sd").apply(hitch.n("<2 4>").chop(32).jux(rev).fast(2))

@yaxu
Copy link
Member

yaxu commented Jan 12, 2025

Attempt at building this into register #1240

@yaxu yaxu linked a pull request Jan 12, 2025 that will close this issue
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement improves an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants