Functions as operands

A final picks one of nine built-in behaviors. Sometimes you need a behavior Garo does not have a final for. This page shows how to hand an operator your own function. You will learn:

  • what a syllable with no final (an open syllable) does;
  • how to pass it a math sign, such as +;
  • how to write a function of your own in braces, a block;
  • the combinators that wire functions together: ., ~, @, trains and <<.

Open syllables

A syllable with no final is the bare form. It has a family and a flow, but no behavior yet. You supply the behavior as a function, glued on its left. The syllable is then open: it is waiting for that function.

+다 [1, 2, 3, 4]
⇒ 10Try it

다 is reduce, along rows, with no final. +다 reduces by +. That is the same as 닫, and Garo treats it as the same operation. Where a final exists for what you want, use it. The bare form earns its keep when no final fits. A reduce by subtraction folds from the left, (10 − 1) − 2:

-다 [10, 1, 2]
⇒ 7Try it

The math signs you can pass are + - × ÷, ⌈ (the larger of two) and ⌊ (the smaller of two), and the comparisons < > ≤ ≥ = ≠. They also work between two values:

3 ⌈ 8
⇒ 8Try it

Blocks

A block is a function written in braces. Inside it, x is the argument:

{x × x}기 [1, 2, 3]
⇒ [1, 4, 9]Try it

기 is map, each element, bare: apply the function to every element. In Python this is [x * x for x in xs].

A block with two arguments calls them x and y. A reduce passes the running value as x and the next element as y:

{x × y}다 [2, 3, 4]
⇒ 24Try it

You can name the arguments yourself, with ⇒ between the names and the body:

{price ⇒ price × 2}기 [10, 20]
⇒ [20, 40]Try it

Remember that a block reads right to left too, so {x × x + 1} is x × (x + 1). Write {(x × x) + 1} for the square plus one.

The flow decides what the function receives. 기 hands it each element, 가 each row, and 거 each column:

{닥 x}가 [[3, 9], [7, 2]]
⇒ [9, 7]Try it

A block can also be the test for the select family. 니 keeps the elements for which the block gives true, like a SQL WHERE or NumPy's a[a > 2]:

{x > 2}니 [1, 2, 3, 4]
⇒ [3, 4]Try it

A block applied directly to a value is a function call:

{x × x} 7
⇒ 49Try it

A syllable as an operand

An open syllable glued to a whole word takes the whole word as its function. Here 가 runs "sort, then keep the first two" on each row, which keeps each row's two smallest values:

산님2가 [[3, 1, 2], [9, 7, 8]]
⇒ [[1, 2], [7, 8]]Try it

A syllable with its parameter becomes a function when you wrap it in parentheses: (긱 0) is "the larger of each element and 0".

(긱 0) [-1, 4]
⇒ [0, 4]Try it

Combinators

A combinator wires functions together without naming their inputs. Garo spells them in plain ASCII, so you can tell the wiring (ASCII) from the work (Hangul) at a glance.

Compose: .

F.G is F, then G. Every dot feeds its left side into its right side, which is why the pipe also uses a dot.

(긱 0).닫 [-1, 2, 3]
⇒ 5Try it

Clamp below 0, then sum: 0 + 2 + 3.

Self and flip: ~

~ after a two-input function changes how it gets its inputs. With one input, it uses that input on both sides: ×~ squares.

×~기 [1, 2, 3]
⇒ [1, 4, 9]Try it

Between two inputs, ~ swaps them: a -~ b is b − a.

3 -~ 10
⇒ 7Try it

Over: @

F @ G runs G on each of two inputs, then combines the results with F. "Transform both sides the same way, then compare":

[1, 2, 3] - @ 닫 [4, 5]
⇒ -3Try it

That is (닫 [1, 2, 3]) − (닫 [4, 5]), or 6 − 9.

Trains

Functions alone in parentheses make a train. Three functions make a fork: (F G H) x is (F x) G (H x). The classic one is the mean, sum divided by count:

(닫 ÷ 담) [1, 2, 3, 4]
⇒ 2.5Try it

Another fork gives the range, the largest minus the smallest:

(닥 - 단) [3, 9, 1, 4]
⇒ 8Try it

Two functions make an atop: (F G) x is F of (G x). It is compose written in math order, so (깃 닫) is "negate the sum". 깃 is map with the flip final: it negates numbers.

(깃 닫) [1, 2]
⇒ -3Try it

Math order: <<

A << B is B, then A, read "A of B". It suits a chain you would write as f(g(h(x))) on paper:

({x × 10} << {x + 1}) 3
⇒ 40Try it

What you have learned

  • An open syllable takes a function glued on its left.
  • A block {…} is a function of x (and y), or of names you give before ⇒.
  • . composes in reading order, ~ duplicates or swaps inputs, @ transforms both sides, and parentheses of functions make trains.

Next, keyed data, tables, absence and errors moves from arrays to the kind of data a SQL table holds.