Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functional language which sits between Go & Rust #18

Open
tj opened this issue May 7, 2020 · 3 comments
Open

Functional language which sits between Go & Rust #18

tj opened this issue May 7, 2020 · 3 comments
Labels
Program Suggestion for a command-line program

Comments

@tj
Copy link
Owner

tj commented May 7, 2020

This one is obviously a pretty huge task, but I've always been wanting something inbetween Go & Rust, and have yet to find what I'm looking for. I think with LLVM doing the heavy lifting these days it would be feasible to try it out. It could potentially compile to WASM/JS as well.

@tj tj added the Program Suggestion for a command-line program label May 7, 2020
@tj tj changed the title Functional language which compiles to Go Functional language which sits between Go & Rust May 8, 2020
@GavinRay97
Copy link

This would be incredible.

In my mind, you're torn between two worlds:

  • Go, which has less of a learning curve but syntax semantics that lead to pretty hideous and verbose code, and an ecosystem better-suited to developing web services and CLI's
  • Rust, which has an (in my opinion) nicer syntax, but more difficult to master and your code often winds up plagued with .box()/.unbox() and .wrap(), which really detracts from the niceties it has like it's match syntax.

The closest thing in this ballpark I think are Zig are V.

Example Zig code:
https://ziglang.org/

const builtin = @import("builtin");
const std = @import("std");
const fmt = std.fmt;
const io = std.io;

const Op = enum {
    Sum,
    Mul,
    Sub,
};

fn ask_user() !i64 {
    var buf: [10]u8 = undefined;
    std.debug.warn("A number please: ");
    const user_input = try io.readLineSlice(buf[0..]);
    return fmt.parseInt(i64, user_input, 10);
}

fn apply_ops(comptime operations: []const Op, num: i64) i64 {
    var acc: i64 = 0;
    inline for (operations) |op| {
        switch (op) {
            .Sum => acc +%= num,
            .Mul => acc *%= num,
            .Sub => acc -%= num,
        }
    }
    return acc;
}

pub fn main() !void {
    const user_num = try ask_user();
    const ops = [4]Op{.Sum, .Mul, .Sub, .Sub};
    const x = apply_ops(ops[0..], user_num);
    std.debug.warn("Result: {}\n", x);
}

Example V code:
https://vlang.io/

struct Point {
    x int
    y int
}

p := Point{
    x: 10
    y: 20
}


fn main()  {
    println(run(5, sqr)) // "25"
    // Anonymous functions can be declared inside other functions:
    double_fn := fn(n int) int {
        return n + n
    }
    println(run(5, double_fn)) // "10"
    // Functions can be passed around without assigning them to variables:
    res := run(5, fn(n int) int {
        return n + n
    })
}

match os {
    'darwin' { println('macOS.') }
    'linux'  { println('Linux.') }
    else     { println(os) }
}

numbers := [1, 2, 3, 4, 5]
for num in numbers {
    println(num)
}

Zig is low-level though, with no garbage collection, and is meant as a C-replacement and touts direct interop with C/C++ and can be used a build-replacement.

V has both C and JS backends, though is pretty new.

Both of them support WASM as a target.

@tj
Copy link
Owner Author

tj commented May 14, 2020

yeahhh I've seen those! Quite a few seem very close to what I want, maybe I should just accept that the extra 5% is probably not worth the time haha..

@GavinRay97
Copy link

I think there's definitely room for improvement on both of those, and you're TJ-F*ckin'-Holowaychuk!

If you think it would be fun I believe a lot of people would be interested and follow along. Doesn't have to be a production-ready language but it's always neat to see different programming paradigms and experimental languages with new ideas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Program Suggestion for a command-line program
Projects
None yet
Development

No branches or pull requests

2 participants