Jaccard index: Difference between revisions

→‎{{header|Wren}}: Updated in line with task description.
(Add Factor)
(→‎{{header|Wren}}: Updated in line with task description.)
Line 69:
=={{header|Wren}}==
{{libheader|Wren-set}}
{{libheader|Wren-trait}}
{{libheader|Wren-fmt}}
Note that the Set object in the above module is implemented as a Map and consequently the iteration order (and the order in which elements are printed) is undefined.
<lang ecmascript>import "./set" for Set
import "./trait" for Indexed
import "./fmt" for Fmt
 
var jacardIndex = Fn.new { |a, b|
Line 77 ⟶ 81:
}
 
var a = Set.new([1, 2, 3, 4, 5, 6])
var b = Set.new([31, 42, 53, 64, 7, 85])
avar c = <2, Set.new([1, 3, 5, 47, 6>9])
System.print("a = %(a)")
var d = Set.new([2, 4, 6, 8, 10])
System.print("b = %(b)")
var e = Set.new([2, 3, 5, 7])
System.print("J(a, b) = %(jacardIndex.call(a, b))")</lang>
var f = Set.new([8])
var sets = [a, b, c, d, e, f]
 
for (se in Indexed.new(sets)) {
var i = se.index
var s = se.value
s = s.toList.sort() // force original sorted order
Fmt.print("$s = $n", String.fromByte(65 + i), s)
}
 
var pairs = [
[a, b], [a, c], [a, d], [a, e], [a, f], [b, c], [b, d], [b, e],
[b, f], [c, d], [c, e], [c, f], [d, e], [d, f], [e, f]
]
 
var names = [
"AB", "AC", "AD", "AE", "AF", "BC", "BD", "BE",
"BF", "CD", "CE", "CF", "DE", "DF", "EF"
]
 
System.print("a = %(a)")
for (se in Indexed.new(pairs)) {
var n = names[se.index]
var ss = se.value
System Fmt.print("J(a$s, b$s) = %($h", n[0], n[1], jacardIndex.call(ass[0], bss[1]))")</lang>
}</lang>
 
{{out}}
<pre>
A = []
a = <2, 1, 3, 5, 4, 6>
bB = <3[1, 52, 8, 73, 4, 6>5]
C = [1, 3, 5, 7, 9]
J(a, b) = 0.5
D = [2, 4, 6, 8, 10]
E = [2, 3, 5, 7]
F = [8]
 
J(A, B) = 0
J(A, C) = 0
J(A, D) = 0
J(A, E) = 0
J(A, F) = 0
J(B, C) = 0.428571
J(B, D) = 0.25
J(aB, bE) = 0.5
J(B, F) = 0
J(C, D) = 0
J(C, E) = 0.5
J(C, F) = 0
J(D, E) = 0.125
J(D, F) = 0.2
J(E, F) = 0
</pre>
9,476

edits