python filter function

109 views
Skip to first unread message

Michael Burns-Kaurin

unread,
Nov 9, 2021, 11:25:37 AM11/9/21
to Glowscript Users
I tried to use the python filter function in glowscript without success.  Is there some trick or does it simply not work?

Michael Burns-Kaurin

unread,
Nov 9, 2021, 12:22:01 PM11/9/21
to Glowscript Users
for this code

b = [0,1,2,3,4,5]
filtered_b = filter(lambda x: x>1, b)
print(list(filtered_b))

I get the error message "Unexpected token name «x», expected punc «,» at line 4: filtered_b = filter(lambda x: x>1, b)"

I use the same code in "regular" python and it works as expected, output is [2, 3, 4, 5].

Bruce Sherwood

unread,
Nov 9, 2021, 12:52:19 PM11/9/21
to Glowscript Users
The problem is that the RapydScript Python-to-JavaScript transpiler does not support this syntax, as can be seen if you run your code at https://sw.kovidgoyal.net/rapydscript/repl/. It appears that the use of "lambda" is not supported. For details, go to


and search for "lambda".

Bruce

Michael Burns-Kaurin

unread,
Nov 9, 2021, 1:03:12 PM11/9/21
to Glowscript Users
I try another method (using a function instead of lambda).  At the rapydscript site, it runs properly.  On glowscript, it does not filter the list and just gives the entire list back.

Michael Burns-Kaurin

unread,
Nov 9, 2021, 1:04:23 PM11/9/21
to Glowscript Users
def criteria(x):
  if x >1:
    return True
  else:
    return False

b = [0,1,2,3,4,5]
filtered_b = filter(criteria, b)
print(list(filtered_b))

Bruce Sherwood

unread,
Nov 9, 2021, 3:01:37 PM11/9/21
to Glowscript Users
Fixed. Thanks for the report.

Bruce

Michael Burns-Kaurin

unread,
Nov 9, 2021, 3:20:51 PM11/9/21
to Glowscript Users
So it should work at glowscript.org?  I must still be missing something.

Michael Burns-Kaurin

unread,
Nov 9, 2021, 3:48:02 PM11/9/21
to Glowscript Users
Tried clearing the cache, still not filtering.

Bruce Sherwood

unread,
Nov 9, 2021, 9:08:01 PM11/9/21
to Glowscript Users
Dumb mistake on my part. Stay tuned.

Bruce

Bruce Sherwood

unread,
Nov 9, 2021, 10:56:44 PM11/9/21
to Glowscript Users
Okay, seems finally to be working properly. Sorry for the confusion. The basic problem is that Python's filter function is rather unusual in having a function name as its first argument, which led to complications in inserting or not inserting async and await in appropriate places in a VPython program.

Bruce

Michael Burns-Kaurin

unread,
Nov 9, 2021, 11:03:04 PM11/9/21
to Glowscript Users
Works!  Thanks!  It must be challenging to replicate python in such an environment.

Bruce Sherwood

unread,
Nov 9, 2021, 11:10:02 PM11/9/21
to Glowscript Users
It is indeed challenging. Obviously RapydScript does the really heavy lifting in converting Python to JavaScript, but the remaining hard part has been appropriately inserting "async" in front of user functions and "await" in front of calls to user functions in order to make it possible for functions to contain statements as pause or waitfor or rate, all of which involve waiting states.

Bruce

Michael Burns-Kaurin

unread,
Nov 10, 2021, 9:18:25 AM11/10/21
to Glowscript Users
Thank you Bruce for doing that work.
Reply all
Reply to author
Forward
0 new messages