# Chaining of monads

**URL:** <https://discourse.dry-rb.org/t/chaining-of-monads/1543>\
**Category:** Support\
**Created:** [November 4, 2022, 8:46am UTC](https://discourse.dry-rb.org/t/chaining-of-monads/1543 "2022-11-04T08:46:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![djolereject](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/djolereject/32/811_2.png) [@djolereject](https://discourse.dry-rb.org/u/djolereject)\
**Post date:** [November 4, 2022, 8:46am UTC](https://discourse.dry-rb.org/t/chaining-of-monads/1543/1 "2022-11-04T08:46:04Z")

</div>

Hi all, I’m trying something and I’m wondering if it’s possible.  
Let’s say we have this:

```auto
      def first(a)
        Success(x: a)
      end

      def second(x:)
        Success(:anything)
      end

```

How would something like this be possible:

```auto
        first(1).bind(method(:second))

```

What I’m trying to do is return Success(hash) and bind that hash to named parameters for the next method.  
Any ideas?

---

<div class="post-metadata">

**Author:** ![bkuhlmann](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/bkuhlmann/32/557_2.png) [@bkuhlmann](https://discourse.dry-rb.org/u/bkuhlmann)\
**Post date:** [November 4, 2022, 2:34pm UTC](https://discourse.dry-rb.org/t/chaining-of-monads/1543/2 "2022-11-04T14:34:27Z")

</div>

Hey Djordje, what you’re hinting at – and what I think you’re looking for – is [function composition](https://www.alchemists.io/articles/ruby_function_composition) which can be pulled off using the [Transactable](https://www.alchemists.io/projects/transactable) gem. Example:

```ruby
pipe 1, method(:first), method(:second)

```

Further details are explained in the links above, though.

---

<div class="post-metadata">

**Author:** ![djolereject](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/djolereject/32/811_2.png) [@djolereject](https://discourse.dry-rb.org/u/djolereject)\
**Post date:** [November 4, 2022, 2:51pm UTC](https://discourse.dry-rb.org/t/chaining-of-monads/1543/3 "2022-11-04T14:51:31Z")

</div>

Oh, interesting, thanks!  
Is this related to dry-transaction in some way? I used to use it years ago and I’m wondering if this is similar.  
Thank you very much for this, I got to look into it and learn how to use it.

---

<div class="post-metadata">

**Author:** ![bkuhlmann](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/bkuhlmann/32/557_2.png) [@bkuhlmann](https://discourse.dry-rb.org/u/bkuhlmann)\
**Post date:** [November 4, 2022, 6:31pm UTC](https://discourse.dry-rb.org/t/chaining-of-monads/1543/4 "2022-11-04T18:31:31Z")

</div>

Yep! So Transactable is an alternative approach to Dry Transaction with a strong focus on function composition. The concept is the same, though, where you _compose_ multiple steps together using monads.

---

<div class="post-metadata">

**Author:** ![djolereject](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/djolereject/32/811_2.png) [@djolereject](https://discourse.dry-rb.org/u/djolereject)\
**Post date:** [November 4, 2022, 8:17pm UTC](https://discourse.dry-rb.org/t/chaining-of-monads/1543/5 "2022-11-04T20:17:24Z")

</div>

I looked into this subject for so long and googled lot of things but somehow missed existence of this gem entirely. Thank you very much!

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alassek/32/314_2.png) [@alassek](https://discourse.dry-rb.org/u/alassek)\
**Post date:** [November 4, 2022, 8:57pm UTC](https://discourse.dry-rb.org/t/chaining-of-monads/1543/6 "2022-11-04T20:57:15Z")

</div>

You could achieve this with a built-in concept in dry-monads called [do notation](https://dry-rb.org/gems/dry-monads/1.3/do-notation/)

```ruby
class Operation
  include Dry::Monads[:result]
  include Dry::Monads::Do.for(:call)

  def call(a)
    result = yield first(a)
    result = yield second(**result)

    Success(result)
  end

  private

  def first(a)
    Success(x: a)
  end

  def second(x:)
    Success(:anything)
  end
end

```

---

<div class="post-metadata">

**Author:** ![djolereject](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/djolereject/32/811_2.png) [@djolereject](https://discourse.dry-rb.org/u/djolereject)\
**Post date:** [November 4, 2022, 9:05pm UTC](https://discourse.dry-rb.org/t/chaining-of-monads/1543/7 "2022-11-04T21:05:44Z")

</div>

Yeah, I know about do notation, but I find it cumbersome to come up with the names for every intermediate step and it somehow seems unnatural. Especially when the situation becomes more complicated and some “or” and “tee” cases are added, it soon loses readability in my opinion.  
Thanks!
