# JavaScript Function invocation with arbitrary type function with invocation,function invocation in javascript,function invocation in

I am trying to implement a function `f` so that when called like the following:

``````(((f 1) 2) 3) ()
``````

It returns the sum of 1, 2, and 3, which is 6. Only when it's invoked with a Unit argument `()` that the computation happens and the sum returned.

In Javascript (ES6), for instance, it could be implemented as such:

``````let f = (v, acc = 0) => {
if (typeof v === 'undefined') return acc;
return next => f(next, acc + v);
};

f(1)(2)(3)();   // 6
``````

However in Ocaml with strong typing it isn't as clear, or at least not as terse.

EDIT: Here is my attempt using variant types:

``````type any =
| Nothing
| Number of int

type result =
| Int of int
| Fn of (any -> result)

let rec sumfun ?(acc=0) v =
match v with
| Nothing -> Int(acc)
| Number n -> Fn(fun next -> sumfun ~acc:(acc+n) next)

let _ =
let a = sumfun (Number 2) in
match a with
| Int n -> print_int n
| Fn f ->
let b = f (Number 3) in
match b with
| Int n -> print_int n
| Fn f ->
let c = f Nothing in
match c with
| Int n -> print_int n
| Fn f -> ()
``````

As it is the calls to `sumfun` are super hairy. Is there a better, less verbose or more idiomatic way than this?

As pointed out in the comments, the general advice would be not to do such a thing. Your particular example would naturally be expressed with a list:

``````let sum = List.fold_left (+) 0

let n = sum [1; 2; 3]
``````

I don't see any advantage in using the complicated construction you show, regardless of types.

But to answer your original question anyway, the best you can do probably is providing custom operators for application, e.g.

``````let (\$) f x = match f (Number x) with Fn f' -> f' | _ -> assert false
let (\$\$) f () = match f Nothing with Int n -> n | _ -> assert false

let n = sumfun \$ 1 \$ 2 \$ 3 \$\$ ()
``````

(I tried to use GADTs to avoid the `assert false` there, but apparently they interact badly with optional arguments.)

But to repeat the point: Don't.

## Print Triangle using javascript function

function makeLine(length) { var line = ""; for (var i = 1; i <= length; i++) { for (var j = 1; j <= i; j++) { line += "*"; } } return line + "\n"; } console....

function makeLine(length) { var line = ""; for (var i = 1; i <= length; i++) { for (var j = 1; j <= i; j++) { line += "*"; } } return line + "\n"; } console....

1. print triangle using recursion
2. print triangle using python
3. print triangle using while loop
4. print triangle using for loop in c++
5. print triangle using for loop in java
6. print triangle using c
7. print triangle using java
8. print triangle using for loop
9. print pascal's triangle using java program
10. print number triangle using python
11. print triangle in python using while loop
12. print triangle in python using for loop
13. print triangle in java using while loop
14. print triangle in python using one for loop
15. print triangle in c++ using while loop
16. program to print triangle using numbers
17. c program to print triangle using *
18. c program to print triangle using for loop
19. print numbers triangle shape using c
20. java program to print triangle using for loop

## How to substract 1 month from selected date with the right format

I want to substract 1 month from the selected date and get it in this format mm/dd/yyyy self.SelectedDate = "02/22/2018"; var temp = new Date(self.SelectedDate); temp.setDate(temp.getDate() - 30);...

I want to substract 1 month from the selected date and get it in this format mm/dd/yyyy self.SelectedDate = "02/22/2018"; var temp = new Date(self.SelectedDate); temp.setDate(temp.getDate() - 30);...

1. subtract month from date
2. subtract month from date sql
3. subtract month from date python
4. subtract month from date javascript
5. subtract month from date oracle
6. subtract month from date r
7. subtract month from current date in sql
8. subtract month from date dax
9. subtract month from pandas datetime
10. subtract month from date in sas
11. subtract month from date in excel
12. subtract month from date c#
13. subtract month from date pandas
14. subtract month from date php
15. subtract month from date mysql
16. subtract month from date java
17. subtract month from date in hive
18. subtract month from current date in c#
19. subtract month from date js
20. subtract month from date sql oracle

## select image and and play an audio by clicking the button using html and javascript

<audio id="audio1" src="a.wav"></audio> <audio id="audio2" src="b.wav"></audio> <img id="img1" src="a.png" alt="" onselect="select()" /> <img id="img2" src="a.png" ...

<audio id="audio1" src="a.wav"></audio> <audio id="audio2" src="b.wav"></audio> <img id="img1" src="a.png" alt="" onselect="select()" /> <img id="img2" src="a.png" ...

## Get report parameters as URL string

I have an application that serves SSRS reports. A given report is accessed in the following way: https://reportserver.com/reportname Upon clicking View Report, a postback is submitted to the report ...

I have an application that serves SSRS reports. A given report is accessed in the following way: https://reportserver.com/reportname Upon clicking View Report, a postback is submitted to the report ...

1. birt report parameters query string
2. ssrs report parameters query string
1