DEV Community

Robin
Robin

Posted on

Trying out V lang, a simple & fast language

A few days ago, I found out about this new programming language V. On their webpage they claim that it is simple, fast, safe and compiled.

In order to try it out you just need to clone the repository 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:

fn split_array(s string) []string {
    return s.split("")
}
Enter fullscreen mode Exit fullscreen mode

You can find the complete documentation here

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.

char_morse_map := {
    'A': '.-',
    'B': '-...',
    'C': '-.-.',
    'D': '-..',
    'E': '.',
    ... 
}
Enter fullscreen mode Exit fullscreen mode

In order to convert a complete sentence to morse you need a simple function that splits and looks up the characters.

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
}
Enter fullscreen mode Exit fullscreen mode

So let's bing everything together...

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'))
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)