Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guest post. #90

Merged
merged 4 commits into from Jun 7, 2018
Merged

Add guest post. #90

merged 4 commits into from Jun 7, 2018

Conversation

ghost
Copy link

@ghost ghost commented Jun 3, 2018

No description provided.

Copy link
Contributor

@dom96 dom96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for writing this, it's great to get more learning resources for this topic :)

@@ -0,0 +1,120 @@

---
title: "Create a simple macro"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps "Creating a simple macro" would be better


```nim
template adder(graph, src, dest): untyped =
graph.addEdge(initEdge(initNode(src), initNode(dest)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

be consistent with your indentation, this has 3 spaces :)

this: `` `->`(result, "Boston", "Providence") ``. A macro, however can be used
to create a simpler and more appealling syntax.

> Macros can be used to implement domain specific languages.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will look a bit odd as a blockquote. Might be better to recreate the admonition HTML generated in this post: https://nim-lang.org/blog/2017/10/02/documenting-profiling-and-debugging-nim-code.html

(It uses adoc for this, but you should be able to just write that HTML into the document)

Copy link
Author

@ghost ghost Jun 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is hard for me. Can you provide the html?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm


```nim
proc buildCityGraph(): Digraph =
result = initGraph()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to generate this in the macro?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the init function? Well I thought (also sections like edges: ...) so but I just wanted something really simple.

```

What is important to understand now is that, while we are done, procedurally
modifying an AST is not the correct solution. It is a tree after all. Also it
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh? This doesn't explain why that is not a correct solution at all. It looks perfectly correct to me.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopping someone can provide better reasoning here but i stand by this opinion.

From making this simple macro we can take away how to structure procs that
transform the Nim AST.

> **Bonus excersise** In the ``buildCityGraph`` proc we see there is an undirected edge.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would write this like so:

"There is an undirected edge in the buildCityGraph proc. Can you ..."

It reads better because it's more concise.

Hello, as you might know Nim is a powerful programming language that supports
metaprogramming using macros. Though a lot of Nim programmers are unaware about
the merits of them due to lack of learning resources. In the first part of
this series we will discuss the use of macros to simplify the creation of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend going through and removing any use of "we" in sentences. This sentence can be rewritten to:

"The first part of this series will discuss the ..."

It makes sentences more concise and thus easier to read.

if n.kind == nnkInfix and $n[0] == "->":
# we pass the template to getAst to avoid constructing
# the AST manualy
result.add getAst(adder(head, n[1], n[2]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great use of getAst.

# a parent node is created with the correct kind.
result = copyNimNode(body)
for n in body:
result.add graphDslImpl(head, n)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why this code is better than the version above.

title: "Create a simple macro"
author: Antonis Geralis
---

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a "Guest Post" div, like done here:

https://nim-lang.org/blog/2018/01/22/yes-command-in-nim.html

Look in the underlying .md to see how it's done.

the possibility of using an operator with a nice syntax. Although operators can
have more than two parameters, we are limited into calling them like
this: `` `->`(result, "Boston", "Providence") ``. A macro, however can be used
to create a simpler and more appealling syntax.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually a template would work for this too. I understand that you want to show off macros here, so add maybe "(A template could do so too here, but I want to show how macros work.)"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, you mean, result is hardcoded in the template?

result.add getAst(adder(head, n[1], n[2]))
```

What is important to understand now is that, while we are done, procedurally
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this paragraph to "This macro is incomplete however, it doesn't replace nested usages of ->. We would like to replace -> anywhere in the passed body, so let's use recursion with the help of a helper called graphDslImpl .

@ghost
Copy link
Author

ghost commented Jun 5, 2018

@dom96 done.

@dom96 dom96 merged commit 4da3f25 into nim-lang:master Jun 7, 2018
dom96 added a commit that referenced this pull request Jun 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants