Once suspended, chrisvasqm will not be able to comment or publish posts until their suspension is removed.
Once unsuspended, chrisvasqm will be able to comment and publish posts again.
Once unpublished, all posts by chrisvasqm will become hidden and only accessible to themselves.
If chrisvasqm is not suspended, they can still re-publish their posts from their dashboard.
Once unpublished, this post will become invisible to the public and only accessible to Christian Vasquez.
They can still re-publish the post if they are not suspended.
Thanks for keeping DEV Community safe. Here is what you can do to flag chrisvasqm:
Unflagging chrisvasqm will restore default visibility to their posts.
Top comments (11)
A pure function is just a function that has no hidden inputs or outputs.
Incase you just want to read code...
Dad just spilled chocolate ice cream on the sheets. And the worst part is: He's supposed to be on a diet. It's only January 9th and he's already cheating. Mom can't find out. Thinking quickly, he runs into your room and gives you the scenario: He'll distract mom by bringing her out to a movie while you go get the sheets dry cleaned. There's a place around the corner and they know you so they'll be able to rush it. And mom won't ever know the difference.
Let's present two ways this plays out:
Dad gives you $40. You take the sheets down to the dry cleaners, hand them the money and the sheets. 45 minutes later they give you your change and the clean sheets. You get the sheets back on the bed just as your parents are getting home and mom is never the wiser.
Dad gives you his credit card. You go to the cleaners, pay with the card and get the sheets back in 45 minutes. Mom is never the wiser.
Until... she sees the credit card statement a week later and starts asking questions. Dad might be sneaky but he can't lie to her face and confesses. The jig is up.
Scenario one was a "pure function" you provided an input,
[cash, dirtySheets], and got back an output
[change, cleanSheets]. Nothing else meaningful happened in between the input and output time as far as you are concerned.
Scenario two was an "inpure function" because there was a side effect. The outside world was affected by the transaction went down. Within the transaction, a call to the credit card company where they made a request to add money to your statement.
A pure function is one where your output is always the exact same based on the input you provide and there are no side effects.
Let's say I have a function labeled.
assignNickname(name). A "pure" version of this might take the letters in your name, consult an internally consistent dictionary and give you an output. "Hannah" becomes "Hannah Bobanna" and "Ben" becomes "Benny Bobenny". Every time. The output is a "pure function of" the input. An alternative function, with the same name, could instead consult an external service which provides different nicknames based on the name and the current moon cycle. On a full moon
assignNickname(name)might turn Hannah's name to "WereHannah". It's a play on werewolf. It's not very clever. But the important thing is that the output is not always the same.
assignNickname(name)doesn't even "return" a name, but instead "assigns" the name to a record in the database, which is a perfectly reasonable thing it might do. It might take the name, perform a function, and then write it to a database table represented of the name passed in. This is impure because it is a "side effect". Even if that is totally the right behavior, it's still called a side effect because other parts of the system are affected by the function.
I haven't done an extensive amount of purely functional programming, so some of this might be a bit off-base, but this is the gist.
So why not write everything purely functional if it's so great? Well it's still pretty complicated and in some cases not worth the burden. Some functional die-hards would probably argue that everything should be done in this way, but take those arguments with some grains of salt. OOP has a lot of logical benefits. Functional programming is great for a lot of reasons. I mostly focused on "correctness" but there are also big performance benefits. But there's no such thing as a silver bullet. Functional programming can make some simple problems overly complicated, even if it can be a great help with the very complex ones. Don't go way out of your way to solve problems you do not have.
Just out of curiosity, how would you implement a pure function of
console.log? I have no experience in functional languages.
Instead of executing the side-effects (e.g. calling a function
logthat returns a
string), you create objects that represent those actions (e.g. calling a function
logthat returns a
You compose these "objects that represent actions" with other objects (usually with map/flatMap).
Once you're done composing all your effects, you bubble them up to
mainand use an escape hatch to trigger the execution of all side-effects at once (sometimes called
unsafePerformIO). In Haskell, you don't use the escape hatch at all - you bubble up all
main, and the runtime will go through your tree of effects and execute them. This is when things actually get "impure" - but you don't care anymore because it's now beyond the boundary of your application.
Here's a sample implementation of what
IO<A>would look like in scala: scalafiddle.io/sf/AnXRGhf/8 (should be fairly easy to read regardless of your background, let me know if it's not and you'd like to see this in another language)
You can't, so you hide it. In Elm's case, it's called
Debug.logto emphasize that it's not something to be used commonly. Essentially it looks like the identity function (takes an argument and gives it back) except that it logs it to the console.
Answer: You cheat by accepting that there are side-effects in order to run the program.
IO Stringin Haskell is just as pure as
String. Haskell only has a handful of escape hatches to break out of purity/referential transparency, like
unsafeInterleaveIO- but you don't need to (and shouldn't) use them at all. They're there for the runtime system to use and execute your code. See my other reply in this thread.
True. Thats why though Haskell is often considered the "most functional" out of others, but not Purely functional, because the type system allows for any side effects to be encapsulated within the context of a type.
That claim is as meaningful as the claim that no computer is Turing complete because they all have finite memory.
They are if you ignore any resource limitations and thats the point really. A Turing machine is more of a mathematical model than a real world implementation as you cannot possibly express the notion of infinity in computer systems. There has to be an upper limit somewhere.
My two general rules to ensure a pure function.
Notes for impure languages (languages which allow mutation and other side effects from user code).
I'm a little lenient with #1 in cases where it is obvious the external data will never change as long as the program is running. E.g. constants, or readonly data read from a config on startup. The latter is still better as an input parameter.
One common way new devs violate #1 is to get the current date/time or generate a random number. You want to do this outside your pure function and pass the result in so the function remains pure (and testable).
The #2 allows a function to mutate a local variable and remain pure. As long as the variable is only created/used inside the function, the function still has no side effects that ripple to the outside.