Aliases and holes¶
Many small functions are a few pipeline steps, or an existing function with one argument fixed. HQL has a short form for each.
Aliases¶
:= defines an alias: a name for a pipeline fragment. The alias is used
as a step:
top(n) := sort | take(n)
[412, 280, 310, 145] | top(2)
[145, 280]
An alias has no written types. The checker infers its signature from the
steps. The input is the most general type the fragment accepts, and each
parameter has the type of the argument it fills. hql check prints the
signature:
top(n) := sort | take(n)
1
top : (Collection<T>, Int) -> List<T> where T : Orderable
Int
sort needs an order, so the input is a Collection<T> with
T : Orderable. take(n) needs an Int, so n is an Int.
Holes¶
A call with _ in place of an argument is a partial application. The
result is a function that takes the missing arguments:
fn days(pages: Int, per_day: Int) : Int = (pages + per_day - 1) / per_day
[412, 280] | map(days(_, 40))
[11, 7]
days(_, 40) is a function of one Int, the number of pages. A hole stands
for a whole argument. _ + 1 is not a function; write the lambda
n => n + 1.