Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Creating true copies (of objects) in JS (possible?)

Reply
Thread Tools

Creating true copies (of objects) in JS (possible?)

 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      11-07-2003
Dom Leonard <(E-Mail Removed)> writes:

> Richard Cornford wrote:


> > ... it also occurred to me that the
> > function (func) itself may not require any parameters, which you have
> > covered here. However, if the function needs no parameters (func.length
> > == 0) shouldn't the value returned at this point be the result of
> > calling that function?

>
> I considered the effect some months ago, but ultimately thought the
> "desirable" behaviour may be unknowable.


In "normal" functional programming, the "type transformation"
associated with currying a function of n arguments is:

(Value^n -> Value) => Value->Value->....->Value -> Value
----------n times--------

The latter would be the type of a normal n-argument function in SML.

In SML, declared functions are normally curried.
The declaration
fun foo arg1 arg2 arg3 arg4 = ...
defines a curried function of four arguments, and has type
'a -> 'b -> 'c -> 'd -> ...

It is really a shorthand for four nested function expressions. If you
wrote it with only the anonymous function constructor, it would be

rec val foo = fn arg1 => fn arg2 => fn arg3 => fn arg4 => ...

The former type expression is the type of an SML function that takes
one argument which is an n-tuple in SML,
fun foo (arg1,arg2,arg3,arg4) = ...
or
rec val foo = fn (arg1,arg2,arg3,arg4) => ...
It is also the type of a normal Scheme function, since Scheme functions
are not curried by default.
(define (foo arg1 arg2 arg3 arg4) (...))
or
(define foo (lambda (arg1 arg2 arg3 arg4) (...)))


Even with that in mind, I think that the transformation of 0-ary
functions should still give a function, not call it. I see currying as
transforming one function into another with a different type. It never
turns a function into a non-function value, which calling the function
might.

> A curried funtion, pursuant to the currying operation in javascript,
> has a function length property of zero. If on account of that,
> currying a curried function returns the curried function, then
> "curry" on a curried function results in the identity transform.


I concur. That would be perfect.

In functional languages, a curried function is not nullary, but unary.
Currying an unary function often gives itself again.

In SML, currying doesn't need to be idempotent. If the original function
has type
(('a * 'b) * 'c) -> ...
then currying once gives
('a * 'b) -> 'c -> ...
which is the same as
('a * 'b) -> ('c -> ...)
This is a function with a product domain, so it can be curried again
to give
'a -> 'b -> 'c -> ...
In Javascript (or Scheme), we don't have tuples, so this case can't
occur.


> The question remaining is what should the curry operation, at the top
> level, do with a function that has a zero length property? Options are
> to return the function (my example), calling the function (your
> example) or throw an exception that curry was attempted on a function
> expecting no arguments.


I vote for returning the function on the grounds that I think the
curring transformation is a function to function transformation. In
SML, a thunk (0-ary function) is really a unary function that takes a
0-ary tuple as argument.

However, looking just at the type transformation,
Value^0 -> Value
should become
Value
so there is some support for calling the function. It is also prettier
in other ways. A recursive definition of currying on types would be:

Curry (Value^{n+1)->Value) = Value -> (Curry (Value^n -> Value))
Curry (Value^0 -> Value) = Value

Hmm. I started out believeing that only returning the function itself
made sense, but I can see that calling it does have theoretical merit.

> Whilst I tend to the last option, it's not hard and fast. In an
> reasonable program I would not expect the problem to arise, but am
> happy to defer to any theoretician with a rule for what should happen.


Theoretically, I would call the function. It is prettier and more consistent.
In practice, I want currying to be a function to function transformation.

/L 'yep, I'm a theoretician.'
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[False,True] and [True,True] --> [True, True]????? bdb112 Python 45 04-29-2009 02:35 AM
"0 in [True,False]" returns True Pierre Quentel Python 59 12-16-2005 01:47 PM
TurboGears /.-ed, >new == True< or >new == "True"< Andy Leszczynski Python 4 10-13-2005 06:56 AM
C and C++ are interoperable languages? True or Not True? Chip C++ 6 01-08-2005 11:10 PM
Does true ^ true return false? Siemel Naran C++ 19 06-18-2004 11:06 AM



Advertisments