[Ur] Ur/Web -> JavaScript objects
Mark Clements
mark.clements at ki.se
Wed Oct 16 03:53:56 EDT 2019
Adam: that is very cool -- thank you.
All: as a noob question, I can update a record using:
{A=1,B=2,C=3} --- [A=_,B=_] ++ {A=2,B=1}
or
{A=1,B=2,C=3} -- #A -- #B ++ {A=2,B=1}
==
{A=2, B=1, C=3}
How can I write a general record update function (assuming all fields in
the update record are in the base record), such that
let
val base = {A=1,B=2,C=3}
val update = {A=2,B=1}
in
updater base update
end
==
{A=2, B=1, C=3}
I got as far as:
fun updater [tbase ::: {Type}] [tupdate ::: {Type}]
(fl : folder tupdate) (base : $tbase) (update : $tupdate) :
$tbase =
@foldR (* ?? *)
(fn [nm ::_] [t ::_] [rest ::_] [[nm] ~ rest] t acc =>
acc -- nm ++ {nm = t})
base fl update
Any guidance would be appreciated.
Kindly, Mark.
On 8/10/19 10:57 pm, Adam Chlipala wrote:
> On 10/6/19 5:45 PM, Mark Clements wrote:
>> In defining an FFI to a JavaScript library such as Chart.js, config
>> options may be defined in terms of (nested) JavaScript objects with a
>> range of potential keys/fields. Analogous to the xml tags, is there
>> any way to define a record type in Ur/Web that potentially includes a
>> subset of fields?
>
> In my own FFI wrapping so far, I have just given fields [option] types
> when they are optional, dealing with writing in [None] values for
> unused fields manually. I don't think it's so bad!
>
> Since I never miss an opportunity to show off metaprogramming in
> Ur/Web, here's a function you can use to cast a record to a wider type
> where all the new fields have [option] types.
>
> fun expand [original ::: {Type}] [additional ::: {Type}] [original ~
> additional]
> (fl : folder additional) (r : $original)
> : $(original ++ map option additional) =
> r ++ @map0 [option] (fn [t ::_] => None) fl
>
> fun foo (x : {A : int, B : option string, C : float, D : option bool,
> E : string}) =
> 42
>
> val example = foo (expand {A = 3, C = 1.234, D = Some True, E = "hi"})
>
>
> _______________________________________________
> Ur mailing list
> Ur at impredicative.com
> https://eur01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.impredicative.com%2Fcgi-bin%2Fmailman%2Flistinfo%2Fur&data=02%7C01%7Cmark.clements%40ki.se%7Cc261896e2002436d168d08d74c3223c8%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C0%7C0%7C637061651008402618&sdata=d0C41U6idMCbn%2F%2B8hSEdEj7fTNvzU2tdf8Jx4olkTT8%3D&reserved=0
>
När du skickar e-post till Karolinska Institutet (KI) innebär detta att KI kommer att behandla dina personuppgifter. Här finns information om hur KI behandlar personuppgifter<https://ki.se/medarbetare/integritetsskyddspolicy>.
Sending email to Karolinska Institutet (KI) will result in KI processing your personal data. You can read more about KI’s processing of personal data here<https://ki.se/en/staff/data-protection-policy>.
More information about the Ur
mailing list