java 8 - What is side effects in functional programming? -


i learning java 8 newly , see 1 definition related functional programming "a program created using only pure functions , no side effects allowed".

one of side effects "modifying data structure in place".

i don't understand line because @ last need speak database storing or retrieving or updating data.

modifying database not functional means how speak database in functional programming ?

"modifying data structure structure in place" means directly manipulate input datastructure (i.e. list). "pure functions" mean

  • the result function of it's input , not other hidden state
  • the function can applied multiple times on same input producing same result. not change input.

in object oriented programming, define behaviour of objects. behaviour provide read access state of object, write access it, or both. when combining operations of different concerns, introduce side effects.

for example stack , it's pop() operation. produce different results every call because changes state of stack.

in functional programming, apply functions immutable values. functions represent flow of data, not change in state. functions stateless. , result of function either original input or different value input, never modified input.

oo knows functions, aren't pure in cases, example sorting: in non-functional programming rearrange elements of list in original datastructure ("in-place"). in java, collections.sort()` does.

in functional programming, apply sort function on input value (a list) , thereby produce new value (a new list) sorted values. function has no state , state of input not modified.

so generalize: given same input value, applying function value produces same result value

regarding database operations. contents of database represent state, combination of stored values, tables etc (a "snapshot"). of course apply function data producing new data. typically store results of operations db, changing state of entire system, doesn't mean change state of function nor it's input data. reapplying function again, doesn't violate pure-function constraints, because apply data new input data. looking @ entire system "datastructure" violate constraint, because function application changes state of "input".

so entire database system hardly considered functional, of course operate on data in functional way.

but java allows both (oo , fp) , mix both paradigms, choose whatever approach fits needs best.

or quote this answer

if have several needs intermixed, mix paradigms. not restrict using lower right corner of toolbox.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -