Polyglot Programming with MetaCall

Polyglot Programming with MetaCall

Rethinking the way developer collaborate with polyglot programming using Metacall

Polyglot programming is a common term for developing software using a variety of programming languages. Usually, they develop using multiple programming languages without any preferential treatment as the job demands. Polyglot programming brings a wider tool-chain and technologies you can use pragmatically. It can help you focus more on the logic and conceptual part of a language than the semantics and the language features that are the hot topic for most discussions around programming languages.

Polyglot Programming, the idea of using multiple languages to solve problems, is a powerful way to create more robust software systems. Polyglot programming is a programming paradigm that blends two or more programming languages to address specific types of challenges—such as the need for strong types or a fast interpreter with an interactive interface.

Often it is used by programmers to expand their tool-set and sometimes to work around problems. In this article, we will look at how we can work with polyglot programming using MetaCall, a command line Interface that you can use as a piece of cake, as it offers functions that can be called from any language.

Slides (1).png

In this article we will take a look at:

What is MetaCall

MetaCall is a cross-platform library that supports the execution and mixing of various programming languages. Leveraging a multi-language interpreter, it was developed for game engines initially, but now it is being used as a Function as a Service (FaaS) using Function Mesh. Using MetaCall Core brings forward an interface that is utilized by function calls between programming languages, like C/C++, JavaScript, Ruby, Python and more. With Metacall, you can now seamlessly integrate Python and JavaScript code together into one codebase.

So what is the specific use-case, MetaCall solving for us? It allows companies to migrate their monolithic architecture to a container-based architecture. Using the FaaS toolchain, you can develop, test and deploy the application using an API gateway. With the cross-platform polyglot runtime, you can now inter-mix code samples in different languages and still manage to deploy them.

It ensures that developers can inter-mix with each other and work on highly scalable software without any friction. In the setup section, we will see how easy it is to set up the MetaCall command line Interface. We will further see an example of mixing some Python and Javascript code to build a URL shortener.

man1.png

Setting up MetaCall CLI

MetaCall is cross-platform. You can install it via Docker, Guix package manager or even through a pre-compiled tarball. For this example, we will install it via curl using a Shell script. Go over to the terminal and enter the command:

$ curl -sL https://raw.githubusercontent.com/metacall/install/master/install.sh | sh

This single command will install the MetaCall CLI and now you can use the metacall command to build polygot application. If you are looking to install MetaCall on Windows and Mac, you can read more about it on the official documentation.

You can also pull the MetaCall CLI through the DockerHub here by entering the command:

$ docker pull metacall/core

Once the command-line interface gets installed, you can launch it by pushing in the command: metacall. A global configuration would be loaded, and now you are ready to develop polyglot applications. In the next section, we will see how we can build a simple polyglot application using Metacall.

image

Building a URL Shortener using MetaCall

Let's build a simple URL shortener using Metacall. The functionality would be dead simple. We will use a simple Python script to use the TinyURL API and the requests library to take a URL and return a shortened URL. However, there would be a twist: We will call this script using a Node script. Let's try it out.

We will first install the dependencies. MetaCall maintains its versions of package managers including, npm and pip to support portability. We will use pip with metacall to install the packages initially:

$ metacall pip3 install requests

Once installed, we can now start leveraging the requests library to build a simple URL shortener script. Let's name the file app.py and type in the following:

import requests
from urllib.parse import urlencode
import sys


def make_tiny(url):
    request_url = ('http://tinyurl.com/api-create.php?' +
                   urlencode({'url': url}))
    result = requests.get(request_url)
    return result.text

print(make_tiny("http://harshcasper.hashnode.dev/polyglot-programming-with-metacall"))

The functionality is very simple. We have defined a function named make_tiny which takes a single parameter: url. The url is then passed to the TinyURL API and using requests we fetch the shortened URL using a GET request.

You can try out the script by passing a driver code. On launch, you will get a shortened URL. Now that we have our URL shortener script up and ready, we can call it from a Node script. Create a file named main.js and type in the following:

const { make_tiny } = require('./app.py');
console.log(make_tiny("https://github.com/metacall/core"));

Pretty simple, isn't it? But let's see what is happening. Here we have used the built-in require function to load the app.py file as the python script that we are using for our purpose to shorten the URL. You can launch the Node Script by pushing in:

We will see the following output:

$ metacall main.js
Information: Global configuration loaded from /gnu/store/XXXXXXXXXXXXXXXXX-metacall-0.3.17/configurations/global.json
https://tinyurl.com/y5ey8s4t
Script (main.js) loaded correctly

Easy-Peasy: You can now start creating polyglot applications using MetaCall and explore the various functionalities. The MetaCall Core is available for open-source contributions here and you can further explore new examples and functionalities.

Conclusion

image

There are plenty of advantages while using MetaCall. With the above example, you can now deploy the application as an Application Programming Interface (API) over the MetaCall FaaS. Some of the core highlights include:

  • Makes jumping between different languages almost frictionless.
  • Ensures that popular libraries in Node & Python work out of the box.
  • Easy integrations between different languages and runtimes.

MetaCall makes use of an asynchronous multi-core I/O Model. It is more efficient than the traditional REST API-based architecture. The MetaCall FaaS can import the functions automatically, generate an API gateway and with a metacall.json, you are now all ready to use your API at scale. In future articles, we will look, how we can deploy an application to the MetaCall FaaS from the ground-up.

You can find the Github Repository here