Press "Enter" to skip to content

Reduce Advanced – Part 4 of Functional Programming in JavaScript


hello flat I need that for lifestyle

pack well it’s Monday again for you guys

or me it’s Saturday evening and there is

a lot of hiking here behind me this

episode will be released automatically

to you guys on Monday while I am in a

canoe traversing the Swedish wilderness

last time I showed you a basic example

of reduce where we used it to summarize

the property of any series of objects

however reduce is not limited to

reducing a list to a number it can

reduce it to anything such as another

array or an object and that is what

we’re gonna do today I’m also gonna

throw a couple of other higher-order

functions into the mix up top previously

about how higher-order functions can

post very well together and I’m gonna

give you some more examples of this day

we’re gonna jump right into it on the

left side of the screen we see data dot

txt this is a tab separated file so

there is ABS here it’s ABS OH

steps

here and here and this is just some file

that we got out of some old system we

having assigned a task we are going to

turn it into something that looks a

little bit like this so we’re going to

turn it into a object literal and every

key and that object is going to be named

so Mark Johnson Mark Johnson here that’s

the same guy and his words or or order

items or whatever those are goes in an

array

so these orders going to be turned into

these so the first first the string have

a pretty string after the the item name

is the price of the item and the second

integer after the name of the item it’s

going to be the quantity of that this is

our mission

because we’re dealing with a file here

we need to import the FS namespace from

no I’m going to use a new require syntax

normally you would do something like

this but we are going to use modern

JavaScript which means that

yes so these are the same thing but

we’re gonna use this one now to read

from the file we just go FS not weed

file sir

beta XT and it wants no high up

let me show you what this looks like

Russell also the blow pop full pipe and

as you can see here where we’re getting

a buffer of bytes and that is not quite

what we want we expect that this to be

the contents of the data of txt file and

that is yes because in in programming

and in the computer strings are used to

long set of lines it doesn’t know how to

interpret that in text unless it has

some kind of mapping to an encoding

table a character encoding the most

common character encoding is utf-8 let’s

run that again yes we have let’s start

by I don’t know splitting this into an

array so split is just a method on the

string object that splits the string

into an array by splitting it using this

character here and this is a line break

so if we run this we’re gonna see an

array of straights

it’s an extra line here I it’s because

files always tend to have one final line

break so we get rid of that by just

calling trim on the string before we

pass it into split and what trim does is

that it just removes any line breaks or

space it at the end and star on a string

if you run that that little string at

the end is now gone before we start

using reduce on these lines

I would like to convert them into a more

manageable object first using map I’m

every line and I’m gonna split it on tab

characters see what that looks like okay

cool now we have an array of arrays

where each array is basically the

equivalent this let’s reduce it remember

that reduce takes two arguments first

takes a function and it also takes a

starting object and in the last episode

we used a number as starting object like

this but in this case we are going to

create an object clip so we’re going to

pass it object the function that we pass

to reduce once two arguments the first

one is the object that we are

constructing the the end goal that we

talked about in the beginning of this

episode and it’s it’s this one and it’s

going to be passed here on the first

loop and the second argument is the

thing that we are iterating in this case

it’s going to be the line and my what I

mean by that is these so reduce is going

to receive this item on the path this

array on the first iteration it will

receive this array on the second

iteration and so on let’s run that so

far

whoops ads returning undefined and that

is because output will be whatever

reduce returns on its final iteration so

let’s return the customer object and

alright it’s an empty object that is

because we’re returning it here and it’s

empty because we’re not doing anything

with it yet just for kicks let me

console.log the line out on it BAM

so you’ll see here that reduce iterates

over the array of arrays that this map

function trades so for each iteration it

receives an array mother array that is

the equivalent of lines in data dot txt

let’s get rid of this console.log i

wanna rename this customers as we’re

making a customer’s object and for every

customer we’re gonna make a property

remember that the first item of the line

first part is the name we’re gonna make

that an empty array let’s run it okay

cool we have an empty array for Mark

Johnson and we have an empty ready for

Nikita Smith and this is because even

though it will get this line than this

line then this line it will simply

overwrite the Mark Jones and property

every time and the Nikita sliprock every

time and now we’re gonna hand the

ordered items so we take this of it and

push to it we push an object so the one

name that is the second part of the line

that is the third part of the line and

we want the quantity which is the 4.5

line and we run that oh that’s a bit

hard to read I’m gonna teach you a trick

now check this out

Jason dot string a fie no to run that

again

Oh much better

so what we did here was create a JSON

string from the the output with two

spaces for indentation we have a bug

because only the last item here and the

caught here is actually being being

added to the array and that is because

we are overriding it here so we have to

copy this one

and just make sure that now we are

reusing the existing array if there

exists one otherwise we’re signing

anyway and we have an output so waffle

iron blender both there’s no quantity oh

I have a bug time free getting tired

right alright and we are done today we

looked at a more advanced reduce example

where instead of using a number we used

eight an object literal we also see some

chaining going on here

good functional code is made up of small

functions that do one thing and you just

find them all together and that is what

we do here we have a trim with a split

we have a map which in turn uses split

inside of it ah and we have a reduce the

chain ability that you see here is

something that you’re gonna see a lot of

if you do functional program I am going

to nibble a bit on the jura superstition

whiskey that we are supposed to spring

on the canoe trip the the other guys

won’t know that enabled because this

I’ll tear them so I am on vacation next

week so there won’t be an episode next

Monday but there will be the Monday

after that so please tell me what you

think that that episode should be about

either comment down below or send me a

tweet at MV Jamie oh and two weeks

without an episode that’s a long time

you might forget about me so make sure

that you subscribe you he quickie there

there or follow me on Twitter for some

of you guys this is good morning

and for me this is good night until next time stay curious

Please follow and like us: