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

Question: Mapping two values to one value #52

Open
badBlackShark opened this issue Feb 23, 2023 · 0 comments
Open

Question: Mapping two values to one value #52

badBlackShark opened this issue Feb 23, 2023 · 0 comments

Comments

@badBlackShark
Copy link

badBlackShark commented Feb 23, 2023

This is more a question whether a feature like this would even be feasible to implement. What I need is mapping two values to one value, here a basic example:

input = { first_name: 'Max', last_name: 'Mustermann' }

# Now we want to map the full name
# Option 1
map(:first_name, to: :full_name).then { value + values.get(:last_name) }

# Option 2
to(:full_name).then { "#{values.get(:first_name)} #{values.get(:last_name]}" }

What I would like to do is something like

map(:first_name, and: :last_name, to: :full_name).then { "#{value[0]} #{value[1]}" }
# or
map(:first_name, and: :last_name, to: :full_name).then { value.compact.join(' ') }

If one of the two doesn't exist, the mapper gets skipped, I would assume. If you use map? and one of them does not exist, then Undefined could be yielded (so if last_name does not exist, then value == ['Max', Undefined].

This and positional argument also takes a path, in case two different paths need to be walked:

input = { a: { b: 1 }, c: { d: 2 } }

map(:a, :b, and: [:c, :d], to: :e).then(&:sum) #=> { e: 3 }

I hope that's detailed enough :) Is it possible & feasible to implement a feature like this? Or would that mean basically having to rewrite everything?

Alternatively: Do you know of a better way of doing this, other than the two options I showed initially?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant