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

Small change to APL #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

codereport
Copy link

Super cool repo! Not sure if you are looking for improvements but your APL can be improved to a 1-liner and it would be the idiomatic way to write the code.


Explanation

Step 1: Use as W Combinator

Whenever you have the same argument to a two-argument (dyadic) function APL, you can use (the W combinator, known as DUP in Forth or join in Haskell) to duplicate or copy your argument so you only need to spell it once. So

≢∪,a∘.*a

becomes

≢∪,∘.*⍨a

Step 2: Remove unnecessary use of variable a

Because a is only used once now (because of Step 1), you can know make it a 1-liner So:

a←(⍳100)~1
≢∪,∘.*⍨a

becomes:

≢∪,∘.*⍨(⍳100)~1

Step 3: Use as C Combinator

Finally, whenever you need to use parentheses on the left argument to a dyadic function (in this case ~) and the right argument would not need them, you can use (the C combinator, known as SWAP in Forth and flip in Haskell) to flip the order the dydyic function is applied and then the parentheses are no longer needed. So:

≢∪,∘.*⍨(⍳100)~1

becomes:

≢∪,∘.*⍨1~⍳100

More info on combinators can be found at: https://combinatorylogic.com/table.html

Once again, really cool project! 🔥

@jaredkrinke
Copy link
Owner

Thanks! I like the idea of having experienced people point out simpler/more idiomatic solutions.

I'm leaving the code as is (I don't want to misrepresent your solution as my code), but I want to leave this PR open so that people can see this improvement, along with the detailed explanation.

Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants