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

mapObjIndexedToArray #798

Open
srghma opened this issue Jan 6, 2019 · 8 comments
Open

mapObjIndexedToArray #798

srghma opened this issue Jan 6, 2019 · 8 comments

Comments

@srghma
Copy link
Collaborator

srghma commented Jan 6, 2019

Is your feature request related to a problem? Please describe.

Function maps object properties into array.

Describe the solution you'd like

// mapObjIndexedToArray: ((v, k, {k: v}) → v') → {k: v} → [v']

/*
* @example
*
*      const xyz = { x: 1, y: 2, z: 3 };
*      const prependKeyAndDouble = (num, key, obj) => key + (num * 2);
*
*      mapObjIndexedToArray(prependKeyAndDouble, xyz); //=> ['x2', 'y4', 'z6']
*/

const mapObjIndexedToArray = curry((fn, obj) => {
  return _map(function(key) {
    return fn(obj[key], key, obj);
  }, keys(obj));
});

Describe alternatives you've considered

// mapObjIndexedToArray: ((v, k, {k: v}) → v') → {k: v} → [v']
const mapObjIndexedToArray = R.curryN(2, (func, obj) =>
  R.compose(
    R.values,
    R.mapObjIndexed(func),
  )(obj),
)

Additional context

const patient = {
  'abbrev@1':{
    version:'1.0.9',
    resolved:'https://registry.yarnpkg.com/abbrev/-/abbrev-1.0.9.tgz#91b4792588a7738c25f35dd6f63752a2f8776135'
  },
  'shell-quote@git+https://github.com/srghma/node-shell-quote.git#without_unlicenced_jsonify':{
    version:'1.6.0',
    resolved:'git+https://github.com/srghma/node-shell-quote.git#0aa381896e0cd7409ead15fd444f225807a61e0a'
  },
  '@graphile/plugin-supporter@git+https://1234user:1234pass@git.graphile.com/git/users/1234user/postgraphile-supporter.git':{
    version:'1.6.0',
    resolved:'git+https://1234user:1234pass@git.graphile.com/git/users/1234user/postgraphile-supporter.git#1234commit'
  },
}

const result = mapObjIndexedToArray((value, key) => ({ ...value, name: key }), patient)

const expected = [
  {
    name: 'abbrev@1',
    version:'1.0.9',
    resolved:'https://registry.yarnpkg.com/abbrev/-/abbrev-1.0.9.tgz#91b4792588a7738c25f35dd6f63752a2f8776135'
  },
  {
    name: 'shell-quote@git+https://github.com/srghma/node-shell-quote.git#without_unlicenced_jsonify',
    version:'1.6.0',
    resolved:'git+https://github.com/srghma/node-shell-quote.git#0aa381896e0cd7409ead15fd444f225807a61e0a'
  },
  {
    name:   '@graphile/plugin-supporter@git+https://1234user:1234pass@git.graphile.com/git/users/1234user/postgraphile-supporter.git',
    version:'1.6.0',
    resolved:'git+https://1234user:1234pass@git.graphile.com/git/users/1234user/postgraphile-supporter.git#1234commit'
  },
]

expect(result).toEqual(result)
@char0n
Copy link
Owner

char0n commented Jan 6, 2019

Can you pls demonstrate some examples on some fixtures data and explain the reason why it should return the array ? I'm trying to understand your motivation and see the practicality of such a function. Thank you

@srghma
Copy link
Collaborator Author

srghma commented Jan 7, 2019

Motivation:

what you can do with it is transform:

const patient = {
  'abbrev@1':{
    version:'1.0.9',
    resolved:'https://registry.yarnpkg.com/abbrev/-/abbrev-1.0.9.tgz#91b4792588a7738c25f35dd6f63752a2f8776135'
  },
  'shell-quote@git+https://github.com/srghma/node-shell-quote.git#without_unlicenced_jsonify':{
    version:'1.6.0',
    resolved:'git+https://github.com/srghma/node-shell-quote.git#0aa381896e0cd7409ead15fd444f225807a61e0a'
  },
  '@graphile/plugin-supporter@git+https://1234user:1234pass@git.graphile.com/git/users/1234user/postgraphile-supporter.git':{
    version:'1.6.0',
    resolved:'git+https://1234user:1234pass@git.graphile.com/git/users/1234user/postgraphile-supporter.git#1234commit'
  },
}

const result = mapObjIndexedReturnArray((value, key) => ({ ...value, name: key }), patient)

const expected = [
  {
    name: 'abbrev@1',
    version:'1.0.9',
    resolved:'https://registry.yarnpkg.com/abbrev/-/abbrev-1.0.9.tgz#91b4792588a7738c25f35dd6f63752a2f8776135'
  },
  {
    name: 'shell-quote@git+https://github.com/srghma/node-shell-quote.git#without_unlicenced_jsonify',
    version:'1.6.0',
    resolved:'git+https://github.com/srghma/node-shell-quote.git#0aa381896e0cd7409ead15fd444f225807a61e0a'
  },
  {
    name:   '@graphile/plugin-supporter@git+https://1234user:1234pass@git.graphile.com/git/users/1234user/postgraphile-supporter.git',
    version:'1.6.0',
    resolved:'git+https://1234user:1234pass@git.graphile.com/git/users/1234user/postgraphile-supporter.git#1234commit'
  },
]

expect(result).toEqual(result)

pretty useful function I think, I needed it 3 times in my life

@char0n
Copy link
Owner

char0n commented Jan 9, 2019

Yeah I see the potential there. We have to be careful with the name though, map invokes homomorphic operation. This is more like unzipObjectWith/unzipObjWith transformation.

@char0n
Copy link
Owner

char0n commented Jan 10, 2019

To form a partial isomorphism with R.zipObj the transformation needs to look like this:

const patient = {
  'abbrev@1':{
    version:'1.0.9',
    resolved:'https://registry.yarnpkg.com/abbrev/-/abbrev-1.0.9.tgz#91b4792588a7738c25f35dd6f63752a2f8776135'
  },
  'shell-quote@git+https://github.com/srghma/node-shell-quote.git#without_unlicenced_jsonify':{
    version:'1.6.0',
    resolved:'git+https://github.com/srghma/node-shell-quote.git#0aa381896e0cd7409ead15fd444f225807a61e0a'
  },
  '@graphile/plugin-supporter@git+https://1234user:1234pass@git.graphile.com/git/users/1234user/postgraphile-supporter.git':{
    version:'1.6.0',
    resolved:'git+https://1234user:1234pass@git.graphile.com/git/users/1234user/postgraphile-supporter.git#1234commit'
  },
}

const result = unzipObjWith((value, key) => [key, { ...value, name: key }]), patient)

const expected = [
  [
    'abbrev@1', 
    'shell-quote@git+https://github.com/srghma/node-shell-quote.git#without_unlicenced_jsonify',
    '@graphile/plugin-supporter@git+https://1234user:1234pass@git.graphile.com/git/users/1234user/postgraphile-supporter.git',
 ]
 [
  {
    name: 'abbrev@1',
    version:'1.0.9',
    resolved:'https://registry.yarnpkg.com/abbrev/-/abbrev-1.0.9.tgz#91b4792588a7738c25f35dd6f63752a2f8776135'
  },
  {
    name: 'shell-quote@git+https://github.com/srghma/node-shell-quote.git#without_unlicenced_jsonify',
    version:'1.6.0',
    resolved:'git+https://github.com/srghma/node-shell-quote.git#0aa381896e0cd7409ead15fd444f225807a61e0a'
  },
  {
    name:   '@graphile/plugin-supporter@git+https://1234user:1234pass@git.graphile.com/git/users/1234user/postgraphile-supporter.git',
    version:'1.6.0',
    resolved:'git+https://1234user:1234pass@git.graphile.com/git/users/1234user/postgraphile-supporter.git#1234commit'
  },
 ]
]

expect(result).toEqual(result)

So you'll need to do expected[1] to get what you need. I like that this is generic concept which plays with the rest of the zipping algos. What do you think ?

@srghma
Copy link
Collaborator Author

srghma commented Jan 10, 2019

ok, we can add unzipObjWith too

@srghma
Copy link
Collaborator Author

srghma commented Jan 10, 2019

found my function in nix (by chance) https://github.com/NixOS/nixpkgs/blob/70732eb4823336bb3234ea399d797c79d8dccdba/lib/attrsets.nix#L229

it's name - mapAttrsToList

@char0n
Copy link
Owner

char0n commented Jan 11, 2019

What about calling your function objToArrayWith with alias of objectToArrayWith. The function accepts a binary transformer function and an object. IMHO having a non-homomorphic function which name starts with map doesn't make any sense. *map* should always return result of the same type as the input data.

@char0n
Copy link
Owner

char0n commented Jan 11, 2019

Created special issue for unzipObjWith as unzipObjWith can be implemented as specialization of objToArrayWith.

@char0n char0n added the feature label Jan 11, 2019
@char0n char0n changed the title mapObjIndexedReturnArray mapObjIndexedToArray Jan 4, 2020
@char0n char0n added the Hacktoberfest Hacktoberfest 2020 label Sep 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants