<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: lit</title>
    <description>The latest articles on DEV Community by lit (@litlighilit).</description>
    <link>https://dev.to/litlighilit</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1379601%2F0c73d64f-3558-499c-9f57-88e5409a9590.png</url>
      <title>DEV Community: lit</title>
      <link>https://dev.to/litlighilit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/litlighilit"/>
    <language>en</language>
    <item>
      <title>NimPyLib - Write Python in Nim</title>
      <dc:creator>lit</dc:creator>
      <pubDate>Thu, 17 Apr 2025 21:38:51 +0000</pubDate>
      <link>https://dev.to/litlighilit/nimpylib-write-python-in-nim-3377</link>
      <guid>https://dev.to/litlighilit/nimpylib-write-python-in-nim-3377</guid>
      <description>&lt;p&gt;Have you ever looked for a faster Python implementation?&lt;br&gt;
Have you ever wondered Nim's stdlib not abundant and not handy like Python's?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://nimpylib.org" rel="noopener noreferrer"&gt;nimpylib/pylib&lt;/a&gt; is here for you!&lt;/p&gt;

&lt;p&gt;NimPyLib(pylib in short), is a pure Nim library written in Nim and targeting in Nim, which implements Python-like functions, sugars and libraries. Visit &lt;a href="https://nimpylib.org" rel="noopener noreferrer"&gt;official website&lt;/a&gt; for details.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to use?
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Install
&lt;/h3&gt;

&lt;p&gt;To use it, just use nimble(nim's package manager):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nimble &lt;span class="nb"&gt;install &lt;/span&gt;pylib
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Use
&lt;/h3&gt;

&lt;h4&gt;
  
  
  basic
&lt;/h4&gt;

&lt;p&gt;After a simple &lt;code&gt;import pylib&lt;/code&gt;,&lt;br&gt;
you can use Python-like builtns and syntax (a.k.a. sugars)&lt;/p&gt;

&lt;p&gt;That means, you can just suppose you're writting Python,&lt;br&gt;
which is static-typed and to be compiled.&lt;/p&gt;

&lt;h4&gt;
  
  
  standard libraries
&lt;/h4&gt;

&lt;p&gt;To use libraries,&lt;br&gt;
use &lt;code&gt;import pylib/Lib/xxx&lt;/code&gt; for xxx is a library name.&lt;/p&gt;

&lt;h3&gt;
  
  
  Contribute
&lt;/h3&gt;

&lt;p&gt;Welcome to &lt;a href="https://github.com/nimpylib/pylib/" rel="noopener noreferrer"&gt;github.com/nimpylib/pylib&lt;/a&gt;, even a star is very encouraging.&lt;/p&gt;

</description>
      <category>python</category>
      <category>nim</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Pure Science/Math Notation in Nim: Multiplication via just spaces</title>
      <dc:creator>lit</dc:creator>
      <pubDate>Sun, 15 Sep 2024 05:24:49 +0000</pubDate>
      <link>https://dev.to/litlighilit/pure-sciencemath-notation-in-nim-multiplication-4jfo</link>
      <guid>https://dev.to/litlighilit/pure-sciencemath-notation-in-nim-multiplication-4jfo</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Flexible is Nim's syntax, which allows you to write something you may never image that you can write freely in any programming language! And sometimes it even makes just wanna say "WoW".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And this time I'd show you something you can easily do to simplify the notation of multiplication in Nim, making your writing mathematics expression simpler and more comfortable.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Multiplication Sign
&lt;/h2&gt;

&lt;p&gt;Just think back: in the period of learning Elementary Mathematics, you were once told that "The multiplication sign can be omitted", e.g. &lt;code&gt;a×b&lt;/code&gt; is written as &lt;code&gt;ab&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;From then on, &lt;code&gt;×&lt;/code&gt; almost disappears between two numbers (you may come up with its usage of &lt;code&gt;vector cross product&lt;/code&gt;, but that's another story, and I'm going to introduce relative feature also supported by Nim in following post), it became very normal for you to omit the multiplication sign between numbers.&lt;/p&gt;

&lt;p&gt;However, later you met programming languages like C/C++, Java, Go, Rust, etc. or smalltalk, Haskell, ... And you were told multiplication is done via &lt;code&gt;*&lt;/code&gt;, and you had to write &lt;code&gt;a*b&lt;/code&gt; over &lt;code&gt;a b&lt;/code&gt;, which is, not just with an additional character, and, for typing this character, &lt;code&gt;*&lt;/code&gt;,&lt;br&gt;
I guess it's sometimes a strain on one's pinky finger due to keeping pressing the "Shift" key.&lt;/p&gt;
&lt;h2&gt;
  
  
  Can We Write Code just like Mathematics Expressions
&lt;/h2&gt;

&lt;p&gt;So can you imagine there are such a language in which &lt;code&gt;*&lt;/code&gt; can just be omitted like in Mathematics?&lt;/p&gt;

&lt;p&gt;Yes, you can make it done in Nim, via &lt;code&gt;{.experimental: "callOperator".}&lt;/code&gt;, mentioned above.&lt;/p&gt;

&lt;p&gt;To clarify, Nim itself isn't a special language peculiar to the field of mathematics (also called &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/DSL/Domain_specific_language" rel="noopener noreferrer"&gt;DSL&lt;/a&gt;), and its original multiplication is also written as &lt;code&gt;a*b&lt;/code&gt;, like most Universal Programming Language. However, with great power provided by Nim, it's easy to make it.&lt;/p&gt;

&lt;p&gt;Let's see how easy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nim"&gt;&lt;code&gt;&lt;span class="p"&gt;{.&lt;/span&gt;&lt;span class="n"&gt;experimental&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"callOperator"&lt;/span&gt;&lt;span class="p"&gt;.}&lt;/span&gt;  &lt;span class="c"&gt;# required for func `()`&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;`()`&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then here you are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nim"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt;
  &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
  &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
  &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;
&lt;span class="n"&gt;assert&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;54&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you may replace your code like (assuming a,b,c is all integer)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;math.h&amp;gt;&lt;/span&gt;&lt;span class="c1"&gt;  // pow&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// in fact for parameter types,&lt;/span&gt;
&lt;span class="c1"&gt;// you shall write `const int` over `int` to express&lt;/span&gt;
&lt;span class="c1"&gt;// what Nim expresses below.&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nim"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;math&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;`^`&lt;/span&gt;

&lt;span class="c"&gt;# ...&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;^&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="c"&gt;# ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And with little speed loss (Nim's speed is almost the same as C)&lt;/p&gt;

&lt;p&gt;Don't you like this?&lt;/p&gt;




&lt;p&gt;In case you wonder what happened to code above,&lt;br&gt;
I'd like to give some explain.&lt;/p&gt;
&lt;h2&gt;
  
  
  Explain
&lt;/h2&gt;

&lt;p&gt;Thanks to Nim's &lt;a href="https://nim-lang.org/docs/manual.html#procedures-command-invocation-syntax" rel="noopener noreferrer"&gt;command-invocation-syntax&lt;/a&gt;, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Routines can be invoked without the () if the call is syntactically a statement.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Therefore, in code above, &lt;code&gt;(a b c)&lt;/code&gt; is just the same as &lt;code&gt;(a(b(c)))&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  What Else We Can Do with These Features of Nim
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Command Invocation Syntax
&lt;/h3&gt;

&lt;p&gt;Also thanks to the feature of &lt;a href="https://nim-lang.org/docs/manual.html#procedures-command-invocation-syntax" rel="noopener noreferrer"&gt;command-invocation-syntax&lt;/a&gt;, life is simpler with Nim:&lt;/p&gt;

&lt;p&gt;Code like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nim"&gt;&lt;code&gt;&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nim"&gt;&lt;code&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;can be written.&lt;/p&gt;

&lt;p&gt;And quite a few parentheses can be omitted, saving your pinky finger from pain of keeping pressing shift key, again.&lt;/p&gt;

&lt;h3&gt;
  
  
  Call Operator
&lt;/h3&gt;

&lt;p&gt;For &lt;code&gt;{.experimental: "callOperator".}&lt;/code&gt;, &lt;/p&gt;

&lt;p&gt;not sure if you recall, but it's Nim's equivalent as of Python's &lt;code&gt;__call__&lt;/code&gt;, though more powerful.&lt;/p&gt;

&lt;p&gt;It allows you to make a object callable.&lt;/p&gt;

&lt;p&gt;But in Nim, not only for object (instance of one class), but for any type! Such a feature that allows programmers bind routines to any type beyond such a type's original definition is often called "extension methods", which is supported in various forms by C#, Kotlin, Swift, Ruby, etc. (though in Nim this feature is subtly different from other languages, but introduce it is beyond topic, and I may mention this in later post)&lt;/p&gt;

&lt;p&gt;Therefore, you can just make primitive types like &lt;code&gt;int&lt;/code&gt; callable, not only custom defined types.&lt;/p&gt;

&lt;h2&gt;
  
  
  EOF
&lt;/h2&gt;

&lt;p&gt;Thanks for reading till EOF.&lt;/p&gt;

&lt;p&gt;I'm going to write about Nim's powerful syntax and macros as a serials, but may lack time.&lt;/p&gt;

&lt;p&gt;The following are some relative resources for you to preview if interested:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A few great magic macro libraries written in Nim: &lt;a href="https://github.com/nim-meta" rel="noopener noreferrer"&gt;nim-meta&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;"&lt;em&gt;Write Python in Nim!&lt;/em&gt; "  A pure Nim library providing Python-like functions, macros... &lt;a href="https://github.com/nimpylib/pylib/" rel="noopener noreferrer"&gt;NimPyLib&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Bridge of Python and Nim,  simplifiying your writing Python C-extension and calling Python in Nim: &lt;a href="https://github.com/yglukhov/nimpy" rel="noopener noreferrer"&gt;NimPy&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>nim</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
