### 4 releases (2 breaking)

0.3.0 | Feb 24, 2020 |
---|---|

0.2.0 | Feb 23, 2020 |

0.1.1 | Aug 19, 2019 |

0.1.0 | Aug 19, 2019 |

#**704** in Rust patterns

**43** downloads per month

Used in **2** crates

**MIT**license

23KB

158 lines

# Higher Order Core

This crate contains core structs and traits for programming with higher order data structures.

### Introduction to higher order data structures

A higher order data structure is a generalization of an ordinary data structure.

In ordinary data structures, the default way of programming is:

- Use data structures for data
- Use methods/functions for operations on data

In a higher order data structure, data and functions become the same thing.

The central idea of a higher order data structure, is that properties can be functions of the same type.

For example, a

has an `Point`

, `x`

and `y`

property.
In ordinary programming, `z`

, `x`

and `y`

might have the type `z`

.`f64`

If

, `x`

and `y`

are functions from `z`

,
then the point type is `T -> f64`

`Point``<`T`>`

.A higher order

can be called, just like a function.
When called as a function, `Point <T>`

`Point``<`T`>`

returns `Point`

.However, unlike functions, you can still access properties of

.
You can also define methods and overload operators for `Point <T>`

`Point``<`T`>`

.
This means that in a higher order data structure, data and functions become the same thing.### Motivation of programming with higher order data structures

The major application of higher order data structures is geometry.

A typical usage is e.g. to create procedurally generated content for games.

Higher order data structures is about finding the right balance between hiding implementation details and exposing them for various generic algorithms.

For example, a circle can be thought of as having the type

.
The argument can be an angle in radians, or a value in the unit interval `Point <f64>`

`[``0``,` `1``]`

.Another example, a line can be thought of as having the type

.
The argument is a value in the unit interval `Point <f64>`

`[``0``,` `1``]`

.
When called with `0`

, you get the start point of the line.
When called with `1`

, you get the end point of the line.Instead of declaring a

type, a `Circle`

type and so on,
one can use `Line`

to represent both of them.`Point <f64>`

Higher order data structures makes easier to write generic algorithms for geometry. Although it seems abstract at first, it is also practically useful in unexpected cases.

For example, an animated point can be thought of as having the type

.
The first argument contains the animation data and the second argument is time in seconds.
Properties `Point <(&[Frame], f64)>`

`x`

, `y`

and `z`

of an animated point determines how the animated point is computed.
The details of the implementation can be hidden from the algorithm that uses animated points.Sometimes you need to work with complex geometry. In these cases, it is easier to work with higher order data structures.

For example, a planet might have a center, equator, poles, surface etc. A planet orbits around a star, which orbits around the center of a galaxy. This means that the properties of a planet, viewed from different reference frames, are functions of the arguments that determine the reference frame. You can create a "higher order planet" to reason about a planet's properties under various reference frames.

### Design

Here is an example of how to declare a new higher order data structure:

