Foldr Zen Zone

MaSH

Basic Types

In MaSH a simple variable can be one of several basic types. A variable’s type determines what we can do with it. For example you can perform arithmetic operations on a number but not on a string of text.

Strings

A string is a piece of data which contains a sequence of characters which form some text. So that MaSH can recognise where your string begins and ends its contents must be surrounded by quotation marks ” “.

Natural

mash
set myString to "Hello from mash"

Standard

mash
myString = "Hello from mash"

Interpolation

String or variable interpolation allows us to construct strings of text which contain other variables. An example of this would be creating a message for a user, inserting details such as their name before sending it to them. In MaSH you can insert other variables into a string using two curly braces {{ }} (also known as mustache templating). The following script shows a very simple example of doing this.

Natural

mash
set username to "grace.hopper@foldr.io"
set message to "Hello {{username}}"

printline message

Standard

mash
username = "grace.hopper@foldr.io"
message = "Hello {{username}}"

printline(message)

Output

output
Hello grace.hopper@foldr.io

Note how we first declare our username as a separate variable and then reference it inside our message by placing it inside the double braces. Subsequently when message is used, MaSH looks up the value of the username variable and inserts it for us.

Numbers

A number in MaSH is, well, a number! They can be positive, negative, whole or decimal.

Natural

mash
set myNumber to -20.5

Standard

mash
myNumber = -20.5

Booleans

Booleans can contain either true or false.

Natural

mash
set myBoolean to true

Standard

mash
myBoolean = true

Arrays

Arrays are lists of values which are stored together and accessible using a single variable name. You construct an array by surrounding your values with square brackets [ ] and separating them with commas , .

Natural

mash
set myArray to ["apple", "banana", "coconut"]

Standard

mash
myArray = ["apple", "banana", "coconut"]

Arrays can also contain other variables. For example…

Natural

mash
set a to "apple"
set b to "banana"
set c to "coconut"

set myArray to [a, b, c]

Standard

mash
a = "apple"
b = "banana"
c = "coconut"

myArray = [a, b, c]

We can retrieve a value from an array using its index or its position within the array. Array indexes start from 0 rather than 1 so to retrieve the first value from our array we would write myArray[0].

Natural

mash
set a to "apple"
set b to "banana"
set c to "coconut"

set myArray to [a, b, c]

printline myArray[0]

Standard

mash
a = "apple"
b = "banana"
c = "coconut"

myArray = [a, b, c]

printline(myArray[0])

Output

output
apple

Ranges

Sometimes we need to create an array containing a series of numerical values. We can do this like so:-

Natural

mash
set myValues to [1,2,3,4,5,6,7,8,9,10]

printline myValues

Standard

mash
myValues = [1,2,3,4,5,6,7,8,9,10]

printline(myValues)

Output

output
Array [
	1,
	2,
	3,
	4,
	5,
	6,
	7,
	8,
	9,
	10
]

Remember though that we’re always trying to find ways to write less code. Ranges allow us to quickly create an array containing a set of values. The following script produces the same output as the previous but is far more concise.

Natural

mash
set myValues to 1...10

printline myValues

Standard

mash
myValues = 1...10

printline(myValues)

Output

output
Array [
	1,
	2,
	3,
	4,
	5,
	6,
	7,
	8,
	9,
	10
]

If we would like our values to begin at zero we can leave off the first number when we create our range.

Natural

mash
set myValues to ...10

printline myValues

Standard

mash
myValues = ...10

printline(myValues)

Output

output
Array [
	0,
	1,
	2,
	3,
	4,
	5,
	6,
	7,
	8,
	9,
	10
]

Dictionaries

Dictionaries are similar to arrays but instead of being indexed numerically they can be indexed using a string of text. They are a useful way of grouping related information within your scripts. Data is added to dictionaries as key-value pairs. For every piece of data that you add to a dictionary you must also supply a key by which it will be accessed. Dictionaries are constructed using curly braces { and } , keys and values are separated with a colon : and multiple pairs are separated with a comma , . The following script shows our keys are name and occupation and our values are “Grace Hopper” and “Computer Scientist” respectively.

Natural

mash
set myDictionary to { name: "Grace Hopper", occupation: "Computer Scientist" }

Standard

mash
myDictionary = { name: "Grace Hopper", occupation: "Computer Scientist" }

Like arrays dictionaries can also contain other variables.

Natural

mash
set fullName to "Grace Hopper"

set myDictionary to { name: fullName, occupation: "Computer Scientist" }

printline myDictionary

Standard

mash
fullName = "Grace Hopper"

myDictionary = { name: fullName, occupation: "Computer Scientist" }

printline(myDictionary)

Output

mash
Dictionary {
	"name": "Grace Hopper",
	"occupation": "Computer Scientist"
}

Remember we said that dictionary values are stored as key-value pairs? Let’s go ahead and retrieve a value from our dictionary using its key. We do this exactly as we do when retrieving values from arrays but we use the key instead of the index.

Natural

mash
set fullName to "Grace Hopper"

set myDictionary to { name: fullName, occupation: "Computer Scientist" }

printline myDictionary["name"]

Standard

mash
fullName = "Grace Hopper"

myDictionary = { name: fullName, occupation: "Computer Scientist" }

printline(myDictionary["name"])

Output

mash
Grace Hopper

And just like arrays we can also set values inside dictionaries too. If we provide a new key which does not exist within the dictionary then it will be inserted. If we use an existing key then its value will be overwritten.

Natural

mash
set fullName to "Grace Hopper"
set myDictionary to { name: "Grace Hopper", occupation: "unknown" }

# Give Grace a career

set myDictionary["occupation"] to "Computer Scientist"

# And add details of her place of birth
set myDictionary["born"] to "New York"

printline myDictionary

Standard

mash
fullName = "Grace Hopper"
myDictionary = { name: "Grace Hopper"}

# This value will be changed
myDictionary["occupation"] = "Computer Scientist"

# This value will be inserted
myDictionary["born"] = "New York"

printline(myDictionary)

Output

mash
Dictionary {
	"name": "Grace Hopper",
	"occupation": "Computer Scientist",
	"born": "New York"
}

Array to Dictionary conversion

Behind the scenes arrays and dictionaries in MaSH are very similar and can be used in much the same way. Note that if you make a change to an array that does not follow the current array structure, inserting a value for a key which does not exist for example, the array will be converted to a dictionary.

Natural

mash
set myArray to ["a", "b", "c"]

set myArray[4] to "d"

# MaSH automatically converts myArray to a dictionary

printline myArray

Standard

mash
myArray = ["a", "b", "c"]

myArray[4] = "d"

#myArray is now a dictionary

printline(myArray)

Output

mash
Dictionary {
	"0": "a",
	"1": "b",
	"2": "c",
	"4": "d"
}

Empty variables – null

A variable which has no value and type is considered null. Whilst this may seem strange it becomes useful as your scripts get more complex.

Natural

mash
# Even though this string is empty (it contains no characters) it is still considered a string

set myString to ""

# This is still a number even though its value is 0

set myNumber to 0

# This variable is type-less and empty

set myNullVariable to null

Standard

mash
# Even though this string is empty (it contains no characters) it is still considered a string

myString = ""

# This is still a number even though its value is 0

myNumber = 0

# This variable is type-less and empty

myNullVariable = null
Every journey begins with a single step

There can be many paths to a desired document. Let Foldr be your guide, wherever the destination...

Find File Zen