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

L

Lasse Reichstein Nielsen

Dom Leonard said:
Richard Cornford wrote:

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.'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top