[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Can someone explain in laymen's terms what "functional
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 20
Thread images: 2
File: image.png (673 KB, 1180x842) Image search: [Google]
image.png
673 KB, 1180x842
Can someone explain in laymen's terms what "functional programming" is?

I can program in MATLAB and Java, and am considering learning Haskell but everyone keeps rambling on and on about "functional" programming.
>>
hard to explain just like that
Both OOP and functional programming are about breaking a problem into pieces that can be then mixed together for reusability.
Only that in functional programming the data isn't encapsulated and interfaced via methods to other classes which themselves encapsulate data. The data is passed around between functions, which are the actual agents (rather than objects) that carry the actions of your program.
Functions are such objects as well. A characteristic of functional programming is first-class functions and higher order functions.
This means functions can be passed around just like integers and strings and all those datatypes you're used to. For example you can have a function treewalk() that takes a tree and another function, and walks the tree applying the function to each node. This works for agenerality of tree structures without the need for polymorphism and classes.
Another important part (actually enforced by haskell but not defining of functional programming as a whole) is that functions should not mutate data, rather, just return an output from a given input without corrupting it. For example
list <- [1, 2, 3]
list2 <- map list (\x *2)

here list and list2 are different, map doesn't alter the contents of the original list. As you can see a function is passed: (\x x*2)
This is an anonymous function (takes an argument and returns that argument multiplied by two), called a lambda, which as you can see by the example, is a useful and often used concept in functional programming.
Oh and that was pseudocode, not actual haskell or any other real language
>>
First class functions and an emphasis on function purity and immutable data structures
>>
>>54378405
Thanks, this is interesting.
>>
>>54378261

In its purest terms, it's programming based on a view of "computation as substitution":

https://en.wikipedia.org/wiki/Lambda_calculus#Beta_reduction


Conceptually, in imperative programming a program is a sequence of statements that modify the "current state" of the machine. This state has two parts: the current instruction (Instruction Pointer, Program Counter, etc), and the "store" (the rest of registers and memory locations).

In pure functional programming there is only one instruction, "substitution", and the state has only one part, the "current term", analogous to the Program Counter but instead of being a number to point to the next instruction (useless because it's always "substitution") it's a term in some formal language (a syntax tree).
>>
>>54378261
>everyone keeps rambling on and on about "functional" programming.
And by everyone you mean /g/
>>
>>54378261
>this is a 44 year old

I fucked up
>>
>>54378824
Most javascript-related languages/frameworks exercise functional paradigms primarily and strongly. The whole web is made of JS. So no, it's not just /g/.

inb4 it's not purely functional. No other paradigm comes in its purest form as the sole paradigm of a language most of the time
>>
>>54378824
And by everyone you mean /g/

/g/ is pretty much the extent of my entire social interaction
>>
>>54378825
No fucking way he is-

>google it


Holy shit.
>>
>>54378790
>This state has two parts: the current instruction (Instruction Pointer, Program Counter, etc), and the "store" (the rest of registers and memory locations).
The store is an abstraction of the outside world. There doesn't have to be a store to be imperative, just actions that can be performed.

>In pure functional programming there is only one instruction, "substitution", and the state has only one part, the "current term", analogous to the Program Counter but instead of being a number to point to the next instruction (useless because it's always "substitution") it's a term in some formal language (a syntax tree).
This also describes Algol.
>>
File: image.jpg (37 KB, 310x349) Image search: [Google]
image.jpg
37 KB, 310x349
>>54379125
Definitely a more youthful looking PM than his dad was.
>>
>>54378887
Not sure what you're talking about. You should look into angular and typescript, it's easily the most popular js framework and it's OO through and through
>>
>>54379502
angular is the meme part of a webdev stack tho
>>
>>54379517
You said 'most' js frameworks yet you ignored the biggest one. It doesn't matter if you like it or not, it's OO and you were wrong lol
>>
http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey
>>
>>54378405
Am I wrong in thinking this is for a completely different set of problems?

A lot of problems is just easier to explain as if it is objects.

say you wanted to find the shortest path between two points.
Usually people use A* for this, but you need a way to store the data, so you make a graph with all the nodes, and all those nodes could have a vertice.

Making this OOP style would be rather simple.

You make a vertice and a node class.
Your graph might also include a data structure where you store the positions so they are easy to find (kd-tree, hashtable, whatever).
All these things, In a OOP style would be a part of a class.
You could be the one who implemented that class or you could be the one who uses it.
The user, would just want to know how to instantiate the class and then call the "get path" function.
maybe that would be.
graph.get_path(path,a,b)


In the functional programming style, would this then be
path = get_path(a,b,graph)
?
or would it be structured completely different?
>>
>>54380423
It's because you are used to it. If you enter from a point of view with a functional mentality then you would probably solve it in another way.
You can do data types in functional programming languages too, this is not something new OOP introduced.
>>
>>54380530
No but you associate actions with data structures.
Like balancing a tree makes sense as a private function of a tree class that is automatically called when you change things in the tree rather than a function someone outside the class would call.

Maybe a sensible mix is fine.
>>
>>54380631
That's because you assign things properties. The design philosophy of functional programming is that you don't have any internal state. Instead you have a pipeline of functions that modify your data.

I'm not saying one is better than the other, I'm saying it's really about which the programmer is used to.
Thread replies: 20
Thread images: 2

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.