Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Corla Plankun
May 8, 2007

improve the lives of everyone
i think i 'get' functional programming and composability and stuff but a lot of my coworkers are really enthusiastic about it and i want to be a fp liker too

what were you doing the first time you went from thinkingn "fp is a different programming paradigm" to "fp is the best programming paradigm for this task"?

Adbot
ADBOT LOVES YOU

The Management
Jan 2, 2010

sup, bitch?
I think floating point has its uses but many people are not aware of the particular challenges and limitation it has.

TheFluff
Dec 13, 2006

FRIENDS, LISTEN TO ME
I AM A SEAGULL
OF WEALTH AND TASTE
writing a node js app, op

javascript is awful regardless of how you write it, so I was looking up ways to write it in some less awful way and came across lodash-FP, which seemed to me to be pretty cool and good, so I used that, and it was pretty cool and good as far as javascript goes, so now I'm a currying liker

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug
it’s good that

TheFluff posted:

writing a node js app, op

javascript is awful regardless of how you write it, so I was looking up ways to write it in some less awful way and came across lodash-FP, which seemed to me to be pretty cool and good, so I used that, and it was pretty cool and good as far as javascript goes, so now I'm a currying liker

it’s good that you like fp in JavaScript because it’s not like it has integers

The Management
Jan 2, 2010

sup, bitch?
people used to pretend that FP was good because it could automatically take advantage of parallelism. when that didn’t pan out, they switched to being indignant a having to write loops.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
fp made me aware of managing state better, before that I was just like yolo

TheFluff
Dec 13, 2006

FRIENDS, LISTEN TO ME
I AM A SEAGULL
OF WEALTH AND TASTE

Symbolic Butt posted:

fp made me aware of managing state better, before that I was just like yolo

:same:

NoneMoreNegative
Jul 20, 2000
GOTH FASCISTIC
PAIN
MASTER




shit wizard dad

it was 2011 OP

https://www.youtube.com/watch?v=ojQrhqW4hX0

Corla Plankun
May 8, 2007

improve the lives of everyone

The Management posted:

I think floating point has its uses but many people are not aware of the particular challenges and limitation it has.

i thought the title might confuse people but then i remembered that literally noone likes floating point representation so it didn't matter if it was misread :o:

gonadic io
Feb 16, 2011

>>=
i learned java and haskell at the same time.

going through loops and classes and objects one one hand, vs seeing this
code:
 quicksort [] = []
 quicksort (x:xs) = quicksort small ++ [x] ++ quicksort large)
   where small = [y | y <- xs, y <= x]
         large = [y | y <- xs, y > x]

atomicthumbs
Dec 26, 2010


We're in the business of extending man's senses.
why, without functional programming we wouldn't have the incredibly useful things made using it, like Cyc

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

Symbolic Butt posted:

fp made me aware of managing state better, before that I was just like yolo

this plus it made me think and plan more before doing stuff which is related

Raluek
Nov 3, 2006

WUT.
wait, theres a fp?

bump_fn
Apr 12, 2004

two of them
the fppening

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Fart Programming

The Management
Jan 2, 2010

sup, bitch?
fp made me realize that most developers have no idea how computers work

Plank Walker
Aug 11, 2005
linq is pretty nice i guess :shobon:

atomicthumbs
Dec 26, 2010


We're in the business of extending man's senses.
a bash oneliner that pipes files through multiple tools is functional programming. never clicking on this thread again so I won't see your replies

Zemyla
Aug 6, 2008

I'll take her off your hands. Pleasure doing business with you!
I was 12, at a nerd summer camp where they were teaching Scheme and propositional logic, and I was like, Holy poo poo, this is so much easier than BASIC!

The Management
Jan 2, 2010

sup, bitch?

atomicthumbs posted:

a bash oneliner that pipes files through multiple tools is functional programming. never clicking on this thread again so I won't see your replies

functional programming is not just about repeatedly copying arrays of data, although honestly it mostly is

Colonel Taint
Mar 14, 2004


fp is cool until something doesn't quite work as you expect and you're left wondering wtf and you can't debug because your 'clever' and 'elegant' one-liner is actually wrapping 10 different things and all you can do is stare at it and think and hope some epiphany about what's wrong pops into your head.

The epiphany that should come to you is that all programming is garbage and you're wasting your life

Farmer Crack-Ass
Jan 2, 2001

this is me posting irl
is fp better than oo? asking for a friend

Bloody
Mar 3, 2013

Symbolic Butt posted:

fp made me aware of managing state better, before that I was just like yolo

Plank Walker posted:

linq is pretty nice i guess :shobon:

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Colonel Taint posted:

