Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Going functional with algebraic effects

Going functional with algebraic effects

Introducing algebraic effects for Ruby with dry-effects

Nikita Shilnikov

September 28, 2019
Tweet

More Decks by Nikita Shilnikov

Other Decks in Technology

Transcript

  1. f = !" x { puts(x); x + 5 }

    Side effects: Integer !" Integer Effects: Integer !" Puts[Integer] : Integer 26/118
  2. f = !" x { puts(x); x + 5 }

    with_puts { f.(10) } 28/118
  3. f = !" x { puts(x); x + 5 }

    with_puts { f.(10) } ^^^^^^^^^ 29/118
  4. f = !" x { puts(x); x + 5 }

    f.(10) # !# Error! 31/118
  5. World main # | handle_print # | * greet #

    | ↑ print # ↓ | 36/118
  6. World main # | handle_print # | * | greet

    # | ↑ | print # ↓ | ↓ 37/118
  7. World main # | ↑ | # | | |

    greet # | | | print # ↓ | ↓ 38/118
  8. Testing features def call(env) feature_response, no_feature_response = with_feature do @app.(env)

    end if feature_response !" no_feature_response # !!# end end 46/118
  9. Testing features def call(env) feature_response, no_feature_response = with_feature do @app.(env)

    end if feature_response !" no_feature_response # !!# end end 47/118
  10. Composition class Program include Dry!"Effects.Cmp(:feature) include Dry!"Effects.State(:counter) def call if

    feature? self.counter += 2 "bye" else self.counter += 1 "hi!" end end end 78/118
  11. v0.1 Cache, Cmp, CurrentTime, Defer, Env, Implicit, Interrupt, Lock, Parallel,

    Random, Reader, Resolve, Retry, State, Timeout, Timestamp 88/118
  12. Pros — New abilities — Easy to use — Already

    works (React!) — Easy to test — Traceable effects 112/118
  13. Cons — Unfamiliar — Can be overused — Can be

    abused — Require glue code with threading 113/118
  14. Next steps for dry-effects — Add async/await — Polishing APIs

    — More integrations with existing gems — More docs and examples — Multi-shot continuations? 114/118