caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Polymorphism question
@ 2011-05-01 18:07 Sen Horak
  2011-05-01 19:32 ` Edgar Friendly
  0 siblings, 1 reply; 2+ messages in thread
From: Sen Horak @ 2011-05-01 18:07 UTC (permalink / raw)
  To: caml-list

Hi,

I am trying to model a situation in which a caller calls a combination
of two functions:

- The first is a postprocessor that depends on the invocation g: a -> 'b
- The second is a higher order function: f: c x d x g -> 'b

One could leave the application of 'g' to the caller, making f: c x d -> a.
Then the caller would run:
let res_a = f inp_c inp_d in my_g res_a

But it would be nice to hide 'a' from the caller and have her call:

let res_b = f inp_c inp_d my_g

I guess I could use functors, and configure a generic f with a
particular type of g. Does that seem right, and if so could there be a
simper or easier way?

Sen

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Polymorphism question
  2011-05-01 18:07 [Caml-list] Polymorphism question Sen Horak
@ 2011-05-01 19:32 ` Edgar Friendly
  0 siblings, 0 replies; 2+ messages in thread
From: Edgar Friendly @ 2011-05-01 19:32 UTC (permalink / raw)
  To: caml-list

On 05/01/2011 02:07 PM, Sen Horak wrote:
> Hi,
>
> I am trying to model a situation in which a caller calls a combination
> of two functions:
> let res_a = f inp_c inp_d in my_g res_a
>
> But it would be nice to hide 'a' from the caller and have her call:
> let res_b = f inp_c inp_d my_g
>
> I guess I could use functors, and configure a generic f with a
> particular type of g. Does that seem right, and if so could there be a
> simper or easier way?
Yes:
let f_simplified inp_c inp_d my_g = let res_a = f inp_c inp_d in my_g res_a

If you want, you can inline f_simplified into the original f, or use 
some sugar notation included in both batteries and core as:
let f_simplified c d g = f c d |> g

This is a straightforward application of higher order functions, where 
the function g is taken as a parameter of f_simplified and called from 
inside it.  The type of f_simplified is 'c -> 'd -> ('a -> 'b) -> 'b, 
showing the third parameter (g) has type ('a -> 'b).

If I were designing this, I'd have the caller use |> to convert the 
output instead of putting this in your function.

E.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-05-01 19:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-01 18:07 [Caml-list] Polymorphism question Sen Horak
2011-05-01 19:32 ` Edgar Friendly

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).