functional programming - Extract single element from list in F# -
I want to remove an item from sequence in F #, or give an error if there is no one or more than one What is the best way to do this?
I currently have
element = data | | (Seq.filter (function | roximal property (x) -> false | _ - & gt; true)). & Gt; List .of_seq | & Gt; (Function head :: [] - gt; head | head :: tail - & gt; unsuccessful ("many elements.") | [] -> Failed ("empty sequence")). & Gt; (Happened with X -> Mileage (Data) -> -> -> Failed ("Bad Element.")) But is this really the best way?
Edit: As I mentioned in the right direction, I came with the following:
let element = data | & Gt; (Seq.filter (function | roximal property (x) -> false | _ - & gt; true)). & Gt; (Fun - - gt; If the height of the ceiling then s & lt;> 1 then fails ("Sequence must be exactly one item") Otherwise); & Gt; Seq.hd | & Gt; ("Bad element".)) My guess is that it's a little good. / P>
There is a function in the sequence.
val search: ('a -> bool) - & gt; Seq & lt; 'A & gt; - & gt; 'A' But if you want to make sure that seq has only one element, then a Seq.filter is doing it, then take the length after the filter and make sure that It's equal to one, and then head. There is no need to change everything in the SAC list.
Edit: On one side, I suggest was to suggest that tail is a result empty (O (1), Rather than using the function length (O), tail is not a part of the seq, but I think that you can work in a good way to emulate that functionality. / P>
Comments
Post a Comment