Comments (5)
@elisbyberi Thanks for your hint! I made it work in Interoperability mode with types provided by codon:
cache_dict = Dict[str, u64]()
def cache(func):
global cache_dict
def wrapper(n: u64) -> u64:
if str(n) not in cache_dict:
temp = func(n)
cache_dict[str(n)] = temp
return u64(cache_dict[str(n)])
return wrapper
@cache
def fact(n: u64) -> u64:
if n == u64(0) | n == u64(1):
return u64(1)
return n.__mul__(u64(fact(n-u64(1))))
def main():
print(fact(u64(15)))
if __name__ == '__main__':
main()
I guess it doesn't need the type of callable being returned as well, which was my main focus earlier.
Another interesting note is the time taken by Interoperability with Codon provided types vs @python
Decorator, interoperability being a faster run:
Though it would still be interesting to have support for Python classes as type-hints. Thanks
from codon.
@itssoap What you are attempting to do with from python import is not documented anywhere.
from codon.
@elisbyberi my attempt was inspired by the numpy example available in the same link that you have provided. When you say its not documented, do you refer to my usage of the typing module specifically or am I doing something incorrectly. Thanks
from codon.
I was able to get it to run by switching to Decorator mode
@python
def run():
cache_dict: dict = {}
def cache(func):
def wrapper(*args, **kwargs) -> int:
n = args[0]
if str(n) not in cache_dict:
temp = func(n)
cache_dict[str(n)] = temp
return cache_dict[str(n)]
return wrapper
@cache
def fact(n: int) -> int:
if n in (0, 1):
return 1
return n * fact(n-1)
def main() -> None:
print(fact(15))
main()
if __name__ == '__main__':
run()
Also, I could further use the same strongly typed code in this decorator, get the build generated and functioning:
@python
def run():
import typing
cache_dict: dict = {}
T = typing.TypeVar('T')
P = typing.ParamSpec('P')
def cache(func: typing.Callable[P, T]) -> typing.Callable[P, T]:
def wrapper(*args: P.args, **kwargs: P.kwargs) -> typing.Type[T]:
n = args[0]
if str(n) not in cache_dict:
temp = func(n)
cache_dict[str(n)] = temp
return cache_dict[str(n)]
return wrapper
@cache
def fact(n: int) -> int:
if n in (0, 1):
return 1
return n * fact(n-1)
def main() -> None:
print(fact(15))
main()
if __name__ == '__main__':
run()
But I am still interested in knowing how to make my original code (Interoperability mode) work, without shoving my whole code in the python decorator. Thanks
from codon.
@itssoap I am referring to your usage of the Python Typing module. Everything imported from python
is just a Python object (PyObject). Hence, the test.py:4:4-31: error: expected type expression
error. Use Codon objects for typing.
You will get the same error if you use the NumPy Python object for typing in Codon:
from python import numpy as np
x: np.array = np.array([1, 2, 3, 4]) * 10
print(x) # [10 20 30 40]
i545.py:3:4-12: error: expected type expression
i545.py:4:7-8: error: name 'x' is not defined
from codon.
Related Issues (20)
- Unbuffered output HOT 1
- Inheritance between 3 classes gives an infer.cpp error HOT 1
- Error: no member named 'ExecutorSymbolDef' in namespace 'llvm::orc'
- how to integration c++ class
- bench/set_partition c++ error: no matching function for call to 'partition_filtered' HOT 2
- Build process for the jupyter plugin stops midway (MacOS)
- What is the difference between the support for reflection in Codon and Python? HOT 1
- using protobuf case HOT 5
- CURSED HOT 2
- 【Codon】With repeated processing, memory consumption increases, Killed is displayed, and processing stops. HOT 2
- Cannot set the bits of UInt data with __setitem__.
- Codon slower than PyPy, can't find out why HOT 1
- Inheritance breaks Optional
- Interop with Python breaks CTRL+C HOT 1
- Generated LLVM code can't be used by lli
- Error:Tuple from_py overload match problem
- String concatenation in a loop slow compared to Python HOT 3
- codon for android HOT 1
- Bad Performance HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from codon.