Formerly Nimrod, Nim, which has been under development since around 2008. To get Python users to understand its features, I think it's a good idea to take a look at the following code in the Japanese version of wikipedia, nim.
wikipedia.nim
proc reverse(s: string): string =
result = "" #Implicit result variable
for i in countdown(high(s), 0):
result.add s[i]
var str1 = "Reverse This!"
echo "Reversed: ", reverse(str1)
A little bit of wikipedia commentary:
--Focus on the implicit result variable. In Nim, procedures whose return type is not void have an implicit result variable. The result variable holds the return value of the procedure. --countdown in a for loop is an iterator. Here, an iterator that counts down from the character string length obtained by high (s) to 0 is described.
Although Nim is a typed compilation language, it can be written like a scripting language.
It seems that Nim began to be known in Japan around the blog about five years ago by Takano32, "Let's write FizzBuzz with Nimrod!".
Takano32, a Ruby committer, then immediately posted an entry, "How can I use Python with Nimrod !?"
Yes, there are quite a few people trying to use python and nim together (what about the relationship between ruby and nim?).
So, this time, I'd like to call nim from python and think about how to use it.
* So, if you are a python user who is interested in nim, which has the same execution speed as C language, not horse-like, please read the source code of nim and the original document and post it on your blog. .. I will be saved ^ _ ^ *Basically, if you follow Connecting Nim to Python, it's quick. What we are doing is creating a so-called shared library in C language from the source code of nim. Binaries are execution environment dependent. Below is an example on a Mac. If you have xcode and the latest brew, you only need to install nim as follows.
brew install nim
First, prepare nim code with only one procedure (function),
fn1.nim
proc summer*(x, y: float): float {. exportc, dynlib .} =
result = x + y
Below, it will be made into a shared library.
nim c --app:lib fn1.nim
The python code that calls the generated libfn1.dylib is below (using the python 2.7 that comes with the Mac from the beginning): Since it is a C language shared library call, use ctypes.
py2nim1.py
from ctypes import *
def main():
test_lib = CDLL('./libfn1.dylib')
# Function parameter types
test_lib.summer.argtypes = [c_double, c_double]
# Function return types
test_lib.summer.restype = c_double
sum_res = test_lib.summer(1.0, 3.0)
print('The sum of 1.0 and 3.0 is: %f'%sum_res)
if __name__ == '__main__':
main()
If you pay attention to the double type in the 64-bit environment, you can get the answer "4.0" from nim.
In an environment where numpy, pypy, micropython, R18python ... etc. are not obscene and specialized py terms are used in pursuit of execution speed, nim may be able to blend in smoothly. Even if it doesn't, "Hey, look at this, you. What do you think of this guy? ”-If you have an environment like“ It's really big ... (in terms of computational power) ”, it's possible to challenge the process with python + C language + nim. It seems to be.
Since about 20 years when it was a minor language, innumerable code has been written by fierce men, and there are various "arts" in python. For example, the Mac environment contains an Objective-c (++) calling library called PyObjC, and you can immediately call nim while calling a GUI written in Objective-c with python. Since the GUI call description on the python side is long, put the code in gist.
Execution example:
Creating a GUI for users to call a calculation library written in nim is much easier with python. Nim, the compilation language, can concentrate on data processing descriptions that call C / C ++, which has a high affinity, as appropriate.
... and I wrote about the ratio and common sense about nim, which is now limited to the material language. But, big brother, don't worry. With this as a starting point, I will write a story post on the subject of IoT in the near future.
Recommended Posts