Skip to content

Commit

Permalink
Add more selecting_record functions
Browse files Browse the repository at this point in the history
  • Loading branch information
NthTensor authored and lpil committed May 17, 2023
1 parent e57509c commit 3cfdb3d
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- The `gleam/erlang/process` module gains functions `selecting_record5`
through to `selecting_record8`.
- The `file` module loses the `is_file` function; gains the `FileType`,
`Access`, and `FileInfo` types; and gains the `is_regular`, `file_info`,
`link_info`, `file_exists`, and `link_exists` functions.
Expand Down
103 changes: 103 additions & 0 deletions src/gleam/erlang/process.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,109 @@ pub fn selecting_record4(
insert_selector_handler(selector, #(tag, 4), handler)
}

/// Add a handler to a selector for 5 element tuple messages with a given tag
/// element in the first position.
///
/// Typically you want to use the `selecting` function with a `Subject` instead,
/// but this function may be useful if you need to receive messages sent from
/// other BEAM languages that do not use the `Subject` type.
///
pub fn selecting_record5(
selector: Selector(payload),
tag: tag,
mapping transform: fn(Dynamic, Dynamic, Dynamic, Dynamic) -> payload,
) -> Selector(payload) {
let handler = fn(message: #(tag, Dynamic, Dynamic, Dynamic, Dynamic)) {
transform(message.1, message.2, message.3, message.4)
}
insert_selector_handler(selector, #(tag, 5), handler)
}

/// Add a handler to a selector for 6 element tuple messages with a given tag
/// element in the first position.
///
/// Typically you want to use the `selecting` function with a `Subject` instead,
/// but this function may be useful if you need to receive messages sent from
/// other BEAM languages that do not use the `Subject` type.
///
pub fn selecting_record6(
selector: Selector(payload),
tag: tag,
mapping transform: fn(Dynamic, Dynamic, Dynamic, Dynamic, Dynamic) -> payload,
) -> Selector(payload) {
let handler = fn(message: #(tag, Dynamic, Dynamic, Dynamic, Dynamic, Dynamic)) {
transform(message.1, message.2, message.3, message.4, message.5)
}
insert_selector_handler(selector, #(tag, 6), handler)
}

/// Add a handler to a selector for 7 element tuple messages with a given tag
/// element in the first position.
///
/// Typically you want to use the `selecting` function with a `Subject` instead,
/// but this function may be useful if you need to receive messages sent from
/// other BEAM languages that do not use the `Subject` type.
///
pub fn selecting_record7(
selector: Selector(payload),
tag: tag,
mapping transform: fn(Dynamic, Dynamic, Dynamic, Dynamic, Dynamic, Dynamic) ->
payload,
) -> Selector(payload) {
let handler = fn(
message: #(tag, Dynamic, Dynamic, Dynamic, Dynamic, Dynamic, Dynamic),
) {
transform(message.1, message.2, message.3, message.4, message.5, message.6)
}
insert_selector_handler(selector, #(tag, 7), handler)
}

/// Add a handler to a selector for 8 element tuple messages with a given tag
/// element in the first position.
///
/// Typically you want to use the `selecting` function with a `Subject` instead,
/// but this function may be useful if you need to receive messages sent from
/// other BEAM languages that do not use the `Subject` type.
///
pub fn selecting_record8(
selector: Selector(payload),
tag: tag,
mapping transform: fn(
Dynamic,
Dynamic,
Dynamic,
Dynamic,
Dynamic,
Dynamic,
Dynamic,
) ->
payload,
) -> Selector(payload) {
let handler = fn(
message: #(
tag,
Dynamic,
Dynamic,
Dynamic,
Dynamic,
Dynamic,
Dynamic,
Dynamic,
),
) {
transform(
message.1,
message.2,
message.3,
message.4,
message.5,
message.6,
message.7,
)
}
insert_selector_handler(selector, #(tag, 8), handler)
}

type AnythingSelectorTag {
Anything
}
Expand Down
74 changes: 72 additions & 2 deletions test/gleam/erlang/process_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,15 @@ pub fn selecting_record_test() {
send(process.self(), #("a", 1))
send(process.self(), #("b", 2, 3))
send(process.self(), #("c", 4, 5, 6))
send(process.self(), "d")
send(process.self(), #("d", 7, 8, 9, 10))
send(process.self(), #("e", 11, 12, 13, 14, 15))
send(process.self(), #("f", 16, 17, 18, 19, 20, 21))
send(process.self(), #("g", 22, 23, 24, 25, 26, 27, 28))
send(process.self(), "h")

let assert Error(Nil) =
process.new_selector()
|> process.selecting_record2("d", dynamic.unsafe_coerce)
|> process.selecting_record2("h", dynamic.unsafe_coerce)
|> process.select(0)

let assert Error(Nil) =
Expand All @@ -257,6 +261,72 @@ pub fn selecting_record_test() {
)
|> process.select(0)

let assert Ok(#(22, 23, 24, 25, 26, 27, 28)) =
process.new_selector()
|> process.selecting_record8(
"g",
fn(a, b, c, d, e, f, g) {
#(
dynamic.unsafe_coerce(a),
dynamic.unsafe_coerce(b),
dynamic.unsafe_coerce(c),
dynamic.unsafe_coerce(d),
dynamic.unsafe_coerce(e),
dynamic.unsafe_coerce(f),
dynamic.unsafe_coerce(g),
)
},
)
|> process.select(0)

let assert Ok(#(16, 17, 18, 19, 20, 21)) =
process.new_selector()
|> process.selecting_record7(
"f",
fn(a, b, c, d, e, f) {
#(
dynamic.unsafe_coerce(a),
dynamic.unsafe_coerce(b),
dynamic.unsafe_coerce(c),
dynamic.unsafe_coerce(d),
dynamic.unsafe_coerce(e),
dynamic.unsafe_coerce(f),
)
},
)
|> process.select(0)

let assert Ok(#(11, 12, 13, 14, 15)) =
process.new_selector()
|> process.selecting_record6(
"e",
fn(a, b, c, d, e) {
#(
dynamic.unsafe_coerce(a),
dynamic.unsafe_coerce(b),
dynamic.unsafe_coerce(c),
dynamic.unsafe_coerce(d),
dynamic.unsafe_coerce(e),
)
},
)
|> process.select(0)

let assert Ok(#(7, 8, 9, 10)) =
process.new_selector()
|> process.selecting_record5(
"d",
fn(a, b, c, d) {
#(
dynamic.unsafe_coerce(a),
dynamic.unsafe_coerce(b),
dynamic.unsafe_coerce(c),
dynamic.unsafe_coerce(d),
)
},
)
|> process.select(0)

let assert Ok(#(4, 5, 6)) =
process.new_selector()
|> process.selecting_record4(
Expand Down

0 comments on commit 3cfdb3d

Please sign in to comment.