+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:
;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)}
==
==
==
$-([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
;div.number.fizz.
;+ ;/ %- trip
'''
3
'''
==
$-([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
;div.number..buzz
;+ ;/ %- trip
'''
5
'''
==
;div.number..buzz
;+ ;/ %- trip
'''
10
'''
==
;div.number.fizz.buzz
;+ ;/ %- trip
'''
15
'''
==
;div.number..buzz
;+ ;/ %- trip
'''
20
'''
==
all elements with .fizz and .buzz
%+ find-class "buzz"
%+ find-class "fizz"
c.data
result: marl
;div.number.fizz.buzz
;+ ;/ %- trip
'''
15
'''
==
$-(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"
$-(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"
$-([id=tape manx] marl)
start a collection with
the element matching the id.
favorite number
%+ get-id "fave"
data
result: marl
;div#fave.number..
;+ ;/ %- trip
'''
8
'''
==
$-([id=tape manx] marl)
filter a collection to first
element matching the id.
find #fave
%+ first-id "fave"
c.data
result: marl
;div#fave.number..
;+ ;/ %- trip
'''
8
'''
==
$-((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