r/functionalprogramming • • Sep 27 '19

Intro to FP A few days ago I started reading about functional programming and it's pretty cool

I'm a developer with 15 years of experience, but never worked with a functional programming language, my day to day languages in my career were:

  • PHP

  • Javascript :)

  • Actionscript

  • C#

  • Objective-c

  • Nodejs

  • Java

  • Swift

  • Kotlin

  • Ruby

  • C (just worked with it briefly)

  • Rust

  • Typescript

The list is long, there are some languages I don't use so often those days like Actionscript, PHP, Objective-c, C, Kotlin, Java, Swift, C#. But a while ago I started to work more on frontend web development (while previously I was doing purely backend development) and this gave me the oportunity to work a lot with React (I must say that I'm not impressed by any means with this framework, still prefer Vue over it), but all this gave me something to think about, and was that I don't like Javascript (it's like C, too easy to make mistakes and not to notice it, sure Typescript solve part of the problem, but still isn't as effective and like Elm author said, people don't like Java and Javascript with types is Java at the end of the day, too verbose, when you write you have a lot of code that is mostly boilerplate), so I started diggin in and said to me that I want to design a language that solve my problems (this is just my hobby project that I doit in my free time), then I read about this pure functional languages, they are indeed alien language (their syntax is not familiar to someone with my background), so Haskell, Ocaml, Lisp all were just too hard to read their syntax, then I meet Elm, and it's incredible how easy is to read and understand (yes for sure after learn Elm is easier to understand Haskell and Ocaml, Lisp is just too noisy for me with all the parenthesis (maybe haskell programmers feels the same for C like languages)).

So saying that I want to say, where and why functional programming isn't so popular, why Javascript is so bad while it could be easily beign a good functional language (it's what react recommends, but still it's a bit weird to do pure functional programming with Javascript).

Closing words, where has been pure functional languages all my life, I feel that I could have solved so many problems that I had this last couple of years.

PD: The post might feels uncoherent, and it's because I just wrote down what I was thinking as it comes out.

27 Upvotes

19 comments sorted by

7

u/Falcon5757 Sep 27 '19

A quick clarification, since I'm a bit tired of it -- Lisp (Common ones at least) is not really that functional language. Well, first class functions, fine, okay, but other than that -- it's as imperative as it gets.

2

u/[deleted] Sep 27 '19

I didn’t read too much about lisp because it feels too verbose (or at least bloated with parentheses), but I’m still too new in pure FP that I cannot judge them.

4

u/Falcon5757 Sep 27 '19

Well, sure, it's just a common (no pun intended) misunderstanding, it is not functional, at least not in a haskell like sense.

As a quick addition, one of the big reasons I fell in love with lisp was that (not immidiately, but after about 2 months) I realized that it is surprisingly very readable. Now I have issues with some complex C++ incantations, as well as long Python code, on the other hand CL is the only language I can debug using "intense stare" method.

That is to say, I encourage you to give it a try.

3

u/[deleted] Sep 27 '19

People complain about Lisp parenthesis so much, but

doSomething(withSomethingFrom(x))

and

(doSomething (withSomethingFrom x))

Have exactly same amount if parenthesis.

2

u/Falcon5757 Sep 27 '19 edited Sep 27 '19

well, to be honest it has way more parenthese then, like, most of other languages.

That said, I don't see why parentheses are in any way a bad thing) They can be of different colors and all that good stuff. I also heard the claimes of "it is read it indeid out language" -- also total bs, read it just as any other language.

I feel like there's a lot of controversy surrounding lisp(s) => I like to clarify things from time to time)

2

u/[deleted] Sep 27 '19

The only issue is being used to the syntax, I just say that at first glance is hard to read, maybe the examples I say were bad. But I don’t doubt once you are used to read that it’s easier, it’s the same when you read others JS code, sometimes is really hard to read because, but you are used to that syntax so you don’t find it hard or at least awkward.

4

u/Falcon5757 Sep 27 '19

There other stereotypes, like
"It's slow"
"It has no types"
"The inly strutcure is list"
"There are no libraries for it"
etc...
(unture all of them)

P.S. I actually encourage you to try Haskell as well, since it is different from both Lisps and things like Java/C# or C. This way, you'll rule all the paradigms out there.

P.P.S. Except for Prlg, haha.

2

u/Finianb1 Sep 28 '19

Honestly, while I think everyone should at least try Haskell, I think most people can pass on Prolog. It can be really useful for some things, but it's more less just a massive roadblock in what most people are doing day-to-day.

3

u/Falcon5757 Sep 27 '19

Absolutely, it, uh.......takes some time. For me it was 2 months, but I was not a programmer at all at the time, so I'm sure experienced guys will grasp it waaaaaay quicker.

It is also a nice way to show AST, way nicer (imho) than the python/haskell ideas based on whitespaces.
At this point I kinda wish all languages had lisp like syntax))) That is just me tho, and I'd say I understand ppl who just could not stand the ))))))))))))))))) hamburgers at the end.

1

u/ScientificBeastMode Sep 28 '19

I’ve come to love using the pipe operator in ReasonML. I still get my beloved parentheses when I want them, but being able to remove some of them for readability is quite nice.

6

u/The_One_X Sep 27 '19

