aboutsummaryrefslogtreecommitdiff
path: root/source/lux/data/writer.lux
blob: 3bf99c1ade5f56d879261f2d1b087d6fb8435961 (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
##  Copyright (c) Eduardo Julian. All rights reserved.
##  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
##  If a copy of the MPL was not distributed with this file,
##  You can obtain one at http://mozilla.org/MPL/2.0/.

(;import lux
         (lux/control (monoid #as m #refer #all)
                      (functor #as F #refer #all)
                      (monad #as M #refer #all)))

## [Types]
(deftype #export (Writer l a)
  (, l a))

## [Structures]
(defstruct #export Writer/Functor (All [l]
                                    (Functor (Writer l)))
  (def (map f fa)
    (let [[log datum] fa]
      [log (f datum)])))

(defstruct #export (Writer/Monad mon) (All [l]
                                        (-> (Monoid l) (Monad (Writer l))))
  (def _functor Writer/Functor)

  (def (wrap x)
    [(:: mon unit) x])

  (def (join mma)
    (let [[log1 [log2 a]] mma]
      [(:: mon (++ log1 log2)) a])))