distinct
Builtin index · Collections & strings
result = distinct(xs)
Input: xs · Output: result
What it is
xs is a Vec/List.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = distinct(xs) | xs Vec or Listnot documented yet | result — | Returns the same type: the unique values, in the order they first appear — which unique does not promise, since it sorts. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
G = vstack(randn(40, 2, seed = 31) + 4,
randn(40, 2, seed = 32) - 4,
randn(40, 2, seed = 33))
d = dbscan(G, 1.5, 4)
print("dbscan labels: {distinct(d)}")
proj = pca(G, 2) # the (n, 2) projected coordinates
comps = pca_components(G, 2) # the (2, 2) loading matrix
ev = pca_explained_variance(G, 2)
print("pca projected shape: {rows(proj)} x {cols(proj)}")
print("pca components:\n{round(comps, 3)}")
print("explained variance: {round(ev, 3)}")
small = vstack(randn(6, 2, seed = 41) + 3, randn(6, 2, seed = 42) - 3)
low = tsne(small, n_components = 2, perplexity = 3, seed = 1, max_iter = 100)
print("tsne output rows: {rows(low)}")
output
dbscan labels: (0, 1, 2) pca projected shape: 120 x 2 pca components: [0.694, 0.72; 0.72, -0.694] explained variance: [0.969, 0.031] tsne output rows: 12
from statistics-ml
function is_even(x)
return x mod 2 == 0
end function
function add2(a, b)
return a + b
end function
function double(x)
return x * 2
end function
xs = [1, 2, 3, 4, 5]
print(all(xs, is_even)) # false
print(any(xs, is_even)) # true
print(first(xs)) # 1
print(last(xs)) # 5
print(take(xs, 3)) # (1, 2, 3)
print(distinct([1, 1, 2, 2, 3])) # (1, 2, 3) -- first-seen order
print(flatten([[1, 2], [3, 4]])) # (1, 2, 3, 4)
print(fold(xs, add2, 0)) # 15 -- with an initial value
print(reduce(xs, add2)) # 15 -- without one
print(map(xs, double)) # (2, 4, 6, 8, 10) -- collection first
print(map(xs, (x) := x * 10)) # (10, 20, 30, 40, 50) -- or write it inline
print(map(xs, "double")) # (2, 4, 6, 8, 10) -- the name as a string still works
print(xs.map(double)) # (2, 4, 6, 8, 10) -- and so the method form works
print(zip([1, 2, 3], ["a", "b", "c"])) # ((1, "a"), (2, "b"), (3, "c"))
output
false true 1 5 (1, 2, 3) (1, 2, 3) (1, 2, 3, 4) 15 15 (2, 4, 6, 8, 10) (10, 20, 30, 40, 50) (2, 4, 6, 8, 10) (2, 4, 6, 8, 10) ((1, "a"), (2, "b"), (3, "c"))
from collections-strings