Is there a good why there is no predefined identity function?
(fun x -> x) is sometimes less readable, and seems to be not
compiled (yet?) as this black magic:
external identity : 'a -> 'a : "%identity"
In the same style, we already have Pervasives.ignore, so why not
Pervasives.identity?
Martin
-------------------
To unsubscribe, mail caml-lis...@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
A good reason, yes: it's generally useless. There are no predefined S
and K combinators either :-)
> (fun x -> x) is sometimes less readable, and seems to be not
> compiled (yet?) as this black magic:
> external identity : 'a -> 'a : "%identity"
Even if you define
external identity: 'a -> 'a = "%identity"
references to "identity" will be compiled like "fun x -> x".
It's only direct applications of "identity", e.g. "identity 3", that
would be more efficient than with the definition
let identity x = x
But I hope your programs don't contain calls like "identity x"...
> In the same style, we already have Pervasives.ignore, so why not
> Pervasives.identity?
Because Pervasives.ignore is very useful and commonly used to deal
with the "should have type unit" warning.
- Xavier Leroy
> Even if you define
> external identity: 'a -> 'a = "%identity"
> references to "identity" will be compiled like "fun x -> x".
> It's only direct applications of "identity", e.g. "identity 3", that
> would be more efficient than with the definition
> let identity x = x
> But I hope your programs don't contain calls like "identity x"...
My problem was:
(* something.mli *)
type key
(* something.ml *)
type key = int
let key_of_int = identity (** what should I write here? **)
(* other.ml *)
let key = Something.key_of_int 1
Sure, there are more important problems in the world...
Martin
> Hello,
>
> Is there a good why there is no predefined identity function?
> (fun x -> x) is sometimes less readable, and seems to be not
> compiled (yet?) as this black magic:
> external identity : 'a -> 'a : "%identity"
>
> In the same style, we already have Pervasives.ignore, so why not
> Pervasives.identity?
>
>
Possibly stupid question: what use would this function be?
--
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
- Gene Spafford
Brian
> Possibly stupid question: what use would this function be?
I use it with higher order functions. For example
I have a function:
type typed_expr = expr_t * type_t
map_typed_expr:
(expr_t -> expr_t) ->
(type_t -> type_t) ->
typed_expr_t -> typed_expr_t
Sometimes I wish to map only the typing in an expression,
and sometimes only the expression terms not the typing.
So I pass 'identity' as one of the arguments.
An identity function would also be useful in machine
generated code in a situation similar to the above
where the HOF was implemented in the code generator,
and you don't want to specialise the generator
but would rather the target compiler optimise
the application of 'identity' away.
So it is 'useful' -- but this is no argument
it should be in Pervasives.
--
John Skaller, mailto:ska...@users.sf.net
voice: 061-2-9660-0850,
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net
I've had to define an identity function from time to time. Most
recently I wanted to define a function for printing labels on charts:
let plot ?(labels = identity) data =
(* ... *)
Called as:
plot ~labels:string_of_int data
if the label was an int instead of the default string. There was a
thread on this on ocaml-beginners I think.
Rich.
--
Richard Jones. http://www.annexia.org/ http://www.j-london.com/
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
http://www.winwinsales.co.uk/ - CRM improvement consultancy