Named optional function arguments - PureScript

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

TheMatten

(I'm not using PS for that long, so bear with me if I'm missing something or this solution is actually common)
Trying to write wrappers around functions taking lots of named arguments (using array), I was wondering whether it would be possible to use something more elegant than bunch of functions in an array to handle them - I've seen some people using Record.union/Record.merge, but trying that, I quickly ran into inference issues that I can't that easily overcome in PS (in HS I would use type equalities on custom typeclass to "trap" label into proper type). At the end, there turned out to be one simple solution - function...

More specfically, taking function

{ | optional } -> { | required optional }

instead of merging with some

Union { | optional } { | required rest } { | required optional } => { | required rest }

where required and optional are corresponding arguments, I can keep inference happy by updating existing fields instead of trying to match some in separate records. Now, great thing about PS are it's underscore-based sections, and using record update section I can get this pretty syntax

foo _
  { a = b
  , c = d
  , ..
  }

instead of

foo
  [ X.a b
  , Y.c d
  , ..
  ]

without any name clashes and without possibility of duplicate fields

Mason Mackaman

I would love to see a full example

TheMatten

I don't really have good example to show right now, but I've basically started bolting Fomantic UI on top of normal html from Halogen (https://pursuit.purescript.org/packages/purescript-halogen/5.0.0/docs/Halogen.HTML.Elements) with some custom parameters
Oh, one fix - {| required } -> ({| optional } -> {| optional }) -> _ (record updates can't introduce new fields, and using insert would defeat the purpose of while thing)

miles

I may be misreading your requirements, but would this do the job for named optional arguments?
https://pursuit.purescript.org/packages/purescript-option

TheMatten

Yeah, technically, though I'm going for the most compact solution and these seem to be slightly more verbose

Mason Mackaman

@miles woah I hadn't seen that before. I didn't know that was possible. that's really cool.

Mason Mackaman

ah man, do I need to use bower in order to use it?

miles

Looks like it's not yet in the package set, but you can add it as an "addition" in spago. https://github.com/purescript/spago/#add-a-package-to-the-package-set

🍝 PureScript package manager and build tool powered by Dhall and package-sets - purescript/spago
Mason Mackaman

I tried to clone it and run it with spago to see the docs for version 2 and it didn't work. so I'm not sure if it's that simple.

Mason Mackaman

I didn't try the version that's on pursuit though, maybe that one would work.

Mason Mackaman

yeah, checking out v1.0.1 (version on pursuit) and running

spago init
spago build

fails to compile