halchester

halchester

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

Most Liked

Sebb

Sebb

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̶̢̪͓͗͐̽  
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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).

John-Goff

John-Goff

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.

Sebb

Sebb

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.

otuv

otuv

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:

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement