AstonJ

AstonJ

Most Liked

sashaafm

sashaafm

  def get_available_internal_users do
    user_coll = from(u in User, 
                     select: {u.name, u.role, u.id},
                     where:  u.role != "Client") |> Repo.all

    for {name, role, id} <- user_coll, into: %{}, do: {{name, role}, id}
  end  

Just wrote this function to get all users from my project’s DB which do not have the role of ‘Client’ and put their name, role and id into a map.

Such nice, concise and readable code.

First I was doing a lot of List.first/1 and List.last/1 and Tuple.to_list/1 to get the values into the map. Then it came to me “why not just pattern match the tuple and use the matches to get them into the Map?” and boom… that code came to fruition :slight_smile:

Honestly can’t think of any other language that would allow me to query a SQL DB, parse the data and make it all so readable and short. There isn’t a need to write comments really, it reads by itself.

11
Post #5
AstonJ

AstonJ

Last Post!

AstonJ

AstonJ

A post was merged into an existing topic: LiveView demos thread!

Where Next?

Popular in Discussions Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
restack_oslo
Hello, Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
wmnnd
The Go vs Elixir thread got me thinking: Would it be too hard to implement a simple mechanism for creating Go-style static app binaries f...
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18595 126
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14362 124
New
arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
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

We're in Beta

About us Mission Statement