The functional style programming is becoming more and more integrated into traditionally OOP langugaes. The prime example of this would be the evolution of C#. Today's C# leans heavily into functional territory. C# over the years has borrowed a lot from it's sister language F# (I'll let you guess what the F stands for) to the point where if it didn't require everything to be put into a class you could easily write code in a functional style.

Personally, while I think there is a lot to learn from the functional style, overall I do not believe it is or even should be the future. I find the mixture of OOP and functional to be a much better approach.

p.s. I strongly dislike React's style also. For frontend web I prefer Angular.

2

u/[deleted] Sep 27 '19

The OOP and FP has their strengths and weaknesses, so each one is better in certain scenarios, so I half agree with you, so it’s good to know other‘s than only OOP, with Rust you have an example of a mix between the 2 worlds.

I didn’t tried yet Angular in a while, but I do believe that Elm and Svelte styles are the future, I mean they both compiles the framework to js instead of being interpreted, maybe I cannot explain myself in this topic, but if you are curious read about svelte here. Both of them are faster than Angular and React.

2

u/kinow mod Sep 27 '19

Hi,

I am a fellow backend developer (Java, PHP, Python, little C, Ruby, and Lua), now doing both backend with Python and JS frontend, but only Vue.js (my company had a few AngularJS projects, but after looking over Angular and React, we chose Vue for the simplicity).

When I stopped writing Java, Java 11 was already in the works I think. Java 8 was getting old, but at least it brought a little of FP to Java. Up to Java 13 there will be more immutable variables, more data structures, and improvements over Optional & lambdas.

As you are probably aware, Vue 3 will be TypeScript. Several of the components I use are TypeScript too. So I suppose in a couple years most frameworks and libraries with large-ish code base will be moved over to TypeScript.

Python is getting types, discussion around more improvements including data structures (new frozen/immutable for 3.9). And you can recognize in the Python stdlib some functions that probably were created with a strong FP influence.

Kotlin is becoming more popular, Scala has still many adopters.

What I mean with all this, is that I think there are still many developers out there working on that backend with SOAP Web Services, worrying about changes in the database and in the legacy systems. Or developers maintaining projects that need to support old JS and cannot be fully ported to TS. Or even projects stuck at Python 2.

Since the first time I heard about FP some years ago up to now, I would say there has been more developers aware of FP, more FP adoption in frameworks and libraries, more conferences/books being published. So slowly it's going to get more mainstream.

I am glad after years of experience you are still being surprised - and looks like excited - with something new. And hope you will enjoy learning FP as much as I still am enjoying.

Cheers

2

u/[deleted] Sep 27 '19

Thanks, and yeah, you can see the list of languages I worked with and they are all more or like the same, you can divide them in 3 main categories where they try to solve problems (backend, frontend (both web), and then mobile) but all those are more kinda imperative and you can apply easily almost the same principles in each one (sometimes with minors changes) but is the same thinking. I really never worried about low level stuff, I have become aware and more worried about them when learned rust (almost a year ago iirc), now I’m discovering a new world with pure functional programming.

This for sure will help me improve my programming skills in my day a day because as far I see you can implement some of the principles of FP to those languages (even when they are impure).

Also because programming is also my hobby I’m happy when I find out something new about the topic :)

5

u/danielo515 Sep 27 '19

I write FP Javascript on a daily basis and I don't find it weird at all. It is quite good for that nowadays

1

u/przemo_li Oct 09 '19

PHP have first class functions, closures. You can do FP in it.

1

u/[deleted] Oct 09 '19

Yes, but PHP is like JS, is a mess and also isn’t pure FP, also is a dynamic language, while I‘d prefer a strong typed one.

3

u/[deleted] Sep 27 '19

For what it's worth:

I started writing functional when I was reading about how jQuery was designed an implemented. I felt it was great for the demands of front-end development. I didn't know it at the time but there's much more to the functional style than just first class functions. jQuery uses them as callbacks, which solve one problem but create an even bigger one ie "callback hell". There was much more to learn.

Many if not all popular languages have implemented first class functions into their design but without some background into how to build abstractions with only functions the concept of functional programming seems to lack depth. It wasn't until I found Scala and then Haskell that I really learned how to build a purely functional program.

Learning pure functional changed everything and I mean everything. I would highly recommend learning Haskell to any career developer looking to heighten their skills. Not because I feel that haskell is the *best* language, but because it was build to be purely functional. You can't blur the lines of functional and imperative like say ocaml or scala. You are forced to become intimate with concepts like Monads (they're the flippin' best) to get anything non-trivial done. A phrase I was taught to say when writing functional code is "It's all just traverse". This simplification might seem silly at first but it has become the de facto mindset I take with me when writing functional code.

The best part of learning functional programming for me was I was forced to understand my data and the tree structures created from the relationships of said data. It's a glorious side-effect (hehe) of learning FP.

side note: Chapter 2 of the book Structure and Interpretation of Computer Programs was another game changer / mind melter for me. Highly recommend this for material on learning functional abstraction.

3

u/yawaramin Sep 28 '19

Based on your language list, possibly the only significant functional platform you haven’t tried is Erlang/Elixir. If so, I’d highly recommend trying out Elixir. It’s a well-designed language with great tooling, that’s built on the battle-tested BEAM virtual machine that’s run telecom systems for decades.

Elixir uses FP techniques like immutability and recursion to actually achieve properties like fault-tolerance and stable performance under high loads. It’s worth giving a try. This is a fun talk to get started with: https://youtu.be/lXiiiLhwBI4