(.require [library [lux (.except list nat int rev symbol type) [abstract [monad (.only do)] [functor ["[0]" contravariant]]] [control ["<>" parser]] [data ["[0]" bit] ["[0]" text] [format ["[0]" xml] ["[0]" json]] [collection ["[0]" list (.use "[1]#[0]" monad)]]] ["[0]" time (.only) ["[0]" instant] ["[0]" duration] ["[0]" date] ["[0]" day] ["[0]" month]] [math ["[0]" modular] [number ["[0]" nat] ["[0]" int] ["[0]" rev] ["[0]" frac] ["[0]" ratio]]] [macro [syntax (.only syntax)] ["[0]" template]] [meta ["[0]" location] ["[0]" symbol] ["[0]" type] ["[0]" code (.only) ["<[1]>" \\parser (.only Parser)]]]]]) (.type .public (Format a) (-> a Text)) (def .public functor (contravariant.Functor Format) (implementation (def (each f fb) (|>> f fb)))) (def .public format (syntax (_ [fragments (<>.many .any)]) (in (.list (` (all "lux text concat" (,* fragments))))))) (with_template [ ] [(def .public (Format ) )] [bit Bit (at bit.codec encoded)] [nat Nat (at nat.decimal encoded)] [int Int (at int.decimal encoded)] [rev Rev (at rev.decimal encoded)] [frac Frac (at frac.decimal encoded)] [text Text text.format] [ratio ratio.Ratio (at ratio.codec encoded)] [symbol Symbol (at symbol.codec encoded)] [location Location location.format] [code Code code.format] [type Type type.format] [instant instant.Instant (at instant.codec encoded)] [duration duration.Duration (at duration.codec encoded)] [date date.Date (at date.codec encoded)] [time time.Time (at time.codec encoded)] [day day.Day (at day.codec encoded)] [month month.Month (at month.codec encoded)] [xml xml.XML (at xml.codec encoded)] [json json.JSON (at json.codec encoded)] ) (with_template [ ,] [(`` (with_template [ ] [(def .public (Format ) (at encoded))] (,, (template.spliced ,))))] [Nat [[nat_2 nat.binary] [nat_8 nat.octal] [nat_10 nat.decimal] [nat_16 nat.hex]]] [Int [[int_2 int.binary] [int_8 int.octal] [int_10 int.decimal] [int_16 int.hex]]] [Rev [[rev_2 rev.binary] [rev_8 rev.octal] [rev_10 rev.decimal] [rev_16 rev.hex]]] [Frac [[frac_2 frac.binary] [frac_8 frac.octal] [frac_10 frac.decimal] [frac_16 frac.hex]]] ) (def .public (mod modular) (All (_ m) (Format (modular.Mod m))) (let [codec (modular.codec (modular.modulus modular))] (at codec encoded modular))) (def .public (list formatter) (All (_ a) (-> (Format a) (Format (List a)))) (|>> (list#each (|>> formatter (format " "))) text.together (text.enclosed ["(list" ")"]))) (def .public (maybe format) (All (_ a) (-> (Format a) (Format (Maybe a)))) (function (_ value) (case value {.#None} "{.#None}" {.#Some value} (..format "{.#Some " (format value) "}"))))