Skip to content

Commit 86da02e

Browse files
author
hartsantler
committed
support multiple inline functions as keyword args in function call.
1 parent ee3e16e commit 86da02e

3 files changed

Lines changed: 64 additions & 21 deletions

File tree

README.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,27 @@ and not have to write `a.some_method.bind(a)`
106106
b.set_callback( a->some_method )
107107
```
108108

109-
. in a function call, as a keyword argument, define callbacks inline with def
109+
. in a function call, inline functions can be given as keyword arguments
110110
```
111-
a.func( callback=def (x,y,z):
112-
x += y
113-
return x - z
114-
111+
a.func(
112+
callback1=def (x,y,z):
113+
x += y
114+
return x - z,
115+
callback2= def (x, y):
116+
return x * y
115117
)
116118
```
117119

120+
. inline functions can be used inside a dict literal
121+
```
122+
a = {
123+
'cb1' : def (x):
124+
return x,
125+
'cb2' : def (y):
126+
return y
127+
}
128+
```
129+
118130
Speed
119131
---------------
120132
PythonJS gives you the option to optimize your program for speed with a new syntax for static typing, in some cases this results in code that is 20X faster.

pythonjs/typedpython.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ def transform_source( source, strip=False ):
6262
if cs.startswith('var '):
6363
c = c.replace('var ', '')
6464

65+
if '= function(' in c:
66+
k = '= function('
67+
a,b = c.split(k)
68+
output.append( '@func_expression(%s)' %a.strip())
69+
c = 'def __NAMELESS__(' + b
70+
71+
6572
if '=\t\t\t\tdef ' in c:
6673
x, c = c.split('=\t\t\t\tdef ')
6774
indent = []
@@ -114,13 +121,27 @@ def transform_source( source, strip=False ):
114121
if 'def (' in c:
115122
c = c.replace('def (', 'def __NAMELESS__(')
116123
c, tail = c.split(d)
117-
#name = c.split(d)[-1].split('(')[0]
124+
125+
#if d.startswith('='):
126+
# if '(' in c:
127+
# c += '=lambda __INLINE_FUNCTION__: %s )' %tail.strip().split(':')[0]
128+
# else:
129+
# c += '=lambda __INLINE_FUNCTION__: %s' %tail.strip().split(':')[0]
130+
# output_post = 'def %s'%tail
131+
118132
if d.startswith('='):
119-
if '(' in c:
120-
c += '=lambda __INLINE_FUNCTION__: %s )' %tail.strip().split(':')[0]
121-
else:
122-
c += '=lambda __INLINE_FUNCTION__: %s' %tail.strip().split(':')[0]
123-
output_post = 'def %s'%tail
133+
c += '=lambda __INLINE_FUNCTION__: %s' %tail.strip().split(':')[0]
134+
135+
if output_post:
136+
if output_post[-1][-1]==',':
137+
output_post[-1] = output_post[-1][:-1]
138+
output[-1] += ','
139+
else: output_post = list()
140+
141+
output.append( c )
142+
143+
c = 'def %s'%tail
144+
124145
else:
125146
c += ':lambda __INLINE_FUNCTION__: %s,' %tail.strip().split(':')[0]
126147
output.append( c )
@@ -148,7 +169,7 @@ def transform_source( source, strip=False ):
148169
else:
149170
output.append( c )
150171

151-
if type(output_post) is str:
172+
if type(output_post) is str: ## DEPRECATED
152173
indent = 0
153174
for u in output[-1]:
154175
if u == ' ' or u == '\t':
@@ -157,14 +178,14 @@ def transform_source( source, strip=False ):
157178
break
158179
output.append( ('\t'*indent)+output_post)
159180
output_post = True
160-
elif output_post == True:
181+
elif output_post == True: ## DEPRECATED
161182
if output[-1].strip()==')':
162183
output.pop()
163184
output_post = None
185+
164186
elif type(output_post) is list:
165-
if output_post[-1].strip().endswith('}'):
187+
if output_post[-1].strip().endswith( ('}',')') ):
166188
output.append( output_post.pop() )
167-
#output.extend( output_post )
168189
indent = 0
169190
for u in output[-1]:
170191
if u == ' ' or u == '\t':
@@ -211,9 +232,16 @@ def xxx():
211232
return x+y
212233
}
213234
214-
c = def (x,y):
235+
X.func( cb1=def ():
236+
return 1,
237+
cb2=def ():
238+
return 2
239+
)
240+
241+
c = function(x,y):
215242
return x+y
216243
244+
217245
'''
218246

219247
if __name__ == '__main__':

regtests/calling/inline_def.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""inline def"""
22

3-
def test( callback=None ):
4-
return callback
3+
def test( callback1=None, callback2=None ):
4+
return {'cb1', callback1, 'cb2': callback2 }
55

66
def main():
7-
f = test( callback=def (x,y):
8-
return x+y
7+
o = test( callback1=def (x,y):
8+
return x+y,
9+
callback2 = def (x):
10+
return x*2
911
)
10-
TestError( f(1,2) == 3 )
12+
TestError( o['cb1'](1,2) == 3 )
13+
TestError( o['cb2'](100) == 200 )
1114

0 commit comments

Comments
 (0)