How can I combine two date lists in F #?

There are two date lists (cannot be allowed for the order of the list)

//first list [date_a; date_b; date_c] //second list [date_A; date_B; date_C] 

I am looking for a function that returns a list as a list: the date is a unique key (a single date will only be displayed once in the list)

 -> (date, true, true) in case both lists contained the date -> (date, true, false) in case the first list contained the date -> (date, false, true) in case the second list contained the date (there will be no (date, false, false) entries) 
+4
source share
3 answers

Using some simple dialing operations:

 open System //'a list -> 'a list -> ('a * bool * bool) list when 'a : comparison let merge dtl1 dtl2 = let dts1 = Set.ofList dtl1 let dts2 = Set.ofList dtl2 let dts1Only = dts1 - dts2 let dts2Only = dts2 - dts1 let dtsBoth = Set.intersect dts1 dts2 [ for dt in dts1Only do yield (dt,true,false) for dt in dts2Only do yield (dt,false,true) for dt in dtsBoth do yield (dt,true,true) ] 

Here is an example:

 let dtl1 = [DateTime.Today.AddDays(1.) DateTime.Today.AddDays(2.) DateTime.Today.AddDays(3.) DateTime.Today.AddDays(4.) DateTime.Today.AddDays(5.) DateTime.Today.AddDays(6.)] let dtl2 = [DateTime.Today.AddDays(4.) DateTime.Today.AddDays(5.) DateTime.Today.AddDays(6.) DateTime.Today.AddDays(7.) DateTime.Today.AddDays(8.) DateTime.Today.AddDays(9.)] merge dtl1 dtl2 

enter image description here

+4
source
 let showMembership l1 l2 = let s1 = Set.ofList l1 let s2 = Set.ofList l2 Set.union s1 s2 |> Set.map (fun d -> (d, Set.contains d s1, Set.contains d s2)) 

Note that this returns Set, but you can use List.ofSeq to create a list if required

+7
source

Another example is implemented with recursive functions (perhaps not as simple and fast as others, but made with a different approach):

 let rec find (b: 'T list) (a: 'T) : bool * 'T list = match b with | [] -> false, b | h :: t -> if h = a then true, t else let res, restB = a |> find t res, h :: restB let rec merge (a: 'T list) (b: 'T list) (order: bool) : ('T * bool * bool) list = match a with | [] -> if not(order) then [] else merge ba false | h :: t -> let resA, newB = h |> find b (h, resA || order, resA || not(order)) :: merge t newB order let Merge (a: 'T list) (b: 'T list) : ('T * bool * bool) list = merge ab true 

And for:

 let dtl1 = [DateTime.Today.AddDays(1.) DateTime.Today.AddDays(2.) DateTime.Today.AddDays(3.) DateTime.Today.AddDays(4.) DateTime.Today.AddDays(5.) DateTime.Today.AddDays(6.)] let dtl2 = [DateTime.Today.AddDays(4.) DateTime.Today.AddDays(5.) DateTime.Today.AddDays(6.) DateTime.Today.AddDays(7.) DateTime.Today.AddDays(8.) DateTime.Today.AddDays(9.)] Merge dtl1 dtl2 

gives:

 [(27.11.2011 0:00:00, true, false); (28.11.2011 0:00:00, true, false); (29.11.2011 0:00:00, true, false); (30.11.2011 0:00:00, true, true); (01.12.2011 0:00:00, true, true); (02.12.2011 0:00:00, true, true); (03.12.2011 0:00:00, false, true); (04.12.2011 0:00:00, false, true); (05.12.2011 0:00:00, false, true)] 

Update: The merge function has been simplified to make DateTimes order similar to other answers as a result.

0
source

Source: https://habr.com/ru/post/1383194/


All Articles