+mq: manx-query

a library of gates for transforming “collections”.

“collections” are lists of tapes, dimes, or manxs.

similar to jquery, which acts on collections of DOM data.

the intended usage pattern is to chain these functions together in arbitrary ways to ergonomically transform a $manx into a hoon noun.

the source code lives in /lib/hawk/hoon

in the following examples, the data face is bound to this manx:

fizz-buzz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
;div ;div.bold: fizz-buzz ;div.numbers.frw.g1 ;* %+ turn (gulf 1 20) |= n=@ =/ fiz ?: =(0 (~(sit fo 3) n)) "fizz" "" =/ buz ?: =(0 (~(sit fo 5) n)) "buzz" "" =; m=manx ?. =(n 8) m m(a.g [[%id "fave"] a.g.m]) ;div =class "number {fiz} {buz}" ; {(scow %ud n)} == == ==

first-class

up

$-([class=tape marl] marl) get the first descendant of each manx in the marl, that matches a class.

first element with .fizz class
%+ first-class "fizz" c.data
result: marl
3
;div.number.fizz. ;+ ;/ %- trip ''' 3 ''' ==

find-class

up

$-([class=tape marl] marl) get the all descendants of each manx in the marl, filtered by class.

all elements with .buzz
%+ find-class "buzz" c.data
result: marl
5
;div.number..buzz ;+ ;/ %- trip ''' 5 ''' ==
10
;div.number..buzz ;+ ;/ %- trip ''' 10 ''' ==
15
;div.number.fizz.buzz ;+ ;/ %- trip ''' 15 ''' ==
20
;div.number..buzz ;+ ;/ %- trip ''' 20 ''' ==
all elements with .fizz and .buzz
%+ find-class "buzz" %+ find-class "fizz" c.data
result: marl
15
;div.number.fizz.buzz ;+ ;/ %- trip ''' 15 ''' ==

as-tapes

up

$-(marl (list tape)) get the concatenated subtext of each manx in the marl. alias: text-content

example
%- as-tapes:mq c.data
result: (list tape)
"fizz-buzz"
"1\0a2\0a3\0a4\0a5\0a6\0a7\0a8\0a9\0a10\0a11\0a12\0a13\0a14\0a15\0a16\0a17\0a18\0a19\0a20\0a"
text of elements with .number
%- as-tapes:mq %+ find-class:mq "number" c.data
result: (list tape)
"1\0a"
"2\0a"
"3\0a"
"4\0a"
"5\0a"
"6\0a"
"7\0a"
"8\0a"
"9\0a"
"10\0a"
"11\0a"
"12\0a"
"13\0a"
"14\0a"
"15\0a"
"16\0a"
"17\0a"
"18\0a"
"19\0a"
"20\0a"

as-tape

up

$-(marl tape) get the fully concatenated subtext of the marl.

example
%- as-tape:mq c.data
"fizz-buzz1\0a2\0a3\0a4\0a5\0a6\0a7\0a8\0a9\0a10\0a11\0a12\0a13\0a14\0a15\0a16\0a17\0a18\0a19\0a20\0a"
text of .numbers
%- as-tape:mq %+ find-class:mq "numbers" c.data
"1\0a2\0a3\0a4\0a5\0a6\0a7\0a8\0a9\0a10\0a11\0a12\0a13\0a14\0a15\0a16\0a17\0a18\0a19\0a20\0a"

get-id

up

$-([id=tape manx] marl) start a collection with the element matching the id.

favorite number
%+ get-id "fave" data
result: marl
8
;div#fave.number.. ;+ ;/ %- trip ''' 8 ''' ==

find-id

up

$-([id=tape manx] marl) filter a collection to first element matching the id.

find #fave
%+ first-id "fave" c.data
result: marl
8
;div#fave.number.. ;+ ;/ %- trip ''' 8 ''' ==

dimes

up

$-((list tape) (list dime)) parse tape collection to dimes defaulting to head of %t.

all text in .number elements as dimes
%- dimes:mq %- text-content:mq %+ find-class:mq "number" c.data
result: (list dime)
[p=~.ud q=1]
[p=~.ud q=2]
[p=~.ud q=3]
[p=~.ud q=4]
[p=~.ud q=5]
[p=~.ud q=6]
[p=~.ud q=7]
[p=~.ud q=8]
[p=~.ud q=9]
[p=~.ud q=10]
[p=~.ud q=11]
[p=~.ud q=12]
[p=~.ud q=13]
[p=~.ud q=14]
[p=~.ud q=15]
[p=~.ud q=16]
[p=~.ud q=17]
[p=~.ud q=18]
[p=~.ud q=19]
[p=~.ud q=20]
summing all the valid numbers
=< q %- roll :_ |= [a=dime b=dime] ud/(add q.a q.b) %+ dimes-as:mq %ud %- text-content:mq %+ find-class:mq "number" c.data
210