Skip to content

A project that brings Clojure features to Ruby for manipulating maps like assoc_in, update_in, update and get_in

License

Notifications You must be signed in to change notification settings

danielspaniel/ruby-like-clojure

Repository files navigation

All your favorite functions that act on maps/hashes in clojure, but for ruby like:

  • assoc_in
  • update_in
  • update
  • get_in

NOTE:

These functions all return a new hash, they do not modify the original hash

Usage

  • assoc_in
RLC.assoc_in({a: 1}, [:b], 3)
# => {a: 1, b: 3}
RLC.assoc_in({a: 1}, [:b, :c], 3)
# => {a: 1, b: {c: 3}}
  • update_in
RLC.update_in({}, [:a, :b]) { |other| (other || 0) + 2 }
# => { a: { b: 2 } }
RLC.update_in({ a: { b: 1 } }, [:a, :b]) { |other| (other || 0) + 2 }
# => { a: { b: 3 } }
  • update
RLC.update({}, :a) { |other| (other || 0) + 2 }
# => { a: 2 }
RLC.update({ a: 1 }, :a) { |other| (other || 0) + 2 }
# => { a: 3 }
  • get_in
RLC.get_in({ a: 1 }, [:a])
# => 1
RLC.get_in({ a: [1, 3] }, [:a, 1])
# => 3
RLC.get_in({ a: { b: 2 } }, [:a, :c])
# => nil
RLC.get_in({ a: { b: 2 } }, [:a, :c], 4)
# => 4
 my_report_hash
  .then { RLC.update_in(_1, %w[reports group1], &method(:fill_in_things)) }
  .then { RLC.update_in(_1, %w[reports group2], &method(:add_things)) }
# => new my_report_hash
Feel free to suggest other functions we can implement in ruby that you miss in clojure

About

A project that brings Clojure features to Ruby for manipulating maps like assoc_in, update_in, update and get_in

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published