Skip to content

Commit

Permalink
New examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
athas committed May 2, 2024
1 parent 2422d14 commit ac9f05c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
2 changes: 2 additions & 0 deletions examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ for plotting or rendering graphics.
- [Matrix multiplication](examples/matrix-multiplication.html)
- [Pairwise L₁ distances](examples/pairwise-l1.html)
- [Outer product](examples/outer-product.html)
- [Comparing arrays for equality](examples/array-equality.html)
- [Searching](examples/searching.html)
- [Computing histograms](examples/histograms.html)
- [Moving average](examples/moving-average.html)
- [Means](examples/means.html)
- [Integer logarithm](examples/integer-logarithm.html)
- [Radix sort](examples/radix-sort.html)
- [Radix sort by key](examples/radix-sort-key.html)
- [Merge sort](examples/merge-sort.html)
Expand Down
2 changes: 1 addition & 1 deletion examples/3d-vectors.fut
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ module f64_3d = mk_vspace_3d f64
-- {x = -3.0f64, y = 6.0f64, z = -3.0f64}
-- ```

-- # See also
-- ## See also
--
-- [Dex: Multi-step ray tracer](dex-raytrace.html)
--
Expand Down
34 changes: 34 additions & 0 deletions examples/array-equality.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- # Comparing arrays for equality

-- To compare two one-dimensional arrays for equality, we can compare
-- them elementwise, yielding an array of `bool`s, then use `and` to
-- check whether all those elements are true:

def eq_arr_i32 [n] (a: [n]i32) (b: [n]i32) : bool =
and (map2 (==) a b)

-- This function requires us to already know that the arrays have the
-- same size. We can also write a variant that does not have this
-- requirement, but instead checks for the size explicitly and then
-- uses a [size coercion](size-coercions.html):

def eq_arr_i32_any [n] [m] (a: [n]i32) (b: [m]i32) : bool =
n == m && eq_arr_i32 a (b :> [n]i32)

-- This implementation is still restricted to arrays of integers. We
-- can make a polymorphic one that expects the user to provide an
-- equality function:

def eq_arr 'a [n] [m] (eq: a -> a -> bool) (a: [n]a) (b: [m]a) : bool =
n == m && and (map2 eq a (b :> [n]a))

-- This can then be used to define a function that works for
-- two-dimensional arrays:

def eq_arr_2d 'a [n1] [n2] [m1] [m2]
(eq: a -> a -> bool) (a: [n1][n2]a) (b: [m1][m2]a) : bool =
n1 == m1 && n2 == m2 && eq_arr (eq_arr eq) a (b :> [n1][n2]a)

-- ## See also
--
-- [Searching](searching.html).
15 changes: 15 additions & 0 deletions examples/integer-logarithm.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- # Integer logarithm
--
-- The base-2 integer logarithm can be computed by counting the number
-- of leading *zero bits* in the integer, through the `clz` builtin
-- function. For example for 64-bit integers:

def ilog2 (n: i64) : i64 = i64.i32 (63 - i64.clz n)

-- > ilog2 127

-- > ilog2 128

-- ## See also
--
-- [Merge sort](merge-sort.html).
2 changes: 1 addition & 1 deletion examples/size-coercions.fut
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ def B = [1,2,3] ++ [4,5,6] :> [6]i32

-- # See also
--
-- [Complex ranges](complex-ranges.html)
-- [Complex ranges](complex-ranges.html), [Comparing arrays for equality](array-equality.html).

0 comments on commit ac9f05c

Please sign in to comment.