4 ms·
Or even Perl: sub deriv { my $f = shift; my $dx = 0.0001; return sub { my $x = shift; return ( &$f( $x + $dx ) - &$f( $x ) ) /
by acqq 14y ago
Or even Perl:
sub deriv { my $f = shift;
my $dx = 0.0001;
return sub { my $x = shift;
return ( &$f( $x + $dx ) - &$f( $x ) ) / $dx; }
}
sub cube { return $_[0] * $_[0] * $_[0]; }
print( deriv( \&cube )->( 2.0 ) );
12.0006000100226
--------
"Lisp has all the visual appeal of oatmeal with fingernail clippings mixed in." -- Larry Wall (but see also http://www.perl.com/pub/2000/01/10PerlMyths.html#Perl_looks_like_line_noise http://www.perl.com/pub/2000/01/10PerlMyths.html#Perl_looks_...)
- draegtun 14y agoor even TMTOWTDI :) Here in perl5i (https://metacpan.org/module/perl5i https://metacpan.org/module/perl5i): func deriv ($f) { my $dx = 0.0001; func ($x) { ($f->($x + $dx) - $f->($x)) / $dx }; } my $cube = func ($x) { $x ** 3 }; say deriv($cube)->(2); And also in perl6: sub deriv (Code $f) { my $dx = 0.0001; -> $x { ($f($x + $dx) - $f($x)) / $dx }; } my $cube = -> $x { $x ** 3 }; say deriv($cube)(2);
- flyinRyan 14y agoLarry Wall must be one of the most ironic people in the open source world, on purpose or otherwise. He also made a statement like "Lua doesn't have real OO, they just use a hash with a magic function" or something like that....
- draegtun 14y agoThe actual statement Larry Wall gave wasn't that bad... I thought Perl 5 had an oversimplified object system till I saw Lua. In Lua, an object is just a hash, and there's a bit of syntactic sugar to call a hash element if it happens to contain code. Thats all there is. They don't even have classes. Anything resembling inheritance has to be handled by explicit delegation. That's a choice the designers of Lua made to keep the language very small and embeddable. For them, maybe it's the right choice. ref: http://www.perl.com/pub/2007/12/06/soto-11.html http://www.perl.com/pub/2007/12/06/soto-11.html Also IIRC Wall's original Lisp quote starts with him praising Lisp and then ending with this infamous|humorous quip.
- flyinRyan 14y agoThis description sounds to me almost exactly like what Perl does with it's OO (Perl's reall OO. Moose and all that are libraries built on top of Perl).
- rjbond3rd 14y agoBut even Perl's barebones (non-Moose) OO has inheritance. Once you add in Moose, you now have an API to your OO which is unusual. Personally, I think Lua is amazing. Rolling your own OO system in the language itself is pretty awesome, if you like that sort of thing. Everything has to be implemented in -something-, right?
- draegtun 14y agoIt's very similar in that they both expose the OO inner workings but after that they're quite different. Perl requires a blessed reference (which doesn't have to be a hash but often it is the best option!) which only contains the objects state. So unlike a Lua object hash the behaviour is maintained by Perl in dynamically scoped packages/subroutines (classes/methods).