~jojo/Carth

ref: 749208029494a7c48ce04444a513424f4b998416 Carth/std/lazy.carth -rw-r--r-- 526 bytes
74920802JoJo update TODO 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
(import macros)
(import sync)
(import io)

(data (Lazy a)
  (Lazy [Mutex (Box (Either (Fun Unit a) a))]))

(define: (strict (Lazy [mx ptr]))
    (forall (a) (Fun (Lazy a) a))
  (unsafe-perform-io
   (io/thenr
    (mutex/lock mx)
    (match (deref ptr)
      (case (Left f)
            (let ((x (f Unit)))
              (apps io/thenr
                    (io/write-ref (Right x) ptr)
                    (mutex/release mx)
                    (io/pure x))))
      (case (Right x) (io/thenr (mutex/release mx) (io/pure x)))))))