isaacg1 / pyth

Pyth, an extremely concise language. Try it here:
https://pyth.herokuapp.com/
MIT License
263 stars 57 forks source link

Filter causes it to hang #248

Closed robbie01 closed 6 years ago

robbie01 commented 6 years ago

I'm trying to golf down some problems from Project Euler. My predicate function L|!%b3!%b5 works as expected, but both sfL|!%b3!%b5U^T3 and L|!%b3!%b5)sfyU^T3 cause it to hang. I've tried both the sum command and the filter command on some sample sets, and I believe the problem to lie in the filter command.

Mr-Xcoder commented 6 years ago

You’re close, but L cannot be used like that. You must declare your function using L and later use it using y<argument>, like this:

L|!%b3!%b5fyTU^T3

Notice that T is the variable you use in the scope of the filter function. In this particular case, you can use # that takes a prefix function instead:

L|!%b3!%b5y#U^T3

However Why do you need to define a function when you can just use the variable created by the scope of the filter function, T? For example, this works, and you do not need to use L at all:

f|!%T3!%T5U^T3

Notice how beautifully T is overloaded here: in the global scope, it has the value of 10, while in the scope of f, it takes the value of the current element in the list you are filtering.

You can see what variable an operator uses by looking at its arguments in Pyth’s fancy search bar on Herokuapp (e.g f has <l:T> <col>, which means that it takes two parameters: a lambda function with the variable T and a collection)

isaacg1 commented 6 years ago

Another way to think of this: The various "higher order functions" in Pyth, such as f, m, o, etc. don't actually take functions as arguments. Instead, they take code blocks. |!%T3!%T5 is placed immediately after f, as Mr-Xcoder mentioned.

Feel free to ask more if it's still unclear. That being said, the language is working as intended.