Closed Thread Icon

Topic awaiting preservation: Did you know that? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=23081" title="Pages that link to Topic awaiting preservation: Did you know that? (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Did you know that? <span class="small">(Page 1 of 1)</span>\

 
Iron Wallaby
Paranoid (IV) Inmate

From: USA
Insane since: May 2004

posted posted 08-26-2004 14:57

I like Javascript lots.

code:
var foo = [function(a) { return 2 * a; }, function(a) { return a * a; }];
document.write(foo[0](5));

Works just dandy.

code:
var bar = { fred: function(a) { return 2 * a; }, wilma: function(a) { return a * a; } };
document.write(bar["wilma"](7));

Works great too.

Heck,

code:
var baz = {
fred: [function(a) { return a; }, function(a) { return 2*a; }] ,
wilma: [function(a) { return a*a; }, function(a) { return 2*a*a; }] ,
barney: [function(a) { return a*a*a; }, function(a) { return 2*a*a*a; }] ,
betty: [function(a) { return a*a*a*a; }, function(a) { return 2*a*a*a*a; }]
};

document.write(baz["barney"][1](7));

Works wonderfully too.

Then, also possible is:

code:
function foo(n) {
return function(i) { return n += i; }
}

document.write(foo(5)(3));

o.o!!!

Finally:

code:
function foo(n) {
return function(i) { return [function(j) { return n * i * j; }, function(a) { return n += i += j; }]; }
}

document.write(foo(5)(3)[0](2));

Works perfectly as well.

I'm starting to think that Javascript is just as powerful and concise as Perl, which is a scary thought indeed... I have more experimentation to do...

"Any sufficiently advanced technology is indistinguishable from magic." -- Arthur C. Clarke
"Any sufficiently arcane magic is indistinguishable from technology." -- P. David Lebling

kastner
Nervous Wreck (II) Inmate

From:
Insane since: Aug 2004

posted posted 08-26-2004 16:22

I didn't know it had anonymous functions - who wants to write a lisp interpreter in JS?

liorean
Obsessive-Compulsive (I) Inmate

From: Umeå, Sweden
Insane since: Sep 2004

posted posted 09-04-2004 04:04

Waldemar Horwat once called JavaScript for "another syntax for CommonLISP". And he should know, he's the editor of the ECMA-262 standard and one of those that truly understand how it works, inside out.

If you want to see something wicked have a look in the mozilla source trees (on lxr.mozilla.org if you want to access it from your browser) and find Narcissus somewhere there. It's an implementation of JavaScript in JavaScript, made for the Mozilla SpiderMonkey JavaScript engine (the one written in C). Or for meassures check out the CommonLISP implementation there. It's the testing ground for additions/improvements to the specification.



Oh, and have you seen Loell? That's also a piece of work in JavaScript, I tell you...

--
David "liorean" Andersson
<http://codingforums.com/member.php?u=5798>

"Any technology distinguishable from magic is insufficiently advanced"

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-04-2004 04:57

ECMA script is a powerful language. I wish it were used for more than scripting on web pages.


 

liorean
Nervous Wreck (II) Inmate

From: Umeå, Sweden
Insane since: Sep 2004

posted posted 09-04-2004 16:18

Oh, ECMAScript is used in a fair number of other situations. The Mozilla browsers are made partially in ECMAScript. The ActionScript implementation found in Macromedia Flash is ECMAScript. Adobe PDF supports EScript scripting which is ECMAScript based. The Adobe SVG Viewer is of course EScript powered. ASP has JScript as a server side language. The Windows Scripting Host supports JScript. Microsoft's .NET platform has JScript.NET (a partial implementation of the upcoming ECMA-262 4ed standard). Avalon in Longhorn will support JScript.NET as one of it's primary scripting languages. On the Mac we have Konfabulator and the upcoming Apple Dashbord as widget facilities. The Apache foundation uses Mozilla's Rhino engine for some of their projects. The Sun/Netscape ONE server platform allows JavaScript as a server side scripting language. Caucho Resin allows compilation of JavaScript into Java bytecode. And I'm sure there are other places it's used that I haven't thought of.

(Edited by liorean on 09-04-2004 16:19)

lallous
Maniac (V) Inmate

From: Lebanon
Insane since: May 2001

posted posted 09-09-2004 09:18

Slime, yes, I wish that too...the freedom it gives you while programming, as compare to high level languages.

IronW, nice topic. From write(foo(5)(3)) and below, these things are new to me.

--
Regards,
Elias

Hugh
Paranoid (IV) Inmate

From: Dublin, Ireland
Insane since: Jul 2000

posted posted 09-09-2004 23:11

IW: Nope, no, I didnt know.

Interesting enough , although I can't imagine a use for calling functions in those ways. I wont ever use anything more complicated than your first example:
var foo = [function(a) { return 2 * a; }, function(a) { return a * a; }];

Never really heard of LISP much, but it seems JS can do nearly all of its features:
http://www.paulgraham.com/diff.html

Including recursion, which I didnt think would work but had never tried until now:

function isPalindrome(str) {
if(str.length>1) {
if(str.charAt(0)==str.charAt(str.length-1)) return isPalindrome(str.substring(1,str.length-1));
else return false;
}
return true;
}


[ isPalindrome("rotator") should return true.]

The fact that functions can be treated as variables makes a lot of sense considering, JS variables arent stored in your memory like in C etc.. just as a collection of variables/objects within the browser's memory. Similar to a scripting language like PERL mentioned above, I would think. The fact that you can't access memory slots directly means the code isnt run "natively" for lack of a better word. i.e Its not sent to the processor directly as it is interpreted. This holds true for java also. Does anyone know if this array of functions malarky is good in java too ? (dont have a compiler on me to test it.)

liorean
Nervous Wreck (II) Inmate

From: Umeå, Sweden
Insane since: Sep 2004

posted posted 09-09-2004 23:55

Actually, the only concepts used in those examples are untyped arrays and first class functions. Two of a number of concepts that originated in LISP. However, recursion is a concept that JavaScript doesn't do right - JavaScript is not properly tail recursive. A properly tail recursive language performs stack call elimination, meaning that calls are not placed on a stack of wrapped function calls, but instead serially executed. See Wikipedia: Tail Recursion for more info.

What does this effectively mean? Well, it means that a call like this:

code:
circ(){
blah;
return circ();
}

would be equivalent to the following

code:
while(true)
blah;



And what does that mean? Well, it means that the recursive statement will run in constant memory instead of nesting function calls in memory.

As for variables, it's half true. Both Java and JavaScript have the primitive/compound distinction for values. It makes the handling inconsistent in both cases, but for good reasons at least in the JavaScript case, I would say.

JavaScript also retains the statement/expression distinction.

--
var Liorean = {
prototype: JavaScriptGuru.prototype,
abode: "http://codingforums.com/",
profile: "http://codingforums.com/member.php?u=5798"};

mobrul
Bipolar (III) Inmate

From:
Insane since: Aug 2000

posted posted 09-10-2004 01:09

Keeping with Asylum tradition, I'm tangentializing.
I have but a vague idea about what is Lisp. It is a programming language. It's especially suited for processing lists. It has some sort of close relationship with Emacs. That's the extent of my knowledge.
So...
What in the hell is it good for? Do people actually use it these days? Why? For what?

Lord_Fukutoku
Paranoid (IV) Inmate

From: Back in West Texas... How disappointing
Insane since: Jul 2002

posted posted 09-10-2004 01:47

The only Lisp I've seen was when I was working in Iowa this summer. They just bought Auto-CAD2004, with an add-on called AutoCrete, which was written in AutoLisp. I poked around in the source a bit because I was curious, but either there was a lot more that I couldn't find, or I don't understand how it works at all (which is entirely possible). But the whole add-on was poorly written, with hundreds of bugs, a lot of which were merely irritating, but some a lot worse.

Lisp links:
Tutorial - http://www.afralisp.com/tutor.htm
Info and stuff - http://xarch.tu-graz.ac.at/autocad/lisp/#lisp

Iron Wallaby
Paranoid (IV) Inmate

From: USA
Insane since: May 2004

posted posted 09-10-2004 02:18

Lisp is a powerful programming language that is functional, instead of procedural or object-oriented. It's code is easily identiable by the ridiculous number of parenthesis it uses. I don't understand it too well, mostly because functional programming has a lot of concepts that tangle your head in knots. I'm trying to learn it, anyway...

http://en.wikipedia.org/wiki/Lisp_programming_language

Emacs is scriptable with Lisp.

"Any sufficiently advanced technology is indistinguishable from magic." -- Arthur C. Clarke
"Any sufficiently arcane magic is indistinguishable from technology." -- P. David Lebling

liorean
Nervous Wreck (II) Inmate

From: Umeå, Sweden
Insane since: Sep 2004

posted posted 09-10-2004 05:39

LISP is only one language (Fortran, if you must know) away from being the oldest high level language in use (i.e. that is neither machine code, assembly nor any other langauge that translates directly form and to machine code). It's core concepts and syntax is small, making for a very highly abstracted language. It's basic syntactical units are so-called S-expressions and symbols, it's basic value types are atoms and lists. This sparsity in concepts makes it at the same time a very powerful language. This expressiveness and power has gradually made it's way into modern languages, to the point that for example Ruby and Python are very closely related to LISP conceptually. (And if you look at two other conceptually very clean languages, APL and SmallTalk have also contributed considerably to modern languages.)


WikiPedia: LISP for a dictionary entry.
Structure and Interpretation of Computer Programs is a near-bible in LISP and Scheme associations.

--
var Liorean = {
prototype: JavaScriptGuru.prototype,
abode: "http://web-graphics.com/",
profile: "http://codingforums.com/member.php?u=5798"};

mobrul
Bipolar (III) Inmate

From:
Insane since: Aug 2000

posted posted 09-10-2004 15:31

Well, thanks to all three of you.

« BackwardsOnwards »

Show Forum Drop Down Menu