Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Map.fromListUnique #161

Open
Dean177 opened this issue Jun 18, 2020 · 3 comments
Open

Add Map.fromListUnique #161

Dean177 opened this issue Jun 18, 2020 · 3 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@Dean177
Copy link
Collaborator

Dean177 commented Jun 18, 2020

val fromListUnique :
     ('key, 'identity) Comparator.s
  -> ('key * 'value) list
  -> (('key, 'value, 'identity) t, 'key) Result.t

This function should work similarly to fromList but will return an Ok if all of the keys are distinct and an Error containing the duplicated key if it is encountered.

Naming suggestions would be helpful.

@Dean177 Dean177 added help wanted Extra attention is needed good first issue Good for newcomers labels Jun 18, 2020
@shubhamkumar13
Copy link

shubhamkumar13 commented Sep 15, 2020

The keys that would be of Error type would they be still part of the sorted list? Where now it would be sorted in terms of value?

for eg. if the input is [(2, "b"), (1, "a"), (2,"c"), (3, "d")] the output should be [ Ok (1, "a"), Ok (2, "b"), Err (2), Ok (3, "d")] ?

also if there are multiple occurrences what output should it take?

@shubhamkumar13
Copy link

shubhamkumar13 commented Sep 16, 2020

So I wrote a function

  let f arr = Array.fold_left (fun acc x -> 
    ((Array.fold_left (fun acc i -> 
      match (Stdlib.compare (fst i) (fst x)) with 
      | 0 -> acc + 1 
      | _ -> acc) 0 arr), acc, x) |> fun (sum,acc,x) -> 
        if sum = 1 then 
          (Result.ok x) :: acc 
        else 
          (Result.error (fst x)) :: acc) [] arr |> Array.of_list

Which takes an input for eg. -> [| (2, "b"); (1, "a"); (2,"c"); (3, "d") |]
And this outputs -> [| Ok (3, "d"); Error 2; Ok (1, "a"); Error 2 |]

But I don't know how to use fromArray or create a custom comparator

@pbiggar
Copy link
Member

pbiggar commented Sep 16, 2020

Thanks for doing all this work!

I think there's a slight misunderstanding as to the intent of this. As you process the list, you should be adding the keys/values to a Map, not making another list.

The Ok is intended to be a ('key, 'value, 'comparator) Map.t with the key/values from the list in it. The Error case just has the key.

So:

Map.fromListUnique [ (2, "b"); (1, "a"); (2,"c"); (3, "d") ] = Error 2

Does that clear things up?

@pbiggar pbiggar added this to the 0.2.0 milestone Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants