DEV Community

loading...
Cover image for Polyglot Programming with MetaCall

Polyglot Programming with MetaCall

siddhantkcode profile image Siddhant Khare Updated on ・4 min read

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 Product. MetaCall allows calling functions, methods, or procedures between multiple programming languages.

MetaCall

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:

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

Enter fullscreen mode Exit fullscreen mode

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.

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

docker pull metacall/core
Enter fullscreen mode Exit fullscreen mode

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

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:

metacall pip3 install requests
Enter fullscreen mode Exit fullscreen mode

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:

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")

Enter fullscreen mode Exit fullscreen mode

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:

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

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

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:

 metacall main.js
Enter fullscreen mode Exit fullscreen mode

We will see the following output:

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

Enter fullscreen mode Exit fullscreen mode

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

Discussion (2)

Collapse
patarapolw profile image
Pacharapol Withayasakpunt

How does it compare to GraalVM? Anyways, looks neat.

Collapse
siddhantkcode profile image
Siddhant Khare Author

GraalVM is focused on performance and is mainly developed in Java. MetaCall is focused on UX experience more than performance and it's written mainly in C/C++. There are other differences like the license (GPL vs Apache 2.0) respectively, but those are the main ones.

We do not reimplement the runtimes, meanwhile, they do it. That's harder to maintain.

Forem Open with the Forem app