Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Dec 29 2016 21:28
  • Dec 29 2016 21:28
  • Dec 29 2016 21:28
  • Dec 29 2016 21:28
  • Dec 29 2016 21:27
  • Dec 29 2016 21:27
  • Dec 29 2016 21:27
  • Dec 29 2016 21:27
  • Dec 29 2016 21:27
  • Dec 29 2016 21:27
  • Dec 29 2016 21:27
  • Dec 29 2016 21:27
  • Dec 29 2016 21:27
  • Dec 29 2016 21:27
  • Dec 29 2016 21:27
  • Dec 29 2016 21:27
  • Dec 29 2016 21:27
  • Dec 29 2016 21:27
  • Dec 29 2016 21:27
  • Dec 29 2016 21:27
Ben Smith
@8enSmith
Using a transducer reduces the number of times a data set is iterated over if your using a transformer which performs multiple loops e.g. filter and then map
The theory is pretty deep and Im still getting my head around it TBH!
illuminist
@illuminist
@8enSmith As I can see, both transducer-js and lodash/fp should be able to work together, but only to some extents as transduce function need transducer function to be wrapped by their own function first.
Ben Smith
@8enSmith
OK, thanks @illuminist I'll start digging around in the transducers.js library. Good if anyone else has an opinion on transducers and lodash though!
illuminist
@illuminist
Yep, transduce does iterate a collection only once. Unlike flow where it iterates according to number of composed function.
I really had a thought about this: you don't really need to worry how many time an object need to be iterate, it still needs the same number of statement to process the data in either way
TonySpegel
@TonySpegel
Hi there I want to use mergeWith but only merge some properties into arrays. I'd like to overwrite others instead
How would my customizer Function have to look like?
My Object could look something like this:
{
    "label": "cat",
    "descriptor": [
        [3, 4, 5]
    ],
    "firstSeen": 14,
    "lastSeen": 14,
    "age": 24,
},

{
    "label": "dog",
    "descriptor": [
        [6, 7, 8]
    ],
    "firstSeen": 16,
    "lastSeen": 16,
    "age": 26,
},

{
    "label": "dog",
    "descriptor": [
        [3, 4, 5],
        [6, 7, 8],
    ],
    "firstSeen": 14,
    "lastSeen": 16,
    "age": [24, 26],
},
Overwrite label, keep firstSeen, overwrite lastSeen, merge descriptor and age
is that doable with mergeWith?
TonySpegel
@TonySpegel

I can do

if (isArray(objValue)) {
    return objValue.concat(srcValue);
}

for Arrays, but how would I target specific keys and ignore others

TonySpegel
@TonySpegel
okay doing it by hand seems easier:
const mergedDetection = {
    ...detection1,
    descriptor: new Array(
        detection1.descriptor,
        detection2.descriptor,
    ),
    lastSeen: detection2.lastSeen,
    age: new Array(detection1.age, detection2.age),
};
Andrey
@Xambey

hi, tell me please, what am I doing wrong? I have a model class and every time I call deepClone, the clone time doubles

model:

export class ServiceInstanceModel extends Layout {
  @prop()
  @i18n()
  @checkbox(new CheckboxConfig({ label: 'services:Форма.Доступен извне' }))
  @layout(new LayoutConfig({ allFlex: 6 }))
  isExternal = false;

  @prop()
  @required()
  @i18n()
  @select(new SelectConfig({
    serverSearchMode: true,
    labelKey: 'name',
    valueKey: 'id',
    distinctKey: 'name',
    targetKey: 'name',
    autoLoadData: (model: ServiceInstanceModel) => model.segmentRegistryApi.getSegmentList,
    autoLoadContext: (model: ServiceInstanceModel) => model.segmentRegistryApi,
    placeholder: 'services:Форма.Выберите сегмент'
  }))
  @layout(new LayoutConfig({ allFlex: 8 }))
  segmentId?: string = null;

  @prop({ ignore: () => true })
  segmentRegistryApi: SegmentRegistryService = AppModule.injector.get(SegmentRegistryService);

  @prop()
  @required()
  @i18n()
  @input(new InputConfig({
    placeholder: 'services:Форма.URL OpenAPI'
  }))
  @layout(new LayoutConfig({ allFlex: 9, offset: 1 }))
  swaggerUrl: string = null;
}

