Title here
Summary here
Here’s a function that will take an arbitrary number of int
s as arguments.
module Main where
import Effect.Console (log)
import Data.Foldable (foldl)
sum :: Array Int -> Int
sum nums = do
log $ show nums
let total = foldl (\acc x -> acc + x) 0 nums
log $ show total
total
main :: Effect Unit
main = do
sum [1, 2]
sum [1, 2, 3]
let nums = [1, 2, 3, 4]
sum nums
Variadic functions can be called in the usual way with individual arguments.
main :: Effect Unit
main = do
sum [1, 2]
sum [1, 2, 3]
let nums = [1, 2, 3, 4]
sum nums
If you already have multiple args in a slice, apply them to a variadic function using the list.
main :: Effect Unit
main = do
let nums = [1, 2, 3, 4]
sum nums
$ spago run
[1,2] 3
[1,2,3] 6
[1,2,3,4] 10
Another key aspect of functions in PureScript is their ability to form closures, which we’ll look at next.