Skip to content

Commit 88523dc

Browse files
committed
Small simplification of Resource.use
1 parent b7f22a2 commit 88523dc

File tree

3 files changed

+35
-56
lines changed

3 files changed

+35
-56
lines changed

docs/api/resource.html

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,11 @@ <h1 class="title">Module <code>raffiot.resource</code></h1>
119119

120120
def safe_use(x: Tuple[A, IO[R, E, None]]) -&gt; IO[R, E, X]:
121121
a, close = x
122-
try:
123-
return (
124-
fun(a)
125-
.attempt()
126-
.flat_map(lambda r: close.attempt().then(io.from_result(r)))
127-
)
128-
except Exception as exception:
129-
return close.attempt().then(io.panic(exception))
122+
return (
123+
io.defer_io(fun, a)
124+
.attempt()
125+
.flat_map(lambda r: close.attempt().then(io.from_result(r)))
126+
)
130127

131128
return self.create.flat_map(safe_use)
132129

@@ -1257,14 +1254,11 @@ <h2 class="section-title" id="header-classes">Classes</h2>
12571254

12581255
def safe_use(x: Tuple[A, IO[R, E, None]]) -&gt; IO[R, E, X]:
12591256
a, close = x
1260-
try:
1261-
return (
1262-
fun(a)
1263-
.attempt()
1264-
.flat_map(lambda r: close.attempt().then(io.from_result(r)))
1265-
)
1266-
except Exception as exception:
1267-
return close.attempt().then(io.panic(exception))
1257+
return (
1258+
io.defer_io(fun, a)
1259+
.attempt()
1260+
.flat_map(lambda r: close.attempt().then(io.from_result(r)))
1261+
)
12681262

12691263
return self.create.flat_map(safe_use)
12701264

@@ -1857,14 +1851,11 @@ <h3>Methods</h3>
18571851

18581852
def safe_use(x: Tuple[A, IO[R, E, None]]) -&gt; IO[R, E, X]:
18591853
a, close = x
1860-
try:
1861-
return (
1862-
fun(a)
1863-
.attempt()
1864-
.flat_map(lambda r: close.attempt().then(io.from_result(r)))
1865-
)
1866-
except Exception as exception:
1867-
return close.attempt().then(io.panic(exception))
1854+
return (
1855+
io.defer_io(fun, a)
1856+
.attempt()
1857+
.flat_map(lambda r: close.attempt().then(io.from_result(r)))
1858+
)
18681859

18691860
return self.create.flat_map(safe_use)</code></pre>
18701861
</details>

docs/api/untyped/resource.html

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,11 @@ <h1 class="title">Module <code>raffiot.untyped.resource</code></h1>
107107

108108
def safe_use(x):
109109
a, close = x
110-
try:
111-
return (
112-
fun(a)
113-
.attempt()
114-
.flat_map(lambda r: close.attempt().then(io.from_result(r)))
115-
)
116-
except Exception as exception:
117-
return close.attempt().then(io.panic(exception))
110+
return (
111+
io.defer_io(fun, a)
112+
.attempt()
113+
.flat_map(lambda r: close.attempt().then(io.from_result(r)))
114+
)
118115

119116
return self.create.flat_map(safe_use)
120117

@@ -1208,14 +1205,11 @@ <h2 class="section-title" id="header-classes">Classes</h2>
12081205

12091206
def safe_use(x):
12101207
a, close = x
1211-
try:
1212-
return (
1213-
fun(a)
1214-
.attempt()
1215-
.flat_map(lambda r: close.attempt().then(io.from_result(r)))
1216-
)
1217-
except Exception as exception:
1218-
return close.attempt().then(io.panic(exception))
1208+
return (
1209+
io.defer_io(fun, a)
1210+
.attempt()
1211+
.flat_map(lambda r: close.attempt().then(io.from_result(r)))
1212+
)
12191213

12201214
return self.create.flat_map(safe_use)
12211215

@@ -1780,14 +1774,11 @@ <h3>Methods</h3>
17801774

17811775
def safe_use(x):
17821776
a, close = x
1783-
try:
1784-
return (
1785-
fun(a)
1786-
.attempt()
1787-
.flat_map(lambda r: close.attempt().then(io.from_result(r)))
1788-
)
1789-
except Exception as exception:
1790-
return close.attempt().then(io.panic(exception))
1777+
return (
1778+
io.defer_io(fun, a)
1779+
.attempt()
1780+
.flat_map(lambda r: close.attempt().then(io.from_result(r)))
1781+
)
17911782

17921783
return self.create.flat_map(safe_use)</code></pre>
17931784
</details>

raffiot/resource.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,11 @@ def use(self, fun: Callable[[A], IO[R, E, X]]) -> IO[R, E, X]:
8888

8989
def safe_use(x: Tuple[A, IO[R, E, None]]) -> IO[R, E, X]:
9090
a, close = x
91-
try:
92-
return (
93-
fun(a)
94-
.attempt()
95-
.flat_map(lambda r: close.attempt().then(io.from_result(r)))
96-
)
97-
except Exception as exception:
98-
return close.attempt().then(io.panic(exception))
91+
return (
92+
io.defer_io(fun, a)
93+
.attempt()
94+
.flat_map(lambda r: close.attempt().then(io.from_result(r)))
95+
)
9996

10097
return self.create.flat_map(safe_use)
10198

0 commit comments

Comments
 (0)