blob: 531bd282b557b85049602bda0282a05f93144f5b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
(.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 <code>.any)])
(in (.list (` (all "lux text concat" (,* fragments)))))))
(with_template [<name> <type> <formatter>]
[(def .public <name>
(Format <type>)
<formatter>)]
[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 [<type> <format>,<codec>]
[(`` (with_template [<format> <codec>]
[(def .public <format>
(Format <type>)
(at <codec> encoded))]
(,, (template.spliced <format>,<codec>))))]
[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) "}"))))
|