:- :~ navhome/'/docs/' sort/'19' title/'2p: Serialization' == ;> # 2p: serialization ### `++cue` Unpack atom to noun Produces a noun unpacked from atom `a`. The inverse of `jam`. #### Accepts An Atom. #### Produces A noun. #### Source ``` ++ cue ~/ %cue |= a=@ ^- * =+ b=0 =+ m=`(map @ *)`~ =< q |- ^- [p=@ q=* r=(map @ *)] ?: =(0 (cut 0 [b 1] a)) =+ c=(rub +(b) a) [+(p.c) q.c (~(put by m) b q.c)] =+ c=(add 2 b) ?: =(0 (cut 0 [+(b) 1] a)) =+ u=$(b c) =+ v=$(b (add p.u c), m r.u) =+ w=[q.u q.v] [(add 2 (add p.u p.v)) w (~(put by r.v) b w)] =+ d=(rub c a) [(add 2 p.d) (need (~(get by m) q.d)) m] ``` #### Examples ``` > (cue 12) 1 > (cue 817) [1 1] > (cue 4.657) [1 2] > (cue 39.689) [0 19] ``` --- ### `++jam` Pack noun to atom Produces an atom packed from noun `a`. The inverse of `cue`. #### Accepts `a` is a noun. #### Produces An atom. #### Source ``` ++ jam ~/ %jam |= a=* ^- @ =+ b=0 =+ m=`(map * @)`~ =< q |- ^- [p=@ q=@ r=(map * @)] =+ c=(~(get by m) a) ?~ c => .(m (~(put by m) a b)) ?: ?=(@ a) =+ d=(mat a) [(add 1 p.d) (lsh 0 1 q.d) m] => .(b (add 2 b)) =+ d=$(a -.a) =+ e=$(a +.a, b (add b p.d), m r.d) [(add 2 (add p.d p.e)) (mix 1 (lsh 0 2 (cat 0 q.d q.e))) r.e] ?: ?&(?=(@ a) (lte (met 0 a) (met 0 u.c))) =+ d=(mat a) [(add 1 p.d) (lsh 0 1 q.d) m] =+ d=(mat u.c) [(add 2 p.d) (mix 3 (lsh 0 2 q.d)) m] ``` #### Examples ``` > (jam 1) 12 > (jam [1 1]) 817 > (jam [1 2]) 4.657 > (jam [~ u=19]) 39.689 ``` #### Discussion `mat` is only used internally as a helper to `jam`. --- ### `++mat` Length-encode Produces a cell whose tail `q` is atom `a` with a bit representation of its length prepended to it (as the least significant bits). The head `p` is the length of `q` in bits. #### Accepts `a` is an atom. #### Produces A cell of two atoms. #### Source ``` ++ mat ~/ %mat |= a=@ ^- [p=@ q=@] ?: =(0 a) [1 1] =+ b=(met 0 a) =+ c=(met 0 b) :- (add (add c c) b) (cat 0 (bex c) (mix (end 0 (dec c) b) (lsh 0 (dec c) a))) ``` #### Examples ``` > (mat 0xaaa) [p=20 q=699.024] > (met 0 q:(mat 0xaaa)) 20 > `@ub`q:(mat 0xaaa) 0b1010.1010.1010.1001.0000 > =a =-(~&(- -) `@ub`0xaaa) 0b1010.1010.1010 > =b =-(~&(- -) `@ub`(xeb a)) 0b1100 > =b =-(~&(- -) `@ub`(met 0 a)) 0b1100 > =c =-(~&(- -) (xeb b)) 4 > [`@ub`a `@ub`(end 0 (dec c) b) `@ub`(bex c)] [0b1010.1010.1010 0b100 0b1.0000] ``` --- ### `++rub` Length-decode The inverse of `mat`. Accepts a cell of index `a` and a bitstring `b` and produces the cell whose tail `q` is the decoded atom at index `a` and whose head is the length of the encoded atom `q`, by which the offset `a` is advanced. #### Accepts `a` is an atom. `b` is a bitstring as an atom. #### Produces A cell of two atoms. #### Source ``` ++ rub ~/ %rub |= [a=@ b=@] ^- [p=@ q=@] =+ ^= c =+ [c=0 m=(met 0 b)] |- ?< (gth c m) ?. =(0 (cut 0 [(add a c) 1] b)) c $(c +(c)) ?: =(0 c) [1 0] =+ d=(add a +(c)) =+ e=(add (bex (dec c)) (cut 0 [d (dec c)] b)) [(add (add c c) e) (cut 0 [(add d (dec c)) e] b)] ``` #### Examples ``` > `@ub`(jam 0xaaa) 0b1.0101.0101.0101.0010.0000 > (rub 1 0b1.0101.0101.0101.0010.0000) [p=20 q=2.730] > `@ux`q:(rub 1 0b1.0101.0101.0101.0010.0000) 0xaaa ``` #### Discussion `rub` is only used internally as a helper to `cue`. ---