`use` `higher_order_core``::``{`Ho`,` Call`,` Arg`,` Fun`,` Func`}``;`
`use` `std``::``sync``::`Arc`;`
`///` Higher order 3D point.
`#``[``derive``(``Clone``)``]`
`pub` `struct` `Point``<`T = `(``)``>`` where f64: Ho<T> ``{`
`///` Function for x-coordinates.
`pub` `x``:` `Fun``<`T, `f64``>`,
`///` Function for y-coordinates.
`pub` `y``:` `Fun``<`T, `f64``>`,
`///` Function for z-coordinates.
`pub` `z``:` `Fun``<`T, `f64``>`,
`}`
`//` It is common to declare a type alias for functions, e.g:
`pub` `type` `PointFunc``<`T`>` `=` `Point``<`Arg`<`T`>``>``;`
`//` Implement `Ho<Arg<T>>` to allow higher order data structures
`//` using properties `Fun<T, Point>` (`<Point as Ho<T>>::Fun`).
`impl``<`T`:` `Clone``>`` ``Ho``<`Arg`<`T`>``>` `for`` ``Point` `{`
`type` `Fun` `=` `PointFunc``<`T`>``;`
`}`
`//` Implement `Call<T>` to allow higher order calls.
`impl``<`T`:` `Copy``>`` ``Call``<`T`>` `for`` ``Point`
`where` `f64``:` `Ho``<`Arg`<`T`>``>` + `Call``<`T`>`
`{`
`fn` `call``(``f``:` `&``Self``::`Fun, `val``:` T`)`` ``->` Point `{`
`Point``::``<``(``)``>` `{`
x`:` `<``f64` `as` Call`<`T`>``>``::`call`(``&`f`.`x`,` val`)``,`
y`:` `<``f64` `as` Call`<`T`>``>``::`call`(``&`f`.`y`,` val`)``,`
z`:` `<``f64` `as` Call`<`T`>``>``::`call`(``&`f`.`z`,` val`)``,`
`}`
`}`
`}`
`impl``<`T`>`` ``PointFunc``<`T`>` `{`
`///` Helper method for calling value.
`pub` `fn` `call``(``&``self`, `val``:` T`)`` ``->` Point `where` T`:` Copy `{`
`<`Point `as` `Call``<`T`>``>``::`call`(``self``,` val`)`
`}`
`}`
`//` Operations are usually defined as simple traits.
`//` They look exactly the same as for normal generic programming.
`///` Dot operator.
`pub` `trait` `Dot`<Rhs = Self> `{`
`///` The output type.
`type` `Output``;`
`///` Returns the dot product.
`fn` `dot``(``self`, `other``:` Rhs`)`` ``->` `Self``::`Output`;`
`}`
`//` Implement operator once for the ordinary case.
`impl` `Dot ``for`` ``Point` `{`
`type` `Output` `=` `f64``;`
`fn` `dot``(``self`, `other``:` `Self``)`` ``->` `f64` `{`
`self``.`x `*` other`.`x `+`
`self``.`y `*` other`.`y `+`
`self``.`z `*` other`.`z
`}`
`}`
`//` Implement operator once for the higher order case.
`impl``<`T`:` `'static` `+` `Copy``>`` Dot ``for`` ``PointFunc``<`T`>` `{`
`type` `Output` `=` `Func``<`T, `f64``>``;`
`fn` `dot``(``self`, `other``:` `Self``)`` ``->` `Func``<`T, `f64``>` `{`
`let` ax `=` `self``.`x`;`
`let` ay `=` `self``.`y`;`
`let` az `=` `self``.`z`;`
`let` bx `=` other`.`x`;`
`let` by `=` other`.`y`;`
`let` bz `=` other`.`z`;`
`Arc``::`new`(``move` `|`a`|` `ax``(`a`)` `*` `bx``(`a`)` `+` `ay``(`a`)` `*` `by``(`a`)` `+` `az``(`a`)` `*` `bz``(`a`)``)`
`}`
`}`

To disambiguate impls of e.g.

from `Point <()>`

`Point``<`T`>`

,
an argument type `Arg``<`T`>`

is used for point functions: `Point``<`Arg`<`T`>``>`

.For every higher order type

and and argument type `U`

,
there is an associated function type `T`

.`T -> U`

For primitive types, e.g.

, the function type is `f64`

.`Func <T, f64>`

For higher order structs, e.g.

, the function type is `X <()>`

`X``<`Arg`<`T`>``>`

.The code for operators on higher order data structures must be written twice:

- Once for the ordinary case
`X``<``(``)``>` - Once for the higher order case
`X``<`Arg`<`T`>``>`

### Higher Order Maps

Sometimes it is useful to construct arbitrary data of the kind:

- Vectors of primitives
- Vectors of vectors, etc.

For example, if a higher order point maps from angles to a circle, then complex geometry primitives might be defined onto the circle using angles:

- Edge, e.g.
`[`a`,`b`]` - Triangle, e.g.
`[`a`,`b`,`c`]` - Square, e.g.
`[``[`a`,`b`]``,``[`c`,`d`]``]`

The

method can be used to work with such structures.`HMap ::`hmap

For example, if

is a higher order point of type `p`

,
then the following code maps two points at the same time:`Point <Arg<f64>>`

`let q: [Point; 2] = [0.0, 1.0].hmap(&p);
`

For binary higher order maps of type

,
the `f : (T, T) -> U`

`HPair``::`hpair

method can be used before using `HMap``::`hmap

.For example:

`let in_between: Func<f64, f64> = Arc::new(move |(a, b)| {
if b < a {b += 1.0};
(a + (b - a) * 0.5) % 1.0
});
// Pair up.
let args: [(f64, f64); 2] = ([0.7, 0.9], [0.9, 0.1]).hpair();
// `[0.8, 0.0]`
let q: [f64; 2] = args.hmap(&in_between);
`