Code calling deepClone:

  addItem() {
    if(!this.config?.entity) {
      this.logService.error('Нельзя добавить элемент, отсутствует экземпляр entity конфигурации ListConfig для RxFormArray', 'ListComponent');
      return;
    }
    console.time('test');
    const copy =  lodash.cloneDeep(this.config.entity); // Object.assign({}, this.config.entity);
    console.timeEnd('test');
    this.abstractControl.push(this.rxFormBuilder.formGroup(this.config.entity.constructor));
    this.structureModel.push(copy);
  }

this.config.entity not changes (simple entity of ServiceInstanceModel)

Benchmark:
image.png
Andrey
@Xambey
I found a reason. All because of the segmentRegistryApi property. This is a great service that comes from the method. What can I do in this case so that this property is copied, not cloned
Nickolay Kulikov
@kokushkin
@illuminist thank you for your "join". Unfortunately, I'd not seen your answer at the time. So we, with guys came up with different solutions) https://codesandbox.io/s/test-algorithms-environment-dmz8b check algoritms.js , yours the last but looks like the simplest and almost the most efficient one (watch console). )) The only one downside - it's the right-join, but it's ok)
illuminist
@illuminist
@kokushkin I think I heard different criteria at that time. Btw, I had created a new one with correct requirement, improved performance, able to accept more than 2 arrays, and able to handle case of having same key in same array: https://codesandbox.io/s/test-algorithms-environment-54zhq
Actually didn't use lodash function because need to optimize for raw performance.
Nickolay Kulikov
@kokushkin
@illuminist thank you again. Good idea! So, it seemed like a generalization of the "joinByBogdan" but you'd used "Object.assign(hash[item.key], item)" instead of "hash[item.key] = { ...hash[item.key], ...item }; " . And It works much faster! Up to 10 times faster in this scenario! Cool.
Furkan Filiz
@furkanflz1_twitter

Merhabalar aşağıdaki gibi state içine cloneDeep ile verilerimi atıyorum. Ancak props değiştiğinde state değişmiyor.

Nasıl bir yol izlemeliyim

state = cloneDeep(this.props.modelData);

Hello, I am putting my data into the state with cloneDeep as below. However, when the props change, the state does not change.

How should I proceed?

state = cloneDeep (this.props.modelData);

illuminist
@illuminist
Is that reactjs?
Furkan Filiz
@furkanflz1_twitter
yes
illuminist
@illuminist
So which component function you're trying to assign state in?
Furkan Filiz
@furkanflz1_twitter

actually i found a solution

I set the data in the constructor without using lodash clonedeep. I then update it in componentDidUpdate

illuminist
@illuminist
If you want it to always update state when props change, you may want to use deriveStateFromProps function instead
Or if you don't need to update state inside component, you can just render using data from this.props directly
Furkan Filiz
@furkanflz1_twitter
Okay.I will examine . Thanks
Andrey
@Xambey

Hello, please tell me what can be done in my case? (Angular)

I have a model with a lot of elements and every time I call cloneDeep, the processing time of the model (almost doubles) is increased, although the model does not change significantly.

In this case, there is a page component that contains a list of tabs. In each tab, a complex component is generated. When I click on the tab, ngOnInit is called with cloneDeep for the model based on which the component is rendered

Lazy.gif
The model contains reactive properties.
julian-code
@julian-code
Hey guys. Is it possible to use intersectionBy with objects comparing two properties?
illuminist
@illuminist
@julian-code You can create iteratee that return a combinination of property, or use intersectionWith instead
julian-code
@julian-code
@illuminist yeah, i just tried that. However using _.isEqual is very costly.
      ctx.selectedPlayers.forEach((player) => {
        const comparableMatches = player.matches.map((match) => ({
          teamName: match.teamName,
          matchId: match.matchId,
          eloDiff: match.eloDiff,
          map: match.map,
          win: match.win,
          time: new Date(match.time),
        }));
        allMatches.push(comparableMatches);
      });
      return _.intersectionWith(...allMatches, _.isEqual);
illuminist
@illuminist
You should use your own comparator that compare between 2 properties
julian-code
@julian-code
what is send in after the ...allMatches?
As the second argument.
illuminist
@illuminist
That's the comparator
Its signature is (object1, object2) => boolean
julian-code
@julian-code
arhhh
I think I got it working now :D
illuminist
@illuminist
That's great
julian-code
@julian-code
 return _.intersectionWith(...allMatches,
        (match1, match2) => {
          if (match1.win === match2.win && match1.matchId === match2.matchId) {
            return true;
          }
          return false;
        });
Thanks for the help :)
illuminist
@illuminist
Glad too
julian-code
@julian-code
How do you find and set a value on a specific object in an array?
Or more like find a specific object and set a value on that object.