Weekly check-in 7/26
Jul. 27th, 2015 03:58 pmI posted my weekly checkin to the TLC mailing list last night, but it was mostly whiny complaining about how I didn't get anything done, so I'm not going to repeat that here.
Instead I just want to talk about something in JavaScript that confuses me. (Maybe just because I'm not used to imperative programming.)
So, I want to represent some comics and comic book characters. So I make some objects.
and I say that those are the characters:
and then I make up some comics:
The only problem is, Pepper isn't CEO of Stark Industries in (not real comic) "Adventures of Pepper". It's an Alternate Universe where she is a Professional Adventurer. But that's easy to fix, right?
And if I look at
But this is where I get confused -- because Pepper is now an adventurer in Iron Man too!!
This is not good!! Stark Industries is sure to go bankrupt without Pepper!
I guess I just find this confusing because I'm not able to alter values like this in Haskell. This is what something similar might look like in Haskell:
This results in the expected:
That's because
So it's constantly surprising when I run into these side effects in JavaScript and I find them kind of hard to understand. HOWEVER I can see how this could also be super useful and make things easier than what I'm used to!!
Instead I just want to talk about something in JavaScript that confuses me. (Maybe just because I'm not used to imperative programming.)
So, I want to represent some comics and comic book characters. So I make some objects.
var tony = { name: "Tony", job: "billionaire playboy philanthropist" }
var pepper = { name: "Pepper", job: "CEO of Stark Industries" }
and I say that those are the characters:
var characters = [ tony, pepper ]
and then I make up some comics:
var ironMan = { title: "Iron Man", characters: characters };
var pepperAdventures = { title: "Adventures of Pepper", characters: [pepper] };
The only problem is, Pepper isn't CEO of Stark Industries in (not real comic) "Adventures of Pepper". It's an Alternate Universe where she is a Professional Adventurer. But that's easy to fix, right?
pepperAdventures.characters[0].job = "Adventurer";
And if I look at
pepperAdventures now, I see that Pepper's job is correct!
> pepperAdventures
{ title: 'Adventures of Pepper',
character: { name: 'Pepper', job: 'Adventurer' } }
But this is where I get confused -- because Pepper is now an adventurer in Iron Man too!!
> ironMan
{ title: 'Iron Man',
characters:
[ { name: 'Tony',
job: 'billionaire playboy philanthropist' },
{ name: 'Pepper', job: 'Adventurer' } ] }
This is not good!! Stark Industries is sure to go bankrupt without Pepper!
I guess I just find this confusing because I'm not able to alter values like this in Haskell. This is what something similar might look like in Haskell:
data Character = Character { name :: String, job :: String }
data Comic = Comic { title :: String, characters :: [Characters] }
tony = Character "Tony" "billionaire playboy philanthropist"
pepper = Character "Pepper" "CEO of Stark Industries"
characters = [ tony, pepper ]
ironMan = Comic "Iron Man" characters
pepperAdventures = Comic "Adventures of Pepper" [ pepper { job = "Adventurer } ]
This results in the expected:
>pepperAdventures
Comic {
title = "Adventures of Pepper",
characters = [
Character {name = "Pepper", job = "Adventurer"}]}
>ironMan
Comic {
title = "Iron Man",
characters = [
Character {name = "Tony", job = "billionaire playboy philanthropist"},
Character {name = "Pepper", job = "CEO of Stark Industries" }]}
That's because
pepper {job = "Adventurer"} creates a new and different Character that's the same as pepper except that the "job" field is "Adventurer". It doesn't change the original pepper at all.So it's constantly surprising when I run into these side effects in JavaScript and I find them kind of hard to understand. HOWEVER I can see how this could also be super useful and make things easier than what I'm used to!!