Skip to content

Conversation

@sledorze
Copy link
Collaborator

No description provided.

@sledorze
Copy link
Collaborator Author

@gcanti I still have the problem of running the tests locally (related to Jest
as depicted here #715
).
So blindly guessing and using the CI to fix issues, sorry for the noise..

@sledorze sledorze changed the title Record: filterWithIndex is conservative (fixes #711) Record: optimise filterWithIndex (conservative) and traverseWithKey (less object creations) (fixes #711) Feb 10, 2019
@gcanti
Copy link
Owner

gcanti commented Feb 11, 2019

I still have the problem of running the tests locally

Ah! Adding

"jest": {
  "testURL": "http://localhost/"
}

in package.json does solve the problem?

@gcanti
Copy link
Owner

gcanti commented Feb 11, 2019

@sledorze actually we could upgrade to [email protected]

*/
export function filterWithIndex<K extends string, A>(fa: Record<K, A>, p: (key: K, a: A) => boolean): Record<string, A>
export function filterWithIndex<A>(fa: Record<string, A>, p: (key: string, a: A) => boolean): Record<string, A>
export function filterWithIndex<A>(fa: Record<string, A>, p: (key: string, a: A) => boolean): Record<string, A> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about

export function filterWithIndex<A>(fa: Record<string, A>, p: (key: string, a: A) => boolean): Record<string, A> {
  const r: Record<string, A> = {}
  let changed = false
  for (const key in fa) {
    if (fa.hasOwnProperty(key)) {
      const a = fa[key]
      if (p(key, a)) {
        r[key] = a
      } else {
        changed = true
      }
    }
  }
  return changed ? r : fa
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's doing all work in case of noop (obj création plus assigns (n log n))

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noop is unlikely in a filter* function so in the most common case looks like you are doing even more work

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I m fine with the simpler approach.
Afk this morning (on phone) so expect a change later.

test/Record.ts Outdated
const actual = R.filter(y, isNumber)
assert.deepStrictEqual(actual, { a: 1 })

const z: Record<string, string | number> = { b: 1, a: 'foo', c: 'bar' }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn't look different from the previous one

@sledorze
Copy link
Collaborator Author

Was to trigger another case for coverage

@sledorze
Copy link
Collaborator Author

@gcanti yes adding

"jest": {
  "testURL": "http://localhost/"
}

Solves the problem (I have another one popping, however)

@gcanti
Copy link
Owner

gcanti commented Feb 11, 2019

FYI we are upgrading jest in the other PR #715 (comment)

@sledorze
Copy link
Collaborator Author

@gcanti not sure how I can fix that coverage issue

@gcanti
Copy link
Owner

gcanti commented Feb 11, 2019

@sledorze did you try open coverage/lcov-report/index.html?

EDIT: specifically coverage/lcov-report/Record.ts.html

r[key] = a
let changed = false
for (const key in fa) {
if (fa.hasOwnProperty(key)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah sorry, do you mean this line right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

@gcanti
Copy link
Owner

gcanti commented Feb 11, 2019

this seems to work

const x = Object.assign(Object.create({ c: true }), { a: 1, b: 'foo' })
assert.deepStrictEqual(R.filter(x, isNumber), { a: 1 })

@sledorze
Copy link
Collaborator Author

thanks @gcanti

@gcanti gcanti merged commit 4c3f3c5 into master Feb 11, 2019
@gcanti gcanti deleted the 711 branch February 11, 2019 18:41
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

Successfully merging this pull request may close these issues.

3 participants