question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validation<'a, 'b> list -> Validation<'a list, 'b list>

See original GitHub issue

I have a scenario the user creates n objects. If all objects are valid I want to get all the objects, if any one of them is invalid, I want to get all the errors, something like: Validation<'a, 'b> list -> Validation<'a list, 'b list>.

I’m posting this here as it might be a common scenario.

Based on @gusty’s comment on a previous issue, I ended up with:

let f (vs : Validation<_, _> list): Validation<_ list, _> = traverse (first result) vs

(it needed a little more help with the types.)

Which gives me the behaviour I would expect:

let private f (vs : Validation<_, _> list): Validation<_ list, _> = traverse (first result) vs

[<Fact>]
let ``My test`` () =
    let x1 : Validation<string, _> list = [ Success   1; Success   2; Success   3 ]
    let e1 : Validation<string list, _> = Success [   1;           2;           3 ]
    let x2                              = [ Success   1; Failure "a"; Success   3 ]
    let e2 : Validation<_, int list>    = Failure [              "a"              ]
    let x3 : Validation<_, int> list    = [ Failure "a"; Failure "b"; Failure "c" ]
    let e3 : Validation<_, int list>    = Failure [ "a";         "b";         "c" ]

    test <@ f x1 = e1 @>
    test <@ f x2 = e2 @>
    test <@ f x3 = e3 @>

Thank you for you help @gusty, @DunetsNM. One slight follow-up, is there an appropriate name for this function?

Originally posted here: https://github.com/fsprojects/FSharpPlus/issues/51#issuecomment-586202997

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
gustycommented, Mar 9, 2020

One more tip:

If you don’t write a generic result F# is able to infer the whole function.

traverse (first List.singleton) vs so you don’t need to pre-create the function.

We might add Bitraversable and Biapplicative to the library.

Bitraversable is almost there as many extensions have bisquence and bitraverse already.

1reaction
NickDarveycommented, Feb 16, 2020

Perfect, thank you @gusty!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Excel Drop Down Lists - Data Validation
Easy steps to make drop down list wth Excel data validation. Show list of valid entries, reduce data entry errors. Videos, written notes ......
Read more >
Apply data validation to cells
Select one or more cells to validate. · On the Data tab, in the Data Tools group, click Data Validation . · On...
Read more >
Data validation with conditional list - Excel formula
Data validation rules are triggered when a user adds or changes a cell value. This formula takes advantage of this behavior to provide...
Read more >
Excel formula: Data validation exists in list - Excelchat
Want to learn how to check if Data validation exists in list? ... Here we have a table named “Item List” in column...
Read more >
Custom Data Validation in Excel : formulas and rules
Learn how to use custom Data Validation in Excel with your own rules and formulas. Formula examples to allow only numbers or text...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found