Skip to content

Commit d976053

Browse files
committed
extract more effects
1 parent 5317e2f commit d976053

14 files changed

Lines changed: 192 additions & 275 deletions

File tree

packages/touch_grass/src/touch_grass.gleam

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import eyg/interpreter/cast
22
import gleam/http/request.{type Request}
3+
import gleam/uri
34
import touch_grass/abort
45
import touch_grass/copy
56
import touch_grass/decode_json
67
import touch_grass/download
78
import touch_grass/fetch
9+
import touch_grass/flip
810
import touch_grass/interface.{type Interface, Interface}
911
import touch_grass/paste
1012
import touch_grass/print
13+
import touch_grass/prompt
14+
import touch_grass/visit
1115

1216
pub fn abort() -> Interface(String, a, b) {
1317
Interface(abort.label, abort.lift(), abort.lower(), abort.decode)
@@ -39,6 +43,10 @@ pub fn fetch() -> Interface(Request(BitArray), a, b) {
3943
Interface(fetch.label, fetch.lift(), fetch.lower(), fetch.decode)
4044
}
4145

46+
pub fn flip() -> Interface(Nil, a, b) {
47+
Interface(flip.label, flip.lift(), flip.lower(), flip.decode)
48+
}
49+
4250
pub fn paste() -> Interface(Nil, a, b) {
4351
Interface(paste.label, paste.lift(), paste.lower(), paste.decode)
4452
}
@@ -47,6 +55,14 @@ pub fn print() -> Interface(String, a, b) {
4755
Interface(print.label, print.lift(), print.lower(), print.decode)
4856
}
4957

58+
pub fn prompt() -> Interface(String, a, b) {
59+
Interface(prompt.label, prompt.lift(), prompt.lower(), prompt.decode)
60+
}
61+
62+
pub fn visit() -> Interface(uri.Uri, a, b) {
63+
Interface(visit.label, visit.lift(), visit.lower(), visit.decode)
64+
}
65+
5066
pub fn map(interface: Interface(t, a, b), f: fn(t) -> u) -> Interface(u, a, b) {
5167
let Interface(decode:, ..) = interface
5268
let decode = cast.map(decode, f)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import eyg/analysis/type_/isomorphic as t
2+
import eyg/interpreter/cast
3+
import gleam/int
4+
5+
pub const label = "Flip"
6+
7+
pub fn lift() {
8+
t.unit
9+
}
10+
11+
pub fn lower() {
12+
t.boolean
13+
}
14+
15+
pub fn decode(input) {
16+
cast.as_unit(input, Nil)
17+
}
18+
19+
pub fn sync() {
20+
int.random(2) |> int.is_even
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import eyg/analysis/type_/isomorphic as t
2+
import eyg/interpreter/cast
3+
4+
pub const label = "Prompt"
5+
6+
pub fn lift() {
7+
t.String
8+
}
9+
10+
pub fn lower() {
11+
t.result(t.String, t.unit)
12+
}
13+
14+
pub fn decode(lift) {
15+
cast.as_string(lift)
16+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import eyg/analysis/type_/isomorphic as t
2+
import eyg/interpreter/cast
3+
import eyg/interpreter/value as v
4+
import gleam/int
5+
6+
pub const l = "Random"
7+
8+
pub fn lift() {
9+
t.Integer
10+
}
11+
12+
pub fn lower() {
13+
t.Integer
14+
}
15+
16+
pub fn decode(lift) {
17+
cast.as_integer(lift)
18+
}
19+
20+
pub fn encode(number) {
21+
v.Integer(number)
22+
}
23+
24+
pub fn sync(max) {
25+
int.random(max)
26+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import eyg/analysis/type_/binding
2+
import eyg/analysis/type_/isomorphic as t
3+
import eyg/interpreter/break
4+
import eyg/interpreter/cast
5+
import eyg/interpreter/value as v
6+
import gleam/http as gleam_http
7+
import gleam/option.{None, Some}
8+
import gleam/result.{try}
9+
import gleam/uri.{Uri}
10+
import touch_grass/http
11+
12+
pub fn uri() -> binding.Mono {
13+
t.record([
14+
#("scheme", http.scheme()),
15+
// host + port = authority
16+
#("host", t.String),
17+
#("port", t.option(t.String)),
18+
// path can be empty
19+
#("path", t.String),
20+
#("query", t.key_value_list(t.String)),
21+
])
22+
}
23+
24+
pub fn uri_to_gleam(url: v.Value(a, b)) -> Result(uri.Uri, break.Reason(a, b)) {
25+
use scheme <- try(cast.field("scheme", http.scheme_to_gleam, url))
26+
use host <- try(cast.field("host", cast.as_string, url))
27+
use port <- try(cast.field("port", cast.as_option(_, cast.as_integer), url))
28+
use path <- try(cast.field("path", cast.as_string, url))
29+
use query <- try(cast.field(
30+
"query",
31+
cast.as_list_of(_, fn(item) {
32+
use key <- try(cast.field("key", cast.as_string, item))
33+
use value <- try(cast.field("value", cast.as_string, item))
34+
Ok(#(key, value))
35+
}),
36+
url,
37+
))
38+
let query = uri.query_to_string(query)
39+
Ok(Uri(
40+
scheme: Some(gleam_http.scheme_to_string(scheme)),
41+
userinfo: None,
42+
host: Some(host),
43+
port:,
44+
path:,
45+
query: Some(query),
46+
fragment: None,
47+
))
48+
}
49+
// We shouldn't have the error cases, maybe touch grass needs a version of a more constrained uri to the encoding
50+
// pub fn url_to_eyg(url: uri.Uri) {
51+
// case url {
52+
// Uri(scheme: Some(scheme), host: Some(host), port:, path:, query:, ..) -> {
53+
// case gleam_http.scheme_from_string(scheme) {
54+
// Ok(scheme) -> {
55+
// let query = option.unwrap(query, "")
56+
// let query = uri.parse_query(query) |> result.unwrap([])
57+
// v.Record(
58+
// dict.from_list([
59+
// #("scheme", http.scheme_to_eyg(scheme)),
60+
// #("host", v.String(host)),
61+
// #("port", v.option(port, v.Integer)),
62+
// #("path", v.String(path)),
63+
// #(
64+
// "query",
65+
// v.LinkedList(
66+
// list.map(query, fn(pair) {
67+
// let #(key, value) = pair
68+
// v.Record(
69+
// dict.from_list([
70+
// #("key", v.String(key)),
71+
// #("value", v.String(value)),
72+
// ]),
73+
// )
74+
// }),
75+
// ),
76+
// ),
77+
// ]),
78+
// )
79+
// |> v.ok()
80+
// }
81+
// Error(_) -> v.error(v.String("invalid scheme"))
82+
// }
83+
// }
84+
// _ -> v.error(v.String("invalid url"))
85+
// }
86+
// }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import eyg/analysis/type_/isomorphic as t
2+
import eyg/interpreter/value as v
3+
import touch_grass/uri
4+
5+
pub const label = "Visit"
6+
7+
pub fn lift() {
8+
t.String
9+
}
10+
11+
pub fn lower() {
12+
t.result(t.unit, t.String)
13+
}
14+
15+
pub const decode = uri.uri_to_gleam
16+
17+
pub fn encode(result) {
18+
case result {
19+
Ok(Nil) -> v.ok(v.unit())
20+
Error(reason) -> v.error(v.String(reason))
21+
}
22+
}

packages/website/src/website/harness/browser/fetch.gleam

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
// Harness components merge type and runtime so cannot be put in either of those libraries
2-
import eyg/analysis/type_/isomorphic as t
32
import eyg/interpreter/value as v
43
import gleam/fetch as gfetch
54
import gleam/javascript/promise
65
import gleam/result
76
import gleam/string
87
import touch_grass/fetch
98

10-
pub const l = "Fetch"
11-
12-
pub fn lift() {
13-
todo
14-
// http.request()
15-
}
16-
17-
pub fn lower() {
18-
todo
19-
// t.result(http.response(), t.String)
20-
}
21-
229
pub fn run(request) {
2310
promise.map(do(request), fn(result) {
2411
result

packages/website/src/website/harness/browser/flip.gleam

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)