Beginner in Elixir, any suggestions?

Hello guys, I’m a beginner in Elixir and I haven’t done any fp by far. I just want to know if there’re any tips and tricks. I’m mainly planning to use Elixir with phoenix for my website servers. If you guys could help me, it’d be very great!

Chester

2 Likes

Hey @halchester, welcome! I’m not sure if this is really a “trick” but my main advice to all Elixir newbies is: develop comfort with Elixir fundamentals before writing a webserver. Phoenix is a very functional approach to webservers, and if you’re used to mutable data you’re gonna have constant problems.

Fortunately, the fundamentals are pretty small. I also think that, unless you’re jumping straight into LiveView or Channels, the fundamentals can exclude OTP or processes in general. You mostly just need to have a good feel for how to work with immutable data, and a decent working knowledge of major standard library modules (Enum, String, etc).

5 Likes

@benwilson512 thanks bro! I’ll make sure I understand those before diving into web frameworks!

First of all fp will not magically solve all your problems. Personally I find functions on structs in fp vs objects with methods in oo as apples and oranges. You still have to keep track of what data you currently have.

Doing that with pattern matching is the first real interesting difference. Once you see the beauty there you will probably be hooked :nerd_face:

A practical advice would be to get a proper recursive function going. Not that you actually will write that many but it really concentrates most of the core concepts into a very dense package. When you can reason with yourself about what you actually do you are well on your way.

Happy coding!

2 Likes

struct be like …

struct ▷ struct ▷ struct ▷ struct

… object be like

object ▷ o̵b̵j̷e̸c̵t̵ ▷ o̴͊͜b̶̭̃j̷̧̐e̷̫͂ć̵͍t̷̹͌ ▷ ō̴̹͙͑b̴̲̯̂̽j̵̻̓̿͝e̸̺̓͂̏ĉ̷̓ͅt̶̢̪͓͗͐̽  
9 Likes

I knew it would upset someone (or many). I still fail to see the conceptual difference.

If you want to know the value of struct.x in step four you have to access the value or look at what transformations have been done.
If you want to know the value of object.x in step four you have to access the value or look at what transformations have been done.

Of cause the object can potentially lie to you about current state but unless you do stuff you should not there is no difference as I can see.

I still prefer the fp way but if someone can enlighten me I happily try to get the full point :smiley:

3 Likes

The point is that the four step pipeline in the functional version will only be affected by those four steps, whereas with the object oriented version it matters what the global context of the function pipeline is, since some, or even all, of those methods in the OOP version access global state. If they aren’t accessing global state, then you’ve written a pure function inside of your OOP, which kinda defeats the point of debating oop VS fp in the first place. That is, in the functional version you can be confident that the same four function pipeline will always return the same result. You can more easily compose things when you only need to know the input and output values, as opposed to the input values, output values and global state in the OOP version. It’s about keeping less stuff in your head at any one given time.

5 Likes

Thank you for a very good answer!

I guess that is why I never really spotted that difference. I have just always stayed away from using global state as it, well, tends to get messy…

The last point about explicit input → output is the one that I really like. I always tend to think of it as here is the stuff, over to you and it does keep the head in a less crowded state :slight_smile:

May I add: Sure you “can do that in {language-with-mutability} also”, but: I don’t know you did when I read your code and I bet you don’t also (except you always program pure, then you should use a language supporting persistent data structures).

Immutability is a (the?) key point in FP. Be aware that you give up a superpower (mutating everything,
everywhere, anytime) and exploit what you get back:

To get started the elixir-track at https://exercism.io/ is great and if you like video courses I can recommend Developing With Elixir and OTP Course | The Pragmatic Studio.

4 Likes

@otuv Yea pattern matching is actually pretty interesting. I think i still have a lot to learn, thanks for the heads up!

hey @halchester, I could say I’m perhapse one step in front of where you could be as I did/am going through a similar journey and here is what I found:

I picked up the Programming Phoenix book so I can get my head around main concepts in Phoenix. I also spent a little time on the Elixir lang website to understand the syntax. Finally I couldn’t resist the urge to write some code and started a Phoenix project. I suggest to avoid a couple of things if you don’t need them: umbrella app and live views and build a good understanding of Ecto as it in itself can take some time to get used to for two reasons: 1) it isn’t like ActiveRecord and 2) learning Ecto and a functional lang at the same time in itself is a challenge.

LiveViews is a shiny new thing that can be very useful but there is still no problem building MVC apps in Phoenix if it helps you get your project up and running.

The next thing I needed to understand was the deployment model for a Phoenix app. This was a hard thing for me to understand because depending on what benefits of Erlang/OTP you prioritise the journey will be different. I ended up going for a basic docker deploy to AWS Lightsail.

Finally I couldn’t stop myself from wanting to get the new shiny thing so I dove into LiveView and am still learning new things about it everyday.

Anyway, the point of all of the above is to say, aim to build something that works and think about what things you still need to learn and prioritise them so you know at what point you need to know what and that will be your own path for how you can get into Elixir/Phoenix.

1 Like

Here are some resources I recommend for beginners:

https://www.manning.com/books/elixir-in-action-second-edition
https://adventofcode.com/
Phoenix LiveView Free Course | The Pragmatic Studio
https://elixircasts.io/

And this forum, of course!

1 Like

Here are some resources I recommend for beginners:

https://www.manning.com/books/elixir-in-action-second-edition
https://adventofcode.com/
Phoenix LiveView Free Course | The Pragmatic Studio
https://elixircasts.io/

And this forum, of course!

You might want to look at exercism.io. They have over 90 exercises for Elixir and you can look at the work of others after you have finished your exercise. You can also ask for feedback/help from instructors if you are having a hard time answering. Good luck!

@armanm @APB9785 @systemplado You guys are really helpful! :smiley: Thanks guys! I will definitely check those out!

1 Like

This is a great course.

1 Like