Create a Sequence Helper to Transduce Without Changing Collection Types

Paul Frend
InstructorPaul Frend
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 6 years ago

A frequent use case when transducing is to apply a transformation to items without changing the type of the collection.

In this lesson, we'll create a helper to do just that. seq will be similar to into, except the target type will be inferred from the source collection. For example, if transducing from an array, seq will create an array as the output collection as well.

seq is thus more restrictive and easier to consume than into.

[00:00] Let's create a function Seq, short for sequence, which will call transduce under the hood. The difference to our Into helper is that Seq is going to infer the destination collection based on the type of the source collection. Seq is more constrained than Into, but useful when you know you don't want to change collection types.

[00:19] As their arguments then, all we need is a transform and a collection. I'm just going to copy the body from Into, and paste it in. The only change we need to make is remove the 2 two argument with an empty collection of the right type. For an array, this will be an empty array. Here, it'll be an empty object. Let's just make our error message a bit more generic.

[00:45] Let's try this out by doubling an array. We'll call Seq, and our transform will be a map that doubles values, and we need a source array with a few numbers. Let's try it out. We get an error, so it looks like we're still checking the 2 argument up here, whereas, really, we should be checking our collection. Let's fix that. Now ,we get our expected result.

[01:12] Let's also try this out with an object. For that, we'll create a transform called Flip, and it's going to flip the keys and the values. We'll call map, destructure the key and the value from our array, we'll need another bracket here, and we'll just return those two, flipped like so. We can call Seq with flip, and we'll pass in a plain object with three pairs.

[01:41] As expected, our returned object has the keys and the values flipped.

egghead
egghead
~ 29 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today