pavlog

ウェブエンジニアがあれやこれやを書きます

【プログラミング言語】モダンなV言語がリリースされたので触ってみる【シンプル且つ高速】

f:id:paveg:20190623140044p:plain

プログラミングしてますか?僕は都内在住のエンジニアで、最近は主にRuby/Go/TypeScriptを触っています。

趣味で競技プログラミングを始めたのでC++も触るようになりました。

そんな僕ですが、6/22にリリースされたV言語に興味を持っています!

最近だとかなりプロダクションコードでも採用されるのを聞くようになったGo言語がありますが、さらにモダンな言語です。

vlang.io

このV言語について公式は以下のように言っています。

Simple, fast, safe, compiled language for developing maintainable software

V言語(The V Programming Language)とは

V is a statically typed compiled programming language designed for building maintainable software. It's similar to Go and is also influenced by Oberon, Rust, Swift.

V is a very simple language. Going through this documentation will take you about half an hour, and by the end of it you will learn pretty much the entire language.

Despite being simple, it gives a lot of power to the developer. Anything you can do in other languages, you can do in V.

出典: https://vlang.io/docs#introduction

...

V言語は、保守可能なソフトウェアの構築のために設計された静的型付言語です。 Goに似ていて、Oberon, Rust, Swiftの影響を受けています。 V言語はとてもシンプルなプログラミング言語です。 30分かけてドキュメントを読めば、学び終えることができます。 単純な言語ですが、他の言語でできることは全てV言語で行うことが可能です。

このV言語は、そもそもデスクトップアプリのVoltを作成するために開発された言語です。 現在開発はAlex Medvednikovによって行われていますが、パッチを出すことも可能です(僕もいくつかパッチを投げました)。 ライセンスにはMITライセンスが採用されています。

インストール手順

$ ghq get https://github.com/vlang/v # vlang/vをcloneする
$ cd v/compiler
$ wget https://vlang.io/v.c # v.cをwgetで取得
$ cc -w -o vc v.c

...

ダメなようです。どうやらディレクトリの参照が誤っている模様。

$ ./vc -o v .
errno=2 err='No such file or directory'
V panic: -o doesnt exist

正しい方法で再度トライしてみる

$ mkdir -p ~/code
$ cd ~/code
$ git clone https://github.com/vlang/v
$ cd v/compiler
$ wget https://vlang.io/v.c
$ cc -w -o vc v.c
$ ./vc -o v .

最後にシンボリックリンクを貼ります。

sudo ln -s $HOME/code/v/compiler/v /usr/local/bin/v

起動してみる

$ v
V 0.0.12
Use Ctrl-D to exit
For now you have to use println() to print values, this will be fixed soon

# Hello Worldを試す
>>> println('Hello World')
Hello World

無事HelloWorldが出力できました!

V言語の嬉しい部分と特徴

特徴

公式サイトに書かれている特徴は、全部入り感があり端的に言って「俺の考える最強のプログラミング言語」っぽいです。

  • C言語と同等のパフォーマンス
  • 高速なコンパイル
  • null / global variable / undefined value などが存在しない
  • Genericsを持つ
  • default Immutable variables
  • default Immutable structs
  • パワフルなグラフィックライブラリ
  • ネイティブなクロスプラットフォームGUIライブラリ
  • 簡単にクロスコンパイルが可能
  • 容易な依存管理と楽なデプロイ

translating C/C++ to V

translating C to V will be available in June. C++ to V will be available later this year.

まだ未実装の機能ではありますが、 C/C++をVへと翻訳することが可能になります。

実行には以下のコマンドを実行します。

$ v translate test.cpp

すると、 test.v が出力されます。

https://vlang.io/docs#cpp

Hot code reloading

詳細については書かれていませんが、いちいちコンパイルする操作が面倒なのであると嬉しいなという気持ちがあります。

その他の機能

便利なところはたくさんありますが、今回は導入記事のため説明を省きます。

軽く書いてみる

C++ で書いていたコードをVに書き換えてみました。

f:id:paveg:20190623140112p:plain

fn gcd(x, y int) int {
    return if (y != 0){gcd(y, x % y)} else {x}
}

fn lcm(x, y int) int {
    return (x / gcd(y, x % y) * y)
}

fn f(x, c, d int) int {
    mut result := x
    result -= (x / c)
    result -= (x / d)
    result += (x / lcm(c, d))
    return result
}

fn main() {
    a := 10
    b := 40
    c := 6
    d := 8
    mut ans := 0
    ans = f(b, c, d) -f(a - 1, c, d)
    println(ans)
}

// output: 23

これは以下のC++の書き換えです。

#include "bits/stdc++.h"

using namespace std;
typedef long long ll;
const ll inf = (1 << 30) - 1;
const ll infll = (1LL << 61) - 1;
const ll mod = 1e9 + 7;
#define REP(i, o, n) for (ll i = o; i < n; i++)
#define rep(i, n) REP(i, 0, n)

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

ll lcm(ll a, ll b) { return a / gcd(b, a % b) * b; }

ll f(ll x, ll c, ll d) {
    ll res = x;
    res -= x / c;
    res -= x / d;
    res += x / lcm(c, d);
    return res;
}

void actual() {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    ll ans = 0;

    ans = f(b, c, d) - f(a - 1, c, d);
    cout << ans << endl;
}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    actual();
    return 0;
}
// input 10 40 6 8
// output: 23

冗長かなと思いましたが、Goに近い文法で書きやすいですね。 標準ライブラリと速さ次第では、全然採用したい。

まとめ

もう少し機能が出揃ってから、V言語で何か作ってみようかなと思える感じでした。

会社のSlackチャンネルにこっそりvlangというチャンネルを開設してますし、気になったことはここで話したりTwitterでも書いていこうかなと思ってますのでそちらもぜひ!

まずは、インストールして触ってみることをお勧めします!

追記

dockerfileを作成したので使ってみてください。

github.com