HN2new | past | comments | ask | show | jobs | submitlogin

> Multi-paradigm. pure-functional, concurrent-actor, imperative-procedural, OO.

Can sombody please explain how "pure-functional" (where I think that "pure" means "without side effects") mixes with "imperative"?

Or do I misunderstand the "pure" part?



A function has an effect. It can be annotated as 'impure' or 'unsafe'. Any function that is not annotated in that way is considered pure.

Pure functions that return a boolean value can be used in the typestate system (ie. typestate calls must not have side effects). 'pure' functions cannot call 'impure' functions, etc.

I'm guessing that's what the quoted sentence refers too.


As long as you don't mix your functional code with imperative code you can stay pure.


To me, multi-paradigm always was about mixing the styles, so this sounds a like quite a radical restriction, and a bit weird.


Well, inside a function, you can have as much imperative code as you like, with mutable local variables and what-not, the function overall is still pure as long as it does not cause side-effects.

Eg, functional code could looks something like this:

    foo a b = if a < b then quux (map bar a) else b
while a pure imperative function could look something like this:

    int foo (string a, int b) { // since a is passed by value, mutating it below will not modify the original
        if (a < b) {
            for (int idx=0; idx<a.length; ++idx) {
                a[idx] = bar(a[idx]);
            }
            return quux(a);
        } else {
            return b;
        }         
    }
Obviously this example is contrived and dumb, but both do the same thing and, besides the syntax difference, the first is written in a functional style, the second in an imperative style, but both are still pure (as long as bar and quux are also pure).

So you can mix functional and imperative without making functions impure.


Yes, but you don't have to mix them each and every time. It's just that you have the option to do so and aren't constrained to a single language designer's favourite style.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: