Polyglot Programming with MetaCall
Tags: polyglot, programming, metacall, opensource
Published: 2021-04-04 10:09:54

Polyglot programming is the practice of writing code in multiple languages to capture additional functionality and efficiency not available in a single language. The use of domain-specific languages (DSLs) has become a standard practice for enterprise application development.

In this article, I will talk about that how can we can work with polyglot programming using MetaCall.

## What is MetaCall

It is an [Open Source](https://github.com/metacall/core) Product. MetaCall allows calling functions, methods, or procedures between multiple programming languages.

![MetaCall](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w2809je6zmcrczpdln5w.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 can install it via `curl` using a Shell script. Go over to the terminal and enter the command:

```sh
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 a polyglot application. If you are looking to install MetaCall on Windows and Mac, you can read more about it on the [official documentation](https://github.com/metacall/core/blob/develop/docs/README.md#41-installation).

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

```sh
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.

## Building a Currency Converter using MetaCall

![CurrencyConverter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4dp5ownk08cne6vdwuc9.png)
 

Let's build a simple Currency Converter using Metacall. The functionality would be dead simple. We will use a simple Python script to convert one currency form to another, (in this example USD to INR) and the `requests` library to take a Currency and return a requested converted Currency. 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:

```sh
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 `main.py` and type in the following:

```sh
import json
from urllib.request import urlopen

with urlopen("http://api.exchangeratesapi.io/v1/latest?access_key=<YOUR_API_KEY>&format=1") as response:
    source = response.read()

data = json.loads(source)


# function for coversion
def convertCurrency(amount, _from, _to):

    rateArray = data["rates"]

    fromR = rateArray.get(_from)
    toR = rateArray.get(_to)

    total = amount * toR / fromR
    print(str(amount) + " " + _from + " = " + str(total) + " " + _to)


convertCurrency(100, "USD", "INR")

```

Add Your API Key in Line 4. The functionality is very simple. We have defined a function named `convertCurrency` which takes a single parameter: currency (string). The string is then passed to the ExchangeRates API and using requests we fetch the shortened Currency using a GET request.

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

```sh
import { convertCurrency } from "./main.py";

console.log(convertCurrency(100, "USD", "INR")); // It will Show the conversion of 100 USD to INR
```

we have used the built-in require function to load the `main.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:

On Running this in Terminal:

```sh
 metacall main.js
```

We will see the following output:

```sh
Information: Global configuration loaded from /gnu/store/XXXXXXXXXXXXXXXXX-metacall-0.3.17/configurations/global.json

100 USD = 7337.804971384352 INR

Script (main.js) loaded correctly

```

## Advantages of MetaCall

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-file.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 containing full code [here](https://github.com/metacall/currency-convert-example)