Blog

Error tonto con _.flow y _.uniq

Error tonto con _.flow y _.uniq

export const graphDistinctTags = ({ edges }) =>
  _.flow(
    _.flatMap((edge) => edge?.tags),
    _.tap((x) => console.log("TAP__1", x)),
    _.map((tag) => tag?.id),
    _.tap((x) => console.log("TAP__2", x)),
    _.uniq(),
    _.tap((x) => console.log("TAP__3", x))
  )(edges);
TAP__1 (4) [{…}, {…}, {…}, {…}]

id: 4
value: {target_type: 'business_concept', type: 'bc_padre'}

id: 8
value: {target_type: 'business_concept', type: 'bc_parent'}

id: 3
value: {target_type: 'business_concept', type: 'bc_caculo'}

id: 3
value: {target_type: 'business_concept', type: 'bc_caculo'}

relationGraphTraversal.js?d01d:117 TAP__2 (4) [4, 8, 3, 3]
relationGraphTraversal.js?d01d:119 TAP__3 (4) [4, 8, 3, 3]

Estoy llamando incorrectamente a _.uniq(), uniq no es una función curried, no admite _.uniq()(valor), lo correcto es _.uniq a secas (la función propiamente):

export const graphDistinctTags = ({ edges }) =>
  _.flow(
    _.flatMap((edge) => edge?.tags),
    _.tap((x) => console.log("TAP__1", x)),
    _.map((tag) => tag?.id),
    _.tap((x) => console.log("TAP__2", x)),
    _.uniq,
    _.tap((x) => console.log("TAP__3", x))
  )(edges);

Ahora el resultado: TAP__3 (3) [4, 8, 3]

…pero… ¿por qué funcionaba antes? _.uniq() devuelve [], equivalente a:

export const graphDistinctTags = ({ edges }) =>
  _.flow(
    _.flatMap((edge) => edge?.tags),
    _.tap((x) => console.log("TAP__1", x)),
    _.map((tag) => tag?.id),
    _.tap((x) => console.log("TAP__2", x)),
    [],
    _.tap((x) => console.log("TAP__3", x))
  )(edges);

… y esto devuelve lo mismo que la primera vez, [4, 8, 3, 3]… misterio