Functional Programming Objects - Immutable

I recently learned to program in Erlang as a long time C and C # developers. I am new to functional programming. Now I'm trying to understand how objects work in languages ​​like Scala. I was taught that OOP is a change in the state of a given object using its public methods. These methods alter the state of public objects and private members. But now I hear that in functional programming, all objects must be immutable. Well, I agree that when an assigned variable (in a given function) should remain a pointer to the same object. But does this mean “immutability” that I cannot change the interiors (properties, private members) of these objects using their public methods? This makes objects look like simple data containers. It extracts all the functionality outside of them.This makes objects more like structures in C. This is something that is strange to me. Maybe I missed something? Is it possible to use objects in the old way and still consider it functional programming?

+4
source share
3 answers

You mix three different concepts. Functional programming, variability, and OOP are three different things.

I was taught that OOP is a change in the state of a given object using publicly available methods.

Yes and no. The important thing in O OP is that you have objects that can transfer data and code (their member methods) at the same time, and that you communicate with objects using their interface so that the object can then

  • send a specific implementation of the called method
  • provide additional information that is stored in the data that the object carries (access to thisthe implementation of the method)
  • execute method implementation code

, , - .

, . , . , , , .


, .

. - , . , : , . - .

, . , . , - . . , , , . , , , . , : , , " ", ,


"", (, ) , ?

, . Scala . , , , . , .

. .

, - . apply(integer i), , , , ,

/** get i-th character */ 
String.apply(integer i) 

/** get value for key `i` */ 
TreeMap.apply(integer i)

C .


- ?

" ". oop- ( ), . , . , , . , ( : , , , - , ). FP .

. , , ( ), , , ( ).

+5

OOP - .

!

!

"", (, ) , ?

"" . "" , . , - .

- ?

, FP. , , FP.

, - ?

, , .


, :

  • . , C. . . : #
  • . , C. , . : # .
  • , (, #, Scala, Erlang): . , ( , ). , .
  • . - ( ).

:

  • . .
  • : . FP ​​ .

FP:

  • FP . FP.

  • , ? , ? "

    FP . FP .

  • pure-FP .

  • FP . . wiki. , .

  • , FP .


, :

  • - . , : / . FP ​​ .
  • - . . FP ​​ , () .
  • " - " . .
  • # Scala - . . . .

, , :

  • , . , .
  • OOP , . , ( ) .
  • FP . .
  • FP , ( ). = . , #, Scala, . . .
  • FP . . , . ( ) .

: FP- . , . , : .


:

  • . . .
  • FP . . FP .
  • , , - .
+3

"", (, ) ?

.

. .

. . , -. getter-like, -, .

C.

. C . .

0
source

Source: https://habr.com/ru/post/1693279/


All Articles