2016-08-29

mindstalk: (Default)
I've started learning Scala. Don't know how far I'll get unless someone's willing to hire me to learn it (not impossible, I see a few jobs saying just that.) So far it at least seems like a nice blend of functional programming (ML type inference branch) and practical utility (JVM base, willingness to let you get dirty and imperative.) Though I'm also getting whiffs of C++ complexity... possibly unavoidable if going for that amount of flexibility in both coding style and performance.

It reminded me that I have trouble respecting a language that doesn't have first class functions, which got me wondering about the languages I know well.

C: no.

C++: no, directly, though you can make function objects with classes and operator(). Bit verbose, though. C++11 made improvements.

Java: I don't know, actually. Searching... looks like not really, though Java 8 made improvements.

Perl: Wikipedia says yes, though you hand around references to subroutines, and I found recently that nested functions aren't actually bound to their scope: function 1 defined in function 2 is still callable outside function 2. Wikipedia does say that nested named functions are in Perl 6, vs. the Perl 5 that AFAIK everyone still uses, if they use Perl at all.

Python: pretty much... but as Robbie and I found recently, Python doesn't do proper lexical scoping. And WP says it doesn't really do anonymous nested functions; Guido seems to have a reluctance to embrace FP. (Also see reduce being exiled to a functools library, not that Perl is better, to my surprise.)

So, wow, none of them.

Ruby I know basically nothing of, but WP says no. JavaScript I don't know enough of, though WP says yes. It also says mostly yes for Rust and Go. I'm sad D isn't on the table.

[Feb 2017 edit: JavaScript has better lambdas than Python, but has the same weird scoping problem.]
mindstalk: (escher)
As mentioned in the previous post, I found Perl has problems here:

[Zefiris:0] cat unbound.pl
sub outer {
    sub inner {
        print "inside inner\n";
    }
    print "inside outer\n";
}

outer();
inner();

[Zefiris:0] perl unbound.pl
inside outer
inside inner


Perl's inner() enters the global scope, even from within outer()

Vs.

[Zefiris:0] cat unbound.py
def outer():
    def inner():
        print("inside inner")

    print("inside outer")

outer()
inner()

[Zefiris:0] python unbound.py
inside outer
Traceback (most recent call last):
  File "unbound.py", line 8, in 
    inner()
NameError: name 'inner' is not defined


Python does the scoping I expect, though not any compile time checking for undefined functions. Though Perl doesn't here, either, even with 'use strict;'. Well, I suppose it's possible that in these languages, outer() could have done something to define inner() before it was called.

Profile

mindstalk: (Default)
mindstalk

July 2025

S M T W T F S
  12345
6789101112
13141516171819
20212223242526
272829 3031  

Most Popular Tags

Expand Cut Tags

No cut tags

Style Credit

Page generated 2025-08-21 12:17
Powered by Dreamwidth Studios