#reduce typing

62 messages ยท Page 1 of 1 (latest)

steel wraithBOT
#
onkeltem#0

Preview:```ts
type Tuple = [string, string]

const res = ["a", "b"].reduce(
(acc: string[], item: string) => {
acc.push(item)
return acc
},
[] as unknown as Tuple
)```

eternal cobalt
#

How to force returning Tuple here?

#

When I put "Tuple" as the acc type, it fails:

steel wraithBOT
#
onkeltem#0

Preview:```ts
type Tuple = [string, string]

const res = ["a", "b"].reduce(
(acc: Tuple, item: string) => {
acc.push(item)
return acc
},
[]
)```

eternal cobalt
#

[] as string[] doesn't help as well

tight plinth
#

not possible

#

because it's not correct

#

at the type level

#

you have an array, and you add elements to it

#

so that array can't have a fixed size

steel wraithBOT
#
ascor8522#0

Preview:```ts
type Tuple = [string, string]

const res = ["a", "b"].reduce(
(acc: string[], item: string) => {
acc.push(item)
return acc
},
[]
) as Tuple```

eternal cobalt
#

I don't really care about some type level ๐Ÿ™‚ I just need to have it done

tight plinth
eternal cobalt
#

haha

tight plinth
eternal cobalt
#

it seems reasonable for me

tight plinth
#

also using a reduce to push some elements looks kinda sus

#

use a filter and a map instead

eternal cobalt
#

why?

#

ah, that

#

Yeah, but the real task is different of course

tight plinth
#

yeah, but using reduce in TS is not that simple

#

especially when you are changing the object at every iteration

#

like adding/removing keys on an object

#

adding/removing elements to an array

#

etc.

#

imo this is where functionnal programming really shines
apply all your transformations at once (map + filter), then recompose your object/array (fromEntries)

eternal cobalt
#

Yep, I agree. But that's what we have to do

#

hmmm

#

recomosing! I forgot about fromEntries

#

Wait, but can fromEntries group something for example? I can't tell that off the top of my head

tight plinth
#

group something?

#

you mean, like a "group by"?

#

fromEntries can only create an objhect based on key-value tuples

#

you can very well use reduce along with a map to group elements

steel wraithBOT
merry robin
#

!ts

steel wraithBOT
#
type Tuple = [string, string]

const res = ['a', 'b'].reduce<Tuple>((acc, item: string, i) => {
  acc[i] = item
  return acc
}, ['', ''])




merry robin
#

๐ŸšŽ

eternal cobalt
#

Ohh

#

Bah

merry robin
#

[] as unknown as Tuple would still work too:

merry robin
eternal cobalt
eternal cobalt
#

@merry robin

steel wraithBOT
#
onkeltem#0

Preview:```ts
type A = {id: number}
type B = {uuid: string}
type C = {val: any}
type Item = A | B | C

class Foo<T extends readonly Item[]> {
constructor(public items: [...T]) {}

group() {
return [
(acc: Item[][], text: string, index: number) => {
const offset = in
...```

eternal cobalt
#

Well, it takes a sequence of strings (they're not used in this example tho) and returns triples of items (again, this is just an example, in RL items are functions which are executed)

#

Meh, sounds not specific. Let me come up with another example

#

Ok, here is a better one, I hope

steel wraithBOT
#
onkeltem#0

Preview:```ts
type A = {run: (arg: string) => string}
type B = {run: (arg: string) => number}
type C = {run: (arg: string) => boolean}
type Item = A | B | C

type MapItemToResult<T extends readonly Item[]> = {
[K in keyof T]: ReturnType<T[K]["run"]>
}

class Foo<T extends readonly Item[]> {
constructor(public items: [...T]) {}

group() {
return [
(
acc:
...```

eternal cobalt
#

This is what I want to get. So it's a grouping thing

#

So it takes a uniform sequences (of arguments) and returns a grouped sequence of results

#

My real task.
Imagine we have a sequence of text strings. And they represent a lesson, where odd members are questions and even - answers.
Now we want to seed them to TTS engines.
As there are two types of strings - Q-s and A-s, we need two TTS engines.

#

A sample input: ['#f00', 'red', '#0f0', 'green', '#00f', 'blue']

#

Not a real one of course ๐Ÿ™‚

#

But still makes sense haha

#

TTS engines be like:

type A = {run: (arg: string) => Output<A..>}
type B = {run: (arg: string) => Output<B..>}
type C = {run: (arg: string) => Output<C..>}
#

so now when I call the group() function (which is synthReduce() in my code), I receive an array of tuples: [Output<A>, Output<B>, Output<C>][]

#

In fact, I don't care much about the exact output type. I think it would be enough to get what they have in common. There are pcmData: Buffer and duration: number fields.

#

But. As I really can fetch this type, then why losing this info? Right?

#

So this task of grouping might not have a practical application at first glance