Skip to content

Instantly share code, notes, and snippets.

@kika
Last active June 20, 2017 13:11
Show Gist options
  • Save kika/23949e51fe9637b2b0ce1aa50373e52b to your computer and use it in GitHub Desktop.
Save kika/23949e51fe9637b2b0ce1aa50373e52b to your computer and use it in GitHub Desktop.
Purescript variadic function - function with variable number of arguments of variable type
module Main where
import Prelude (class Show, Unit, ($), show, (++), (<<<))
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Data.String (joinWith)
import Data.ExistsR
class PrintF t where
printf' :: Array String -> t
instance sprintfOut::PrintF String where
printf' acc = joinWith " " acc
instance printfArg::(Show a, PrintF r) => PrintF (a -> r) where
printf' acc = \x -> printf' (acc ++ [show x])
printf::forall t. (PrintF t) => t
printf = printf' []
newtype Record = Record (ExistsR Object)
instance showRecord::Show Record where
show (Record r) = showRecordImpl r
foreign import showRecordImpl::ExistsR Object -> String
mkR::forall r. {|r} -> Record
mkR = Record <<< mkExistsR
main :: forall e. Eff (console::CONSOLE|e) Unit
main = do
log $ printf "Record print: " (mkR {first:"John", last:"Doe" }) " Int= " 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment