Polyglot with Open Source and Serverless
Tags: programming, openwhisk, ibm, serverless
Published: 2019-08-03 18:44:55

---
title: Polyglot with Open Source and Serverless
published: true
tags: programming,openwhisk,ibm,serverless
canonical_url: 
---

I often mentor at hackathons as part of my job as a developer advocate at IBM and one of the common problems teams face is which language to pick for their solution. Generally the person who originally thought of the idea will decide on a language. That is better than analysis paralysis, however there is a better way! If you can divide your application into multiple services and/or functions, you can use a polyglot framework such as openwhisk.

> The OpenWhisk platform supports a programming model in which developers write functional logic (called [**Actions**](https://github.com/apache/incubator-openwhisk/blob/master/docs/actions.md#openwhisk-actions)), in any supported programming language, that can be dynamically scheduled and run in response to associated events (via [**Triggers**](https://github.com/apache/incubator-openwhisk/blob/master/docs/triggers_rules.md#creating-triggers-and-rules)) from external sources ( [**Feeds**](https://github.com/apache/incubator-openwhisk/blob/master/docs/feeds.md#implementing-feeds)) or from HTTP requests.

Using such a framework enables you to

1. Write multiple functions in parallel increasing the effectiveness of the team.
2. Use multiple languages ensuring everybody is able to participate to the best of their ability.

If you are new to OpenWhisk architecture, let’s do a quick crash course crash💥!

- Action → your code that runs on the serverless platform
- Trigger → fired when an external event occurs
- Rule → connects an trigger to an action
- Package → bundle or modularize your actions, triggers and rules and share with other code

Let’s recreate this simple example provided by [James Thomas](https://medium.com/u/604ac4ebfc5f) in his fabulous [tutorial/bootcamp on OpenWhisk](https://github.com/IBM-Cloud/openwhisk-workshops/tree/master/bootcamp?cm_mmc=OSocial_Blog-_-Developer_IBM+Developer-_-WW_WW-_-OInfluencer-Medium-USL-openwhisk-workshop-sequence&cm_mmca1=000037FD&cm_mmca2=10010797). [Exercise 1.2](https://github.com/IBM-Cloud/openwhisk-workshops/tree/master/bootcamp?cm_mmc=OSocial_Blog-_-Developer_IBM+Developer-_-WW_WW-_-OInfluencer-Medium-USL-openwhisk-workshop-sequence&cm_mmca1=000037FD&cm_mmca2=10010797) provides an example of action sequence, which simply is an action calling another action, which in turn calls a third action. The results of one action are passed onto the next one. If there is an error, the whole sequence errors out. Instead of implementing the whole sequence in a single language, we will use Javascript, Python and Swift! How cool is that! Here are are requirements

1. Split: should take in a string and return an array of words split by space. We will write this function in **Javascript**.
2. Reverse: should take in an array and return a new array that is reverse of the input array. We will write this function in  **Swift**.
3. Join: takes in an array and returns a new array that is the space delimited join of the elements. We will write this function in  **Python**.

For example, if I pass in “hello world” to this sequence, I will get back “world, hello”. What do these functions looks like?

#### Create Actions

**splitjs Javascript action**

```
function main(params) {
	var text = params.text || "";
	console.log(`incoming text: ${text}`)
	var words = text.split(' ');
	return {words: words}
}
```

**reverseswift Swift action**

```
struct Input: Codable {
    var words: [String]?
}
struct Output: Codable {
    let message: [String]
}
func main(param: Input, completion: (Output?, Error?) -> Void) -> Void {

    var result = Output(message: [] )
    
    if let words = param.words{
        result = Output(message: words.reversed() )
    }
    completion(result, nil)
}
```

**joinpy Python action**

```
import sys

import json

def main (dict):

  print ('dict ' + json.dumps(dict))

  if "message" in dict:

     return {'message': ', '.join(dict['message'])}

  else:

     return { 'message': [] }
```

That’s it! The following three lines **_deploy_** these code snippets as actions on _openwhisk._

```
wsk action create splitjs splitjs.js
wsk action create reverseswift reverseswift.swift
wsk action create joinpy joinpy.py
```

Let’s test these out now.

#### Testing our actions individually

**Test split javascript action**

```
$ wsk action invoke splitjs -r -p text "hello world"


{  
 "words": [  
 "hello",  
 "world"  
 ]  
}
```

**Test reverse swift action**

```
$ wsk action invoke reverseswift -r -p words '["hello","world"]'


{  
 "message": [  
 "world",  
 "hello"  
 ]  
}
```

**Test join python action**

```
$ wsk action invoke joinpy -r -p message '["world", "hello"]'


{  
 "message": "world, hello"  
}
```

#### Creating a sequence

We can create a sequence

splitjs → reverseswift → joinpy

```
wsk action create split-reverse-join --sequence splitjs,reverseswift,joinpy
```

Finally, let’s test it out!

```
$ wsk action invoke split-reverse-join -r -p text "Hello World"


{  
 "message": "World, Hello"  
}
```

How cool is that! Let’s all celebrate with a Chandler dance!

![](https://cdn-images-1.medium.com/max/480/1*QCks4cIzllXVqUltOeecpw.gif)

If you are around the bay area, join me for a [hands-on workshop on OpenWhisk](https://www.meetup.com/IBM-Developer-SF-Bay-Area-Meetup/events/263172151/) on 08/08/19. You [can register here](https://www.meetup.com/IBM-Developer-SF-Bay-Area-Meetup/events/263172151/). [IBM Developer](https://medium.com/u/262975298e3a) and the API wizard [Max Katz](https://medium.com/u/bf01a11701fe) will be around.