Live data from Hacker News

Functional programmers need to take a look at Zig

pure-systems.org

11–20 of 163 posts

Re: Functional programmers need to take a look at Zig

#11

Do you really prefer this: fn Maybe(comptime T: type) type { return union(enum) { value: T, nothing, const Self = @This(); pub fn just(the_val: T) Self { return .{ .value = the_val }; } pub fn nothing() Self { return .nothing; } } } Over this? data Maybe a = Just a | Nothing

Optionals handle this in zig:

  var value: ?T = null;
Write:

  value = 10;
Read:

  if (value) |x| x+=1

Re: Functional programmers need to take a look at Zig

#13

Do you really prefer this: fn Maybe(comptime T: type) type { return union(enum) { value: T, nothing, const Self = @This(); pub fn just(the_val: T) Self { return .{ .value = the_val }; } pub fn nothing() Self { return .nothing; } } } Over this? data Maybe a = Just a | Nothing

This looks like an example of a low level language vs a high level language (relatively speaking). The low level language makes a lot more of what is going on underneath explicit compared to the higher level language which abstracts that away for a common pattern. Presumably that explicitness allows for more control and/or flexibility. So apples to oranges?

Re: Functional programmers need to take a look at Zig

#14
post #11

Do you really prefer this: fn Maybe(comptime T: type) type { return union(enum) { value: T, nothing, const Self = @This(); pub fn just(the_val: T) Self { return .{ .value = the_val }; } pub fn nothing() Self { return .nothing; } } } Over this? data Maybe a = Just a | Nothing

Optionals handle this in zig: var value: ?T = null; Write: value = 10; Read: if (value) |x| x+=1

Came to say this. Early in my career I really thought implementing Maybe in any language is necessary but not I know better. Use the idioms and don’t try to make every language something it’s not.

Re: Functional programmers need to take a look at Zig

#15

Isn't the whole point of abstraction to not care about whats underneath unless you really have to? But ideally, you don't because the abstraction is "good enough"? I haven't heard anyone writing code in Elixir complain about performance issues.

What’s up with the last paragraph? Nobody is complaining because the BEAM is good enough for the typical use case?

Re: Functional programmers need to take a look at Zig

#16

Do you really prefer this: fn Maybe(comptime T: type) type { return union(enum) { value: T, nothing, const Self = @This(); pub fn just(the_val: T) Self { return .{ .value = the_val }; } pub fn nothing() Self { return .nothing; } } } Over this? data Maybe a = Just a | Nothing

This looks like an example of a low level language vs a high level language (relatively speaking). The low level language makes a lot more of what is going on underneath explicit compared to the higher level language which abstracts that away for a common pattern. Presumably that explicitness allows for more control and/or flexibility. So apples to oranges?

I don't think so, where's the extra information in the Zig example?

In Rust, which is arguably also a low level language, it looks like this:

    enum Option {
        None,
        Some(T),
    }

Re: Functional programmers need to take a look at Zig

#17
post #11

Do you really prefer this: fn Maybe(comptime T: type) type { return union(enum) { value: T, nothing, const Self = @This(); pub fn just(the_val: T) Self { return .{ .value = the_val }; } pub fn nothing() Self { return .nothing; } } } Over this? data Maybe a = Just a | Nothing

Optionals handle this in zig: var value: ?T = null; Write: value = 10; Read: if (value) |x| x+=1

Sure, but this is an example from the article, and pertains to sum types in general, not just Maybe.

Re: Functional programmers need to take a look at Zig

#20
post #10

A functional programmer who casts away proper sum types and pattern matching is no functional programmer at all

I thought lisps were all functional programming, and lack sum types and pattern matching?

In which case, what's the term for the "proper sum types and pattern matching" flavour of things?

Post reply on HN