Common sorted list: Difference between revisions

Content added Content deleted
(Added Wren)
Line 81: Line 81:
import "/sort" for Sort
import "/sort" for Sort


var distinctUnion2 = Fn.new { |l1, l2| Lst.distinct(l1 + l2) }
var distinctSortedUnion = Fn.new { |ll|
var res = ll.reduce([]) { |acc, l| acc + l }

res = Lst.distinct(res)
var distinctUnionN = Fn.new { |ll|
Sort.insertion(res)
var n = ll.count
if (n == 0) return ll
return res
if (n == 1) return ll[0]
var first2 = distinctUnion2.call(ll[0], ll[1])
if (n == 2) return first2
return ll.skip(2).reduce(first2) { |acc, l| distinctUnion2.call(acc, l) }
}
}


var ll = [[5, 1, 3, 8, 9, 4, 8, 7], [3, 5, 9, 8, 4], [1, 3, 7, 9]]
var ll = [[5, 1, 3, 8, 9, 4, 8, 7], [3, 5, 9, 8, 4], [1, 3, 7, 9]]
System.print("Distinct sorted union of %(ll) is:")
System.print("Distinct sorted union of %(ll) is:")
System.print(distinctSortedUnion.call(ll))</lang>
var u = distinctUnionN.call(ll)
Sort.insertion(u)
System.print(u)</lang>


{{out}}
{{out}}