~jojo/Carth

ref: ebf21d4a945812d85c63439ab2bbed1aa38e6080 Carth/std/io.carth -rw-r--r-- 965 bytes
ebf21d4aJoJo std: Update io/{map,bind} to work with new IO newtype 1 year, 3 months ago
                                                                                
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
(import std)

(define: (io/for xs f)
    (forall (a) (Fun (Iter a) (Fun a (IO Unit)) (IO Unit)))
  (IO (fun (real-world)
        [Unit (foldl (fun (real-world a) (cadr (perform-io (f a) real-world)))
                     real-world
                     xs)])))

(define (io/pure a) (IO (fun (real-world) [Unit real-world])))

(define: (io/map f (IO ma))
    (forall (a b) (Fun (Fun a b) (IO a) (IO b)))
  (IO (o> ma (map-car f))))

(define: (io/bind f (IO ma))
    (forall (a b) (Fun (Fun a (IO b)) (IO a) (IO b)))
  (IO (o> ma (uncurry (o> f perform-io)))))

(define io/bindr (flip io/bind))

(define: (unsafe-perform-io ma)
    (forall (a) (Fun (IO a) a))
  (car (perform-io ma UnsafeRealWorld)))

(define: (perform-io (IO ma))
    (forall (a) (Fun (IO a) RealWorld [a RealWorld]))
  ma)

(extern unsafe-display-inline (Fun Str Unit))

(define (display-inline s)
  (io/pure (unsafe-display-inline s)))

(define (display s)
  (display-inline (str-append s "\n")))