Jaccard index: Difference between revisions

m
syntax highlighting fixup automation
(Initial Haskell version.)
m (syntax highlighting fixup automation)
Line 18:
 
=={{header|APL}}==
<langsyntaxhighlight lang="apl">task←{
jaccard ← (≢∩)÷(≢∪)
 
Line 29:
 
'.ABCDEF' ⍪ 'ABCDEF' , ∘.jaccard⍨ A B C D E F
}</langsyntaxhighlight>
{{out}}
<pre>. A B C D E F
Line 40:
 
=={{header|BQN}}==
<langsyntaxhighlight lang="bqn">Jaccard ← ≡◶⟨∊ ÷○(+´) ∊∘∾, 1⟩
 
a ← ⟨⟩
Line 49:
f ← ⟨8⟩
 
Jaccard⌜˜ ⟨a,b,c,d,e,f⟩</langsyntaxhighlight>
{{out}}
<pre>┌─
Line 62:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: assocs formatting grouping kernel math math.combinatorics
prettyprint sequences sequences.repeating sets ;
 
Line 71:
{ { } { 1 2 3 4 5 } { 1 3 5 7 9 } { 2 4 6 8 10 } { 2 3 5 7 } { 8 } }
[ 2 <combinations> ] [ 2 repeat 2 group append ] bi
[ 2dup jaccard "%u %u -> %u\n" printf ] assoc-each</langsyntaxhighlight>
{{out}}
<pre>
Line 98:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Applicative (liftA2)
import Data.List (genericLength, intersect, nub, union)
import Data.List.Split (chunksOf)
Line 146:
, ("D", [2, 4, 6, 8, 10])
, ("E", [2, 3, 5, 7])
, ("F", [8])]</langsyntaxhighlight>
{{out}}
<pre>
Line 175:
<br>
'''Preliminaries'''
<langsyntaxhighlight lang="jq">def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def gcd(a; b):
Line 183:
else [.[1], .[0] % .[1]] | rgcd
end;
[a,b] | rgcd;</langsyntaxhighlight>
<br>
'''The Task'''
<langsyntaxhighlight lang="jq">def rjaccardIndex(x; y):
def i(a;b): a - (a-b);
def u(a;b): a + (b - i(a;b)) | unique;
Line 221:
| " \($name): \(.)" ) ;
 
task</langsyntaxhighlight>
{{out}}
<pre>
Line 234:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">J(A, B) = begin i, u = length(A ∩ B), length(A ∪ B); u == 0 ? 1//1 : i // u end
 
A = Int[]
Line 249:
replace(string(J(a, b)), "//" => "/"))
end
</langsyntaxhighlight>{{out}}
<pre>
Set A Set B J(A, B)
Line 292:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">sets</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 321:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 348:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
Line 372:
%union ? (grep $_ == 2, values %union) / (keys %union) : 1, "\n";
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 422:
 
=={{header|Prolog}}==
<langsyntaxhighlight lang="prolog">
show([]).
show([X|Xs]):- write(X), show(Xs).
Line 433:
findall(X, (member(X,B), not(member(X,A))), T), append(A,T,U), length(U,M),
j(N,M,J), show(["A = ",A,", B = ",B,", J = ",J]), nl)).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 477:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>sub J(\A, \B) { A ∪ B ?? (A ∩ B) / (A ∪ B) !! A ∪ B == A ∩ B ?? 1 !! 0 }
 
my %p =
Line 490:
.say for %p.sort;
say '';
say "J({.join: ','}) = ", J |%p{$_} for [X] <A B C D E F> xx 2;</langsyntaxhighlight>
{{out}}
<pre>A => ()
Line 541:
{{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.
<langsyntaxhighlight lang="ecmascript">import "./set" for Set
import "./trait" for Indexed
import "./fmt" for Fmt
Line 572:
Fmt.print("J($s, $s) = $h", i1, i2, jaccardIndex.call(v1, v2))
}
}</langsyntaxhighlight>
 
{{out}}
10,327

edits