fp is cool until something doesn't quite work as you expect and you're left wondering wtf and you can't debug because your 'clever' and 'elegant' one-liner is actually wrapping 10 different things and all you can do is stare at it and think and hope some epiphany about what's wrong pops into your head.

The epiphany that should come to you is that all programming is garbage and you're wasting your life

*morpheus voice* what if i told u that if you use functional programming you won't have to debug (because you won't make bugs)

OldAlias
Nov 2, 2013

Farmer Crack-rear end posted:

is fp better than oo? asking for a friend

yeah but in the Linux “worse is better” way. if there are libs that handle your problem domain well then use it. if not then you’re on your own and do it yourself. it can be significantly easier to reason about with the caveat of an upfront cost of time, to formulate a solution and to understand anything. you could use fp features in otherwise OO langs I guess

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

OldAlias posted:

yeah but in the Linux “worse is better” way. if there are libs that handle your problem domain well then use it. if not then you’re on your own and do it yourself. it can be significantly easier to reason about with the caveat of an upfront cost of time, to formulate a solution and to understand anything. you could use fp features in otherwise OO langs I guess

spending time doing fp in erlang made me better at ruby which is extremely oo

Winkle-Daddy
Mar 10, 2007
When I was first introduced to the idea of OOP I was skeptical but didn’t know why - it just felt “wrong”. After its introduction OOP became very popular (I will explain why later) and criticising OOP was rather like “swearing in church”. OOness became something that every respectable language just had to have.

As Erlang became popular we were often asked “Is Erlang OO” - well, of course the true answer was “No of course not” - but we didn’t to say this out loud - so we invented a serious of ingenious ways of answering the question that were designed to give the impression that Erlang was (sort of) OO (If you waved your hands a lot) but not really (If you listened to what we actually said, and read the small print carefully).

At this point I am reminded of the keynote speech of the then boss of IBM in France who addressed the audience at the 7th IEEE Logic programming conference in Paris. IBM prolog had added a lot of OO extensions, when asked why he replied:

“Our customers wanted OO prolog so we made OO prolog”

I remember thinking “how simple, no qualms of conscience, no soul-searching, no asking "Is this the right thing to do” …

My principle objection to OOP goes back to the basic ideas involved, I will outline some of these ideas and my objections to them.

Objection 1 - Data structure and functions should not be bound together
Objects bind functions and data structures together in indivisible units. I think this is a fundamental error since functions and data structures belong in totally different worlds. Why is this?

*Functions do things. They have inputs and outputs. The inputs and outputs are data structures, which get changed by the functions. In most languages functions are built from sequences of imperatives: “Do this and then that …” to understand functions you have to understand the order in which things get done (In lazy FPLs and logical languages this restriction is relaxed).

Data structures just are. They don’t do anything. They are intrinsically declarative. “Understanding” a data structure is a lot easier than “understanding” a function.
Functions are understood as black boxes that transform inputs to outputs. If I understand the input and the output then I have understood the function. This does not mean to say that I could have written the function.

Functions are usually “understood” by observing that they are the things in a computational system whose job is to transfer data structures of type T1 into data structure of type T2.

Since functions and data structures are completely different types of animal it is fundamentally incorrect to lock them up in the same cage.

Objection 2 - Everything has to be an object
Consider “time”. In an OO language “time” has to be an object. But in a non OO language a “time” is a instance of a data type. For example, in Erlang there are lots of different varieties of time, these can be clearly and unambiguously specified using type declarations, as follows:

code:
-deftype day() = 1..31.
-deftype month() = 1..12.
-deftype year() = int().
-deftype hour() = 1..24.
-deftype minute() = 1..60.
-deftype second() = 1..60.
-deftype abstime() = {abstime, year(), month(), day(), hour(), min(), sec()}.
-deftype hms() = {hms, hour(), min(), sec()}.
...
Note that these definitions do not belong to any particular object. they are ubiquitous and data structures representing times can be manipulated by any function in the system.

There are no associated methods.

Objection 3 - In an OOPL data type definitions are spread out all over the place
In an OOPL data type definitions belong to objects. So I can’t find all the data type definition in one place. In Erlang or C I can define all my data types in a single include file or data dictionary. In an OOPL I can’t - the data type definitions are spread out all over the place.

Let me give an example of this. Suppose I want to define a ubiquitous data structure. ubiquitous data type is a data type that occurs “all over the place” in a system.

As lisp programmers have know for a long time it is better to have a smallish number of ubiquitous data types and a large number of small functions that work on them, than to have a large number of data types and a small number of functions that work on them.

A ubiquitous data structure is something like a linked list, or an array or a hash table or a more advanced object like a time or date or filename.

In an OOPL I have to choose some base object in which I will define the ubiquitous data structure, all other objects that want to use this data structure must inherit this object. Suppose now I want to create some “time” object, where does this belong and in which object…

Objection 4 - Objects have private state
State is the root of all evil. In particular functions with side effects should be avoided.

While state in programming languages is undesirable, in the real world state abounds. I am highly interested in the state of my bank account, and when I deposit or withdraw money from my bank I expect the state of my bank account to be correctly updated.

Given that state exists in the real world what facilities should programming language provide for dealing with state?

OOPLs say “hide the state from the programmer”. The states is hidden and visible only through access functions.
Conventional programming languages (C, Pascal) say that the visibility of state variables is controlled by the scope rules of the language.
Pure declarative languages say that there is no state.
The global state of the system is carried into all functions and comes out from all functions. Mechanisms like monads (for FPLs) and DCGs (logic languages) are used to hide state from the programmer so they can program “as if state didn’t matter” but have full access to the state of the system should this be necessary.

The “hide the state from the programmer” option chosen by OOPLs is the worse possible choice. Instead of revealing the state and trying to find ways to minimise the nuisance of state, they hide it away.

Why OO was popular?
Reason 1 - It was thought to be easy to learn.
Reason 2 - It was thought to make code reuse easier.
Reason 3 - It was hyped.
Reason 4 - It created a new software industry.
I see no evidence of 1 and 2. Reasons 3 and 4 seem to be the driving force behind the technology. If a language technology is so bad that it creates a new industry to solve problems of its own making then it must be a good idea for the guys who want to make money.

This is is the real driving force behind OOPs.

ultravoices
May 10, 2004

You are about to embark on a great journey. Are you ready, my friend?

OldAlias posted:

yeah but in the Linux “worse is better” way. if there are libs that handle your problem domain well then use it. if not then you’re on your own and do it yourself. it can be significantly easier to reason about with the caveat of an upfront cost of time, to formulate a solution and to understand anything. you could use fp features in otherwise OO langs I guess

list(filter(lambda x: x in big_butts, map(fart, butts))) feels fun to write even if it is less clear than a loop.

atomicthumbs
Dec 26, 2010


We're in the business of extending man's senses.
if your software isn't formally verified, go gently caress yourself

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

ultravoices posted:

list(filter(lambda x: x in big_butts, map(fart, butts))) feels fun to write even if it is less clear than a loop.

that's really hard to read, you want:

code:
map(fart, butts)
|> filter(lambda x: x in big_butts)
|> list()

Bloody
Mar 3, 2013

atomicthumbs posted:

if your software isn't formally verified, go gently caress yourself

just say "formal methods" a lot nobody knows what it means

gonadic io
Feb 16, 2011

>>=
ive formally verified that my program is a piece of poo poo

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

gonadic io posted:

ive formally verified that my program is a piece of poo poo

yes i too us the formal verification tool `ls` to locate programs that are poses

Bloody
Mar 3, 2013

`ls` stands for `locate poo poo`

Star War Sex Parrot
Oct 2, 2003

atomicthumbs posted:

if your software isn't formally verified, go gently caress yourself
hell yeah check out the sweet invariants I wrote for my hash table
code:
  type hashtbl 'a = {
    mutable size: int ;
    mutable data: data 'a ;
    ghost mutable model: model 'a }

    invariant { length self.data > 0 }
    invariant {
      forall u:key. forall v:'a.
      mem (u, v) self.data[bucket u (length self.data)] <->
      Map.get self.model u = Some v }
    invariant {
      forall u:key. forall v:'a. forall w:'a.
      (mem (u,v) self.data[bucket u (length self.data)] /\
      mem (u,w) self.data[bucket u (length self.data)]) ->
      v = w }
    invariant {
      forall i: int. 0 <= i < length self.data ->
      forall u: key, v: 'a. mem (u,v) self.data[i] ->
      bucket u (length self.data) = i }
proving these during resize was sort of a bitch. this is the signature and contracts for a resize function whose body is like 6 lines
code:
    let rec rehash (new_data : data 'a) (l : bucket 'a) (ghost i : int) : unit =
      requires {
        forall j: int. 0 <= j < new_size ->
        forall u: key, v: 'a. mem (u,v) new_data[j] ->
        bucket u new_size = j }
      requires {
        forall u: key, v: 'a. mem (u,v) l ->
        bucket u old_size = i }
      requires {
        forall u:key. forall v:'a.
        0 <= bucket u old_size < i ->
        (mem (u, v) new_data[bucket u new_size] <->
        Map.get t.model u = Some v) }
      requires {
        forall u:key. forall v:'a.
        bucket u old_size = i ->
        (Map.get t.model u = Some v <-> mem (u, v) l \/
        (mem (u, v) new_data[bucket u new_size])) }
      requires {
        forall u:key. forall v:'a.
        bucket u old_size > i ->
        not (mem (u, v) new_data[bucket u new_size]) }
      requires { new_size = length new_data }
      ensures {
        forall j: int. 0 <= j < new_size ->
        forall u: key, v: 'a. mem (u,v) new_data[j] ->
        bucket u new_size = j }
      ensures {
        forall u:key. forall v:'a.
        0 <= bucket u old_size <= i ->
        (mem (u, v) new_data[bucket u new_size] <->
        Map.get t.model u = Some v) }
      ensures {
        forall u:key. forall v:'a.
        bucket u old_size > i ->
        not (mem (u, v) new_data[bucket u new_size]) }
      variant { l }

Bloody
Mar 3, 2013

Star War Sex Parrot posted:

hell yeah check out the sweet invariants I wrote for my hash table
code:
  type hashtbl 'a = {
    mutable size: int ;
    mutable data: data 'a ;
    ghost mutable model: model 'a }

    invariant { length self.data > 0 }
    invariant {
      forall u:key. forall v:'a.
      mem (u, v) self.data[bucket u (length self.data)] <->
      Map.get self.model u = Some v }
    invariant {
      forall u:key. forall v:'a. forall w:'a.
      (mem (u,v) self.data[bucket u (length self.data)] /\
      mem (u,w) self.data[bucket u (length self.data)]) ->
      v = w }
    invariant {
      forall i: int. 0 <= i < length self.data ->
      forall u: key, v: 'a. mem (u,v) self.data[i] ->
      bucket u (length self.data) = i }
proving these during resize was sort of a bitch. this is the signature and contracts for a resize function whose body is like 6 lines
code:
    let rec rehash (new_data : data 'a) (l : bucket 'a) (ghost i : int) : unit =
      requires {
        forall j: int. 0 <= j < new_size ->
        forall u: key, v: 'a. mem (u,v) new_data[j] ->
        bucket u new_size = j }
      requires {
        forall u: key, v: 'a. mem (u,v) l ->
        bucket u old_size = i }
      requires {
        forall u:key. forall v:'a.
        0 <= bucket u old_size < i ->
        (mem (u, v) new_data[bucket u new_size] <->
        Map.get t.model u = Some v) }
      requires {
        forall u:key. forall v:'a.
        bucket u old_size = i ->
        (Map.get t.model u = Some v <-> mem (u, v) l \/
        (mem (u, v) new_data[bucket u new_size])) }
      requires {
        forall u:key. forall v:'a.
        bucket u old_size > i ->
        not (mem (u, v) new_data[bucket u new_size]) }
      requires { new_size = length new_data }
      ensures {
        forall j: int. 0 <= j < new_size ->
        forall u: key, v: 'a. mem (u,v) new_data[j] ->
        bucket u new_size = j }
      ensures {
        forall u:key. forall v:'a.
        0 <= bucket u old_size <= i ->
        (mem (u, v) new_data[bucket u new_size] <->
        Map.get t.model u = Some v) }
      ensures {
        forall u:key. forall v:'a.
        bucket u old_size > i ->
        not (mem (u, v) new_data[bucket u new_size]) }
      variant { l }

idk what any of that means tbh

Silver Alicorn
Mar 30, 2008

𝓪 𝓻𝓮𝓭 𝓹𝓪𝓷𝓭𝓪 𝓲𝓼 𝓪 𝓬𝓾𝓻𝓲𝓸𝓾𝓼 𝓼𝓸𝓻𝓽 𝓸𝓯 𝓬𝓻𝓮𝓪𝓽𝓾𝓻𝓮

Bloody posted:

`ls` stands for `locate poo poo`

tweet this so I can re-tweet it

My Linux Rig
Mar 27, 2010
Probation
Can't post for 6 years!

Cocoa Crispies posted:

*morpheus voice* what if i told u that if you use functional programming you won't have to debug (because you won't make bugs)

:cawg:

Arcsech
Aug 5, 2008

Cocoa Crispies posted:

that's really hard to read, you want:

code:
map(fart, butts)
|> filter(lambda x: x in big_butts)
|> list()

some people in /r/haskell argue against this and say you should prefer the composition operator (.) because it is more like math

these people have been huffing math farts for way too long

Adbot
ADBOT LOVES YOU

atomicthumbs
Dec 26, 2010


We're in the business of extending man's senses.
Programming in a functional style can also be accomplished in languages that are not specifically designed for functional programming. For example, the imperative Perl programming language has been the subject of a book describing how to apply functional programming concepts.[29] This is also true of the PHP programming language.[30]

  • Locked thread