Trying out V lang, a simple & fast language
Tags: vlang, programming, c
Published: 2023-01-29 15:12:36

A few days ago, I found out about this new programming language V. On their [webpage](https://vlang.io/) they claim that it is simple, fast, safe and compiled.

In order to try it out you just need to clone the [repository](https://github.com/vlang/v) and compile it with _make_.

V is a typed language, so for a function you must specify a type for both the parameters and the return value. A basic function with a string input and array output would be like this:

```C
fn split_array(s string) []string {
	return s.split("")
}
```

You can find the complete documentation [here](https://github.com/vlang/v/blob/master/doc/docs.md)


## Small Project


So I wanted to implement a small morse code conversion script.
To translate characters to morse code and vice-versa, you need a hash map that maps the characters to their morse code.

```C
char_morse_map := {
	'A': '.-',
	'B': '-...',
	'C': '-.-.',
	'D': '-..',
	'E': '.',
	...	
}
```
In order to convert a complete sentence to morse you need a simple function that splits and looks up the characters.

```C
fn translate(chars []string, mapping map[string]string) []string {
	mut output := []string{}
	for s in chars {
		if s == ' ' {
			output << s
			continue
		}
		output << mapping[s]
	}
	return output
}
```

So let's bing everything together...

```C
fn char_to_morse(input string) []string {
	char_morse_map := {
		'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.', 'G': '--. ', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--', 'Z': '--..', ',': '--..--', ':': '---...', ';': '-.-.-.', '.': '.-.-.-', '"': '.-..-.', '(': '-----.', ')': '.-----', '1': '.----', '2': '..---', '3': '...-- ', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.', '0': '-----'
	}
	return translate(input.to_upper().split(""), char_morse_map)
}

fn morse_to_char(input []string) []string {
	morse_char_map := {
		'.-': 'A', '-...': 'B', '-.-.': 'C', '-..': 'D', '.': 'E', '..-.': 'F', '--. ': 'G', '....': 'H', '..': 'I', '.---': 'J', '-.-': 'K', '.-..': 'L', '--': 'M', '-.': 'N', '---': 'O', '.--.': 'P', '--.-': 'Q', '.-.': 'R', '...': 'S', '-': 'T', '..-': 'U', '...-': 'V', '.--': 'W', '-..-': 'X', '-.--': 'Y', '--..': 'Z', '--..--': ',', '---...': ':', '-.-.-.': ';', '.-.-.-': '.', '.-..-.': '"', '-----.': '(', '.-----': ')', '.----': '1', '..---': '2', '...-- ': '3', '....-': '4', '.....': '5', '-....': '6', '--...': '7', '---..': '8', '-----': '0', '----.': '9'
	}
	return translate(input, morse_char_map)
}

fn translate(chars []string, mapping map[string]string) []string {
	mut output := []string{}
	for s in chars {
		if s == ' ' {
			output << s
			continue
		}
		output << mapping[s]
	}
	return output
}

println(char_to_morse('V lang is amazing'))
```

The result would be: ...-   .-.. .- -. --.    .. ...   .- -- .- --.. .. -. --. 


## Conclusion
I think V is pretty interesting language that has a lot of potential. It even comes with a lot of features like:

- Hot code reloading
- Support for graphics libraries
- GUI library
- Cross compilation
- Cross-platform shell scripts
- Built-in code profiler
- Web framework

To visualize the speed of V there is a small example that shows the fast build compiler. Compared to other languages it is amazingly fast.

```
        Space           Build time
Go      525 MB          1m 33s
Rust    30 GB           45m
GCC     8 GB            50m
Clang   90 GB           60m
Swift   70 GB           90m
V       < 10 MB         <1s
```

All in all I can recommend you to try it yourself, setting up takes just a few seconds.