hotscript icon indicating copy to clipboard operation
hotscript copied to clipboard

Proposal for Zip

Open eloytoro opened this issue 2 years ago • 2 comments

Keeping it simple, only takes into account values up to the minimum of both array's length

https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgGImQbwFDOaABwFcwAuZIkAaxAHsB3EAbl2VpOLIursZYF9s2MAE8CKAMJwANtIA8MDBAAekEABMAzmhAAafCE4A+ZAF5kACkXIAZFgOdyhEsn4BKANoAidmE5eAXRZsUEhYRBQALWACZBU1LR0sVl9HZDAAC2BNb2cwQLjVCA1tD1AYaGQ4KCgARkKE7UoaBhAPAP1yyuqoACYG4sTm3jaAgNY8AH5kDx7a-R7egIGSmbKQCqh8SABbeeQAOiOuragITTBajpmT7Ygd3v0jg9uzi6XxvC-kaY8y3f2wF2SyeRyksjk0QI+g8b0u+jhSyMn2+yHI7QmaOQIAgADdoNhBMIxCgAF4xAjidRmZDg+RQmEefYgmZgKBECD6eDSTQQMZGFgAekFXwAepMhKJxNw8dAoVSaXTITFGcz9ABma4eNkcrkyXn8oUivDioA

eloytoro avatar Feb 13 '23 18:02 eloytoro

Nice, thanks for implementing this Eloy!

I was thinking about implementing a ZipWith function too that would take a callback as parameter, similar to lodash's zipWith, and implement Zip using it. Do you want to give it a shot?

gvergnaud avatar Feb 14 '23 20:02 gvergnaud

I'll do this one, transforming this proposal to a tail recursive optimized one. image

type ZipImpl<arr1 extends unknown[],arr2 extends unknown[], acc extends unknown[]= []> =
  [arr1, arr2] extends [[infer item1, ...infer rest1], [infer item2, ...infer rest2]]
      ? ZipImpl<rest1, rest2,[...acc,[item1, item2]]>
      : acc

interface Zip extends Fn {
  output: this["input"] extends [infer arr1 extends unknown[], infer arr2 extends unknown[]]
    ? ZipImpl<arr1,arr2>
    : never
}

ecyrbe avatar Feb 15 '23 13:02 ecyrbe

Done on #26

ecyrbe avatar Feb 15 '23 20:02 ecyrbe