-module(shelf). -export([rnt_init/0, titles/0, authors/0]). -define(TITLES, [<<"karuta">>, <<"program">>, <<"titles">>]). -define(BOOKS, {$b, [<<"karuta">>, <<"program">>, <<"books">>]}). %% A FOL program over a beam program RNT holds, defined from the node. rnt_init() -> rnt:define(?TITLES, <<"fol">>, project(?BOOKS, [<<"title">>])). titles() -> rnt:invoke(?TITLES). authors() -> rnt:evaluate(<<"fol">>, project(?BOOKS, [<<"author">>])). project(Term, Attributes) -> bencode({$p, #{<<"term">> => Term, <<"attributes">> => Attributes}}). bencode(Bytes) when is_binary(Bytes) -> [integer_to_binary(byte_size(Bytes)), $:, Bytes]; bencode(List) when is_list(List) -> [$l, [bencode(X) || X <- List], $e]; bencode(Map) when is_map(Map) -> [$d, [[bencode(K), bencode(V)] || {K, V} <- lists:sort(maps:to_list(Map))], $e]; bencode({Tag, Value}) -> [$t, Tag, bencode(Value)].