5 ms·
Anyone using this and anyone using this with an Elixir background? I am curious if programming/language patterns like this, shared across languages, can help te
by dpflan 3y ago
Anyone using this and anyone using this with an Elixir background? I am curious if programming/language patterns like this, shared across languages, can help teams that have multi-language (eco)systems (e.g. ruby and elixir).
- ezekg 3y agoI don't have an Elixir background (though I envy Elixir in many ways), but I absolutely utilize pattern matching in my Ruby code bases: - https://github.com/keygen-sh/typed_params/blob/4e4982b7d2b263862c9a77296780c45c91c7e90f/lib/typed_params/formatters/jsonapi.rb#L72-L90 https://github.com/keygen-sh/typed_params/blob/4e4982b7d2b26... - https://github.com/keygen-sh/typed_params/blob/4e4982b7d2b263862c9a77296780c45c91c7e90f/lib/typed_params/controller.rb#L116-L123 https://github.com/keygen-sh/typed_params/blob/4e4982b7d2b26... - https://github.com/keygen-sh/keygen-api/blob/36cd61db143cc1c770cf0e4b8e9fb1be63993a3b/app/migrations/adjust_validity_for_validation_codes_migration.rb https://github.com/keygen-sh/keygen-api/blob/36cd61db143cc1c... - https://github.com/keygen-sh/keygen-api/blob/36cd61db143cc1c770cf0e4b8e9fb1be63993a3b/app/policies/release_policy.rb#L10-L26 https://github.com/keygen-sh/keygen-api/blob/36cd61db143cc1c... - https://github.com/keygen-sh/typed_params/blob/4e4982b7d2b263862c9a77296780c45c91c7e90f/spec/typed_params/parameterizer_spec.rb#L6-L32 https://github.com/keygen-sh/typed_params/blob/4e4982b7d2b26... I love it. I want even more pattern matching too, like defp: https://bugs.ruby-lang.org/issues/19764 https://bugs.ruby-lang.org/issues/19764 (with original credit to https://zverok.space/blog/2023-05-05-ruby-types.html https://zverok.space/blog/2023-05-05-ruby-types.html).
- thibaut_barrere 3y agoI use both and I believe pattern matching helps a lot to be more defensive in the programs (verify pre-conditions) and raise accordingly, with a low effort in code.
- hosh 3y agoI wrote Ruby for 10 years and Elixir for about 4. The Ruby version is a bit awkward because it has to make compromises with existing syntax … however, I have already been able to make use of it. My Ruby code these days have gotten a lot more functional, and the type of code I write these days (devops tooling, not Rails code) involves complex transformation or parsing, which makes this useful. I have not tried this with flow control (with :ok and :error tuples) … but I can tell you the missing half missing from this picture are function clauses. (How that would work with being able to redefine functions, I have no idea; just saying though ….) Pattern matching comes all the way from logic programming (Prolog). It should not be surprising that this really shines when working with complex logic.