The last few days since therapy my head has been all over the place. So many things from the past are coming back to me. And I have blocked a lot of things. They say to start from the beginning but my head doesn’t track a timeline very well. I forget when things happen. I just remember that they happened. In case anyone reads this I guess I should add a trigger warning. This is your trigger warning for what is ahead.
This is all scary to put out there. But I need to get some things out of my head. My head has been thinking of how I couldn’t afford the money for my ex’s drugs and I would have to pay another way. I would try to save money, but it was hard with needing to buy his cigarettes, alchohol, and anything else he wanted. On top of that I needed to take care of my kids and bills. My work didn’t pay that much for saving any money was difficult. I remember being tricked to take drugs. He said it was marijuana and I believed him. I knew something was off when I wasn’t falling asleep. The smoke was white, I was wide awake, paranoid. He gave me meth. I know I should have known better but I was naive. I feel stupid and angry at myself. I don’t like drugs. Why did I let him do that? I was too scared to say no. I feel ashamed for what he had me do while I was under the influence. I feel disgusted. I did say no the next night when he wanted me to do it again. He screamed at me for a week after. Threw things at me. Hit me. Made me feel like I failed.
I remember my privacy taken away. He followed me everywhere. Even into the bathroom. I got used to someone watching me do a number 2 or taking care of my period. I had to leave my phone with him at all times. He would get mad when my phone told me the number of updates it has because he thought they were the number of texts. I was isolated from my friends and family. Little by little he made sure that he was the only one that I conversed with, that I hung out with. I’m surprised that he didn’t follow me around at work. But he did have me text me at every break I got, including when I got to work, when I left work. To call him every lunch break. He made sure that I didn’t hang out with my coworkers. I was to be home at a certain time. He timed how long it took for me to get home. He was controlling, manipulative, angry, and violent.
I remember having to have coitus every chance he wanted it. We have done it upwards to 15 times in one day. Everyday was like this. We did it so often that I had to get a biopsy during one of my exams. I was so torn up that the doctor asked if there was anything that I wanted to talk about. I couldn’t talk because he was in the room with me. I wouldn’t talk because I was too scared to. I remember having to do it with other people while he watched. I feel disgusted and shamed. How could I allow this to happen to me? Why did it happen to me?
I did tell one person and that person helped me. Tried to get me to go to the police so many times but I couldn’t. I was too scared to bring myself to do that. Towards the end, things were getting worse. He was becoming more violent, more angry. Finally, after so many years, he was arrested. Not for anything he has done to me. But what he has done in the past. Because he was running this whole time. I was told that my kids would be taken away if I turned him in. That I would be locked up as well. I was stupid to believe that. But I didn’t turn him in. I had nothing to do with his arrest. After he was was gone I thought everything would be fine, that I would be fine. I was wrong. My mental state became unstable. My anxiety was so bad that I cried every day. I didn’t know why I was crying. I didn’t know what to do. I have been isolated for so long that I felt alone. I slept with the light on because I was too scared to sleep in the dark. I had to slowly get back to all my friends. My family.
I remember telling my girls that we had to move. To leave their friends and school behind. They weren’t happy but accepted it. I sold my house in less than a month. Took the loss on the money. Didn’t matter to me. But it was the only money I had. He had spent my annuity account and savings. All of it.
But I’m happier now. I’m safe. My girls are safe. I have freedom now. But why am I still trapped in the past? Why do I still have flashbacks? Why am I still paranoid and scared at times? Part of me wants to delete this post but part of me wants to post it. I know that the longer I sit in that negative spot in my mind, the longer I will stay there and the harder it will be to get out. I want out of my head. I know I can do this. I have been through a lot but I know I will get better. A little at a time. It’s a slow process too.
I have my javescript notes at the bottom here. I wrote the notes before I wrote my post so I didn’t know what I’d be writing about when I started the notes. So it may seem a little weird that I have notes along with this post. But I don’t want to delete my notes so there it is.
JavaScript notes…
———————————-
So far, we have seen two distinct principles for functional programming:
Don’t alter a variable or object – create new variables and objects and return them if need be from a function. Hint: using something like const newArr = arrVar, where arrVar is an array will simply create a reference to the existing variable and not a copy. So changing a value in newArr would change the value in arrVar.
Declare function parameters – any computation inside a function depends only on the arguments passed to the function, and not on any global object or variable.
Adding one to a number is not very exciting, but we can apply these principles when working with arrays or more complex objects.
Rewrite the code so the global array bookList is not changed inside either function. The add function should add the given bookName to the end of the array passed to it and return a new array (list). The remove function should remove the given bookName from the array passed to it.
Note: Both functions should return an array, and any new parameters should be added before the bookName parameter.
/* This function should add a book to the list and return the list */ // New parameters should come before bookName function add(list, bookName) { return [...list, bookName]; } /* This function should remove a book from the list and return the list */ // New parameters should come before the bookName one function remove(list, bookName) { return list.filter(book => book !== bookName); } var newBookList = add(bookList, 'A Brief History of Time'); var newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies'); var newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies'); console.log(bookList);
// the global variable var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"]; /* This function should add a book to the list and return the list */ // New parameters should come before bookName function add(list, bookName) { return [...list, bookName]; } /* This function should remove a book from the list and return the list */ // New parameters should come before the bookName one function remove(list, bookName) { return list.filter(book => book !== bookName); } var newBookList = add(bookList, 'A Brief History of Time'); var newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies'); var newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies'); console.log(bookList);
So far we have learned to use pure functions to avoid side effects in a program. Also, we have seen the value in having a function only depend on its input arguments.
This is only the beginning. As its name suggests, functional programming is centered around a theory of functions.
It would make sense to be able to pass them as arguments to other functions, and return a function from another function. Functions are considered first class objects in JavaScript, which means they can be used like any other object. They can be saved in variables, stored in an object, or passed as function arguments.
Let’s start with some simple array functions, which are methods on the array object prototype. In this exercise we are looking at Array.prototype.map(), or more simply map.
The map method iterates over each item in an array and returns a new array containing the results of calling the callback function on each element. It does this without mutating the original array.
When the callback is used, it is passed three arguments. The first argument is the current element being processed. The second is the index of that element and the third is the array upon which the map method was called.
See below for an example using the map method on the users array to return a new array containing only the names of the users as elements. For simplicity, the example only uses the first argument of the callback.
const users = [ { name: 'John', age: 34 }, { name: 'Amy', age: 20 }, { name: 'camperCat', age: 10 } ]; const names = users.map(user => user.name); console.log(names);
The console would display the value [ ‘John’, ‘Amy’, ‘camperCat’ ].
The watchList array holds objects with information on several movies. Use map on watchList to assign a new array of objects to the ratings variable. Each movie in the new array should have only a title key with the name of the film, and a rating key with the IMDB rating. The code in the editor currently uses a for loop to do this, so you should replace the loop functionality with your map expression.
// Only change code below this line let ratings = watchList.map(movie => { let movieData = {}; movieData["title"] = movie["Title"]; movieData["rating"] = movie["imdbRating"]; return movieData; }) // Only change code above this line console.log(JSON.stringify(ratings));
As you have seen from applying Array.prototype.map(), or simply map() earlier, the map method returns an array of the same length as the one it was called on. It also doesn’t alter the original array, as long as its callback function doesn’t.
In other words, map is a pure function, and its output depends solely on its inputs. Plus, it takes another function as its argument.
You might learn a lot about the map method if you implement your own version of it. It is recommended you use a for loop or Array.prototype.forEach().
Another useful array function is Array.prototype.filter(), or simply filter().
filter calls a function on each element of an array and returns a new array containing only the elements for which that function returns a truthy value – that is, a value which returns true if passed to the Boolean() constructor. In other words, it filters the array, based on the function passed to it. Like map, it does this without needing to modify the original array.
The callback function accepts three arguments. The first argument is the current element being processed. The second is the index of that element and the third is the array upon which the filter method was called.
See below for an example using the filter method on the users array to return a new array containing only the users under the age of 30. For simplicity, the example only uses the first argument of the callback.
const users = [ { name: 'John', age: 34 }, { name: 'Amy', age: 20 }, { name: 'camperCat', age: 10 } ]; const usersUnder30 = users.filter(user => user.age < 30); console.log(usersUnder30);
The console would display the value [ { name: 'Amy', age: 20 }, { name: 'camperCat', age: 10 } ].
The slice method returns a copy of certain elements of an array. It can take two arguments, the first gives the index of where to begin the slice, the second is the index for where to end the slice (and it's non-inclusive). If the arguments are not provided, the default is to start at the beginning of the array through the end, which is an easy way to make a copy of the entire array. The slice method does not mutate the original array, but returns a new one.
Here's an example:
const arr = ["Cat", "Dog", "Tiger", "Zebra"]; const newArray = arr.slice(1, 3);
newArray would have the value ["Dog", "Tiger"].
A common pattern while working with arrays is when you want to remove items and keep the rest of the array. JavaScript offers the splice method for this, which takes arguments for the index of where to start removing items, then the number of items to remove. If the second argument is not provided, the default is to remove items through the end. However, the splice method mutates the original array it is called on. Here's an example:
const cities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"]; cities.splice(3, 1);
Here splice returns the string London and deletes it from the cities array. cities will have the value ["Chicago", "Delhi", "Islamabad", "Berlin"].
As we saw in the last challenge, the slice method does not mutate the original array, but returns a new one which can be saved into a variable. Recall that the slice method takes two arguments for the indices to begin and end the slice (the end is non-inclusive), and returns those items in a new array. Using the slice method instead of splice helps to avoid any array-mutating side effects.
Concatenation means to join items end to end. JavaScript offers the concat method for both strings and arrays that work in the same way. For arrays, the method is called on one, then another array is provided as the argument to concat, which is added to the end of the first array. It returns a new array and does not mutate either of the original arrays. Here's an example:
[1, 2, 3].concat([4, 5, 6]);
The returned array would be [1, 2, 3, 4, 5, 6].
Use the concat method in the nonMutatingConcat function to concatenate attach to the end of original. The function should return the concatenated array.
function nonMutatingConcat(original, attach) { // Only change code below this line // Only change code above this line } const first = [1, 2, 3]; const second = [4, 5]; nonMutatingConcat(first, second);
function nonMutatingConcat(original, attach) { // Only change code below this line return original.concat(attach); // Only change code above this line } const first = [1, 2, 3]; const second = [4, 5]; nonMutatingConcat(first, second);
Functional programming is all about creating and using non-mutating functions.
The last challenge introduced the concat method as a way to merge arrays into a new array without mutating the original arrays. Compare concat to the push method. push adds items to the end of the same array it is called on, which mutates that array. Here's an example:
const arr = [1, 2, 3]; arr.push(4, 5, 6);
arr would have a modified value of [1, 2, 3, 4, 5, 6], which is not the functional programming way.
concat offers a way to merge new items to the end of an array without any mutating side effects.
Change the nonMutatingPush function so it uses concat to merge newItem to the end of original without mutating original or newItem arrays. The function should return an array.
function nonMutatingPush(original, newItem) { // Only change code below this line return original.push(newItem); // Only change code above this line } const first = [1, 2, 3]; const second = [4, 5]; nonMutatingPush(first, second);
Category: Uncategorized