fjoppe / Legivel

F# Yaml 1.2 parser
https://fjoppe.github.io/Legivel
The Unlicense
60 stars 5 forks source link

Map vs List of tuples? #11

Closed pkese closed 5 years ago

pkese commented 5 years ago

Hi Frank,

I have a Yaml object in which I need to preserve ordering of items (e.g. when iterating over object "world cup" should come before "superbowl"):

recent_searches:
    world cup: 71
    superbowl: 5
    hurricane florence: 3
    donald trump: 81

Now if I model this as {recent_searches: Map<string,int>} then I loose that ordering.

Is it possible to somehow model this in a way to get a list of tuples as a result:
{recent_searches: List<string,int>}.

Yes, I know, minuses in yaml document would help, but unfortunately in this case I can't change the yaml source - it's probably done so, because it's using yaml's & and * references all over the place to build these objects.

I'm just fishing for ideas. Any hint will help if you happen to have one.

Thanks

fjoppe commented 5 years ago

Hi,

I wouldn't know how. You now have a mapping with entries, and the only way to distingiush a mapping from a sequence is with extra syntax. Even the !!omap, supported by one of the schema's in the parser, not in the mapper, uses sequence: https://yaml.org/type/omap.html

If you can't change the yaml schema, you can only restore ordering after deserialization, with your own function.

Regards, Frank

Verstuurd vanaf mijn iPad

Op 8 mei 2019 om 23:31 heeft Peter Keše notifications@github.com<mailto:notifications@github.com> het volgende geschreven:

Hi Frank,

I have a Yaml object in which I need to preserve ordering of items (e.g. when iterating over object "world cup" should come before "superbowl":

recent_searches: world cup: 71 superbowl: 5 hurricane florence: 3 donald trump: 81

Now if I model this as {recent_searches: Map<string,int>} then I loose that ordering.

Is it possible to somehow model this in a way to get a list of tuples as a result: {recent_searches: List<string,int>}.

Yes, I know, minuses in yaml document would help, but unfortunately in this case I can't change the yaml source - it's probably done so, because it's using yaml's & and * references all over the place to build these objects.

I'm just fishing for ideas. Any hint will help if you happen to have one.

Thanks

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/fjoppe/Legivel/issues/11, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ACMMW5FQLM7JLUT3VDXDV43PUNBE3ANCNFSM4HLVMQHA.

fjoppe commented 5 years ago

Added to that. I believe that the yaml Parser keeps the order of the mapping entries. That requires you to process the raw results. However if that is the case, I cannot promise that this ‘feature’ will be kept in future. But, seeing your question, it is good to think about this.

Regards, Frank

Verstuurd vanaf mijn iPhone

Op 8 mei 2019 om 23:31 heeft Peter Keše notifications@github.com<mailto:notifications@github.com> het volgende geschreven:

Hi Frank,

I have a Yaml object in which I need to preserve ordering of items (e.g. when iterating over object "world cup" should come before "superbowl":

recent_searches: world cup: 71 superbowl: 5 hurricane florence: 3 donald trump: 81

Now if I model this as {recent_searches: Map<string,int>} then I loose that ordering.

Is it possible to somehow model this in a way to get a list of tuples as a result: {recent_searches: List<string,int>}.

Yes, I know, minuses in yaml document would help, but unfortunately in this case I can't change the yaml source - it's probably done so, because it's using yaml's & and * references all over the place to build these objects.

I'm just fishing for ideas. Any hint will help if you happen to have one.

Thanks

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/fjoppe/Legivel/issues/11, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ACMMW5FQLM7JLUT3VDXDV43PUNBE3ANCNFSM4HLVMQHA.

pkese commented 5 years ago

I guess the yaml file is in this format because up until now they used Python to process these files: Python preserves the order of its dictionaries, so people were not paying attention, because the default dictionary solved their issue.

I was thinking: one option to solve this in Legivel would be to make Legivel (besides Map<k,v>) also accept any type that implements IDictionary<k,v> interface.

There are several OrderedDictionary<k,v> implementations flying around, so one could supply Legivel with such a type (these also implement IDictionary<k,v> interface): https://github.com/electricessence/Open.Collections/blob/master/source/OrderedDictionary.cs there's even one being planned for official introduction into dotnet core (it's marked for milestone dotnet core 3.0, but I think it is more probable to land at a later time). https://github.com/dotnet/corefx/issues/26634

This is just a suggestion of a solution that doesn't require major API changes.

I don't want to pressure you to implement it (I'll find my ways around somehow) - feel free to close this issue.

fjoppe commented 5 years ago

It may be an easy implementation. I've tried in this branch. Still needs some extra testing and a look at the documentation.

fjoppe commented 5 years ago

I've commited v0.3.1 now with this feature: https://www.nuget.org/packages/Legivel/0.3.1

I've also created a unit-test for the OrderedDictionary<k,v> from the Open.Collections package from your example.

pkese commented 5 years ago

Frank,

You've impressed me so much... I totally did not expect you to fulfil my random wishes for everything that I experiment with, and yet you did nevertheless.

Now my next big worry at this time is: how do I buy you a beer? Is this challenge solvable?
Please let me know!

fjoppe commented 5 years ago

I believe a nice beer could go via this link

It was a nice idea thanks.

pkese commented 5 years ago

There you go. Cheers.

It was just a symbolic 'beer' sum - sort of a token of gratitude and recognition. I hope you'll enjoy it nevertheless.

Thanks for helping me solve this other problem as well - the level of your resourcefulness is admirable ;-)

fjoppe commented 5 years ago

Thanks!

Verstuurd vanaf mijn iPhone

Op 13 mei 2019 om 22:51 heeft Peter Keše notifications@github.com<mailto:notifications@github.com> het volgende geschreven:

There you go. Cheers.

It was just a symbolic 'beer' sum - sort of a token of gratitude and recognition. I hope you'll enjoy it nevertheless.

Thanks for helping me solve this other problem as well - the level of your resourcefulness is admirable ;-)

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/fjoppe/Legivel/issues/11?email_source=notifications&email_token=ACMMW5BYQ2Y3PRXWJHD2CPDPVHIDLA5CNFSM4HLVMQHKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVJQVCY#issuecomment-491981451, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ACMMW5AKDLIR2SLUKGIG6ITPVHIDLANCNFSM4HLVMQHA.