LiveScript icon indicating copy to clipboard operation
LiveScript copied to clipboard

Suggestion: Remove commas when calling a function

Open lessandro opened this issue 13 years ago • 6 comments

As someone who's used to Haskell, when writing Livescript I often wish I could omit the commas between arguments when calling a function.

In particular, code that uses prelude-ls looks a lot more natural without the commas. For instance:

concat-map (-> [1 to it]) [1 2 3]
filter (== 2) [1 2 3 4 5]
foldr (++) "" <| map (.to-string!) [1 2 3 4 5]

Adding $ as an alias for <| would also be interesting.

lessandro avatar Oct 27 '13 07:10 lessandro

Haskell can do this because of its type system. For example in the filter call, it sees that (== 2) has type Eq a => a -> Bool, and [1 2 3 4 5] has [Int], so the array can never be applied to the function, and both are instead treated as arguments to filter. LiveScript has no type system: anything that looks like expr1 expr2 is a call, unless expr1 is a literal, which it knows cannot be called and so inserts a comma.

$ is a valid JavaScript identifier. Using it as an operator would be ambiguous (and preclude using jQuery).

apaleslimghost avatar Oct 27 '13 10:10 apaleslimghost

This is not related to its type-system, actually. You just use parenthesis (or $) whenever you want to make an inner call.

vendethiel avatar Oct 27 '13 10:10 vendethiel

@quarterto It has nothing to do with its type system. Haskell can do this because whitespace is used for left-associative function application with the highest precedence. x y z is by definition equivalent to (x y) z, so filter (== 2) [1..5] is the same as (filter (== 2)) [1..5].

(Also, I should point out that it is actually possible to apply a function of type a -> Bool with a [Int]: try const True [1 :: Int] or (\_ -> True) [1 :: Int], since the type variable a really means anything and [Int] is one of anything.)

Anyway, I support this idea as I've always felt the commas are a bit redundant and also cause awkward cases such as having to use ; as delimiters in lists when elements contains a function call (and this seems to be a hidden feature which is not documented on the front-page?).

Adding $ as an alias for <| would also be interesting.

Unfortunately $ is a valid identifier in Javascript. :(

PkmX avatar Oct 27 '13 12:10 PkmX

I also find the commas annoying, but I can't see how LiveScript could drop them without breaking the world. Whitespace denotes a right-associative function application, such that f g x means f (g x), not (f g) x.

OTOH, not all functions in JavaScript are curried by default, and LiveScript's curry takes many parameters, rather than just one, so it would be doable to compile f g x to f(g, x), however this would break existing code due to the aforementioned implications :(

robotlolita avatar Oct 27 '13 12:10 robotlolita

Well that'll teach me not to answer issues when tired and hungover.

apaleslimghost avatar Oct 27 '13 15:10 apaleslimghost

@killdream is right. You must be able to use any JS library from LS, and they do not have curried functions. We can't know if f g x is meant to be f(g)(x) or f(g, x).

gkz avatar Nov 12 '13 23:11 gkz