Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

act.flatten #2

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/act.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,13 @@ pub fn try_each(
})
}
}

/// Merges a nested `Action` into a single layer.
///
pub fn flatten(action: Action(Action(b, a), a)) -> Action(b, a) {
fn(state) {
let #(state, action) = action(state)
let #(state, result) = action(state)
#(state, result)
}
}
9 changes: 9 additions & 0 deletions test/act_test.gleam
MystPi marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import gleam/int
import gleam/io
import gleeunit
import gleeunit/should

// The `Action` type and `do` function are imported unqualified since they are
// so common.
Expand Down Expand Up @@ -46,4 +48,11 @@ pub fn main() {

io.println("The result is " <> int.to_string(result))
io.println("The state is " <> int.to_string(final_state))

gleeunit.main()
}

pub fn flatten_test() {
act.flatten(fn(ctx) { #(ctx, fn(ctx) { #(ctx, "result") }) })("state")
|> should.equal(#("state", "result"))
}