thousandsofthem
Trying to write good elixir code.
Input:
data = %{"a" => 0, "b" => 2, "c" => 0, "evilkey" => 666}
for specific keys i need to write some specific output if condition is true
Ruby version:
out = []
out << "text1" if data["a"] > 0
out << "text2" if data["b"] > 0
out << "text3" if data["c"] > 0
out
Ugly elixir version:
out = []
out = if data["a"] > 0 do
out ++ ["text1"]
else
out
end
...
...
out
Good elixir version: ???
Any thoughts here?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 13 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
rvirding
Yes, for many structures you definitely need to know whether you are folding left or right. That is one reason why calling them
foldlandfoldris better than justreduce. The:mapsmodule just hasfoldas there is no defined ordering.NobbZ
I’m aware that we have no left or right in a map but we do have more data structures which in fact do have. And for some operations it is important to know if we are folding from the left or the right.
rvirding
Ah, classic name and not from the functional world. On a map there is no left or right.
NobbZ
It’s called reduce and is a left fold AFAIR
thousandsofthem
Yeah, in my use-case ordering guarantees are required so maps don’t work (but for someone else it could be better approach). also there are other non-related keys as well (which should be filtered out)
plain
[..., ..., ...] |> remove nilsworks just fine - ordering granaries, short and easy to read codervirding
I know people love
|>but you don’t have to use it:There is probably a function equivalent to
foldinEnumbut it is called something else and I could not immediately see it, hence using the one in:maps.NobbZ
We do not have any guarantee of ordering in a map. So in theory the begs pour of this snippet is non deterministic, while in the OPs version we do have an explicit ordering of the keys and their transformation/concatenation into the result.
thousandsofthem
It will, there are re-assignment for
out:so every consequent
outis different variablebenwilson512
Also because nobody has mentioned this yet, the “ugly elixir version” is not actually an elixir version. It would not work at all because
outis immutable.benwilson512