# \#comprehensions

**URL:** https://forum.elixirforum.com/tag/comprehensions/65.md

[Latest](https://forum.elixirforum.com/latest.md) · [Categories](https://forum.elixirforum.com/categories.md) · [Tags](https://forum.elixirforum.com/tags.md)

---

## [List of numbers sometimes displays as garbage (e.g., ~c"\\a\\t\\n\\f\\r")](https://forum.elixirforum.com/t/list-of-numbers-sometimes-displays-as-garbage-e-g-c-a-t-n-f-r/74816)

<div class="topic-metadata">

**Author:** [@ub4](https://forum.elixirforum.com/u/ub4)\
**Replies:** 3\
**Last updated:** [March 29, 2026, 8:03pm UTC](https://forum.elixirforum.com/t/list-of-numbers-sometimes-displays-as-garbage-e-g-c-a-t-n-f-r/74816 "2026-03-29T20:03:03Z")

</div>

The problem with my code is that for x ← list, do: x+5 always returns garbage (e.g., ~c"\\a\\t\\n\\f\\r"). It seems that all other values work correctly. I have tried this using a MacBook Pro M2 in VSCode and iex. I have trie…

---

## [Phoenix Liveview comprehensions and dynamic forms](https://forum.elixirforum.com/t/phoenix-liveview-comprehensions-and-dynamic-forms/63773)

<div class="topic-metadata">

**Author:** [@silviurosu](https://forum.elixirforum.com/u/silviurosu)\
**Replies:** 2\
**Last updated:** [May 25, 2024, 3:47pm UTC](https://forum.elixirforum.com/t/phoenix-liveview-comprehensions-and-dynamic-forms/63773 "2024-05-25T15:47:09Z")

</div>

I am rendering a comprehension inside Liveview template (a list of reviews). For each one when pressing a button I want to display an inline form to reply to that message. Some code snippets simplified to show what …

---

## [Local accumulators for cleaner comprehensions](https://forum.elixirforum.com/t/local-accumulators-for-cleaner-comprehensions/60130)

<div class="topic-metadata">

**Author:** [@josevalim](https://forum.elixirforum.com/u/josevalim)\
**Replies:** 245\
**Last updated:** [December 31, 2023, 7:04am UTC](https://forum.elixirforum.com/t/local-accumulators-for-cleaner-comprehensions/60130 "2023-12-31T07:04:26Z")

</div>

Hi everyone, This is a proposal for introducing local accumulators to Elixir. This is another attempt of solving the comprehension problem, which we first discussed two years ago. This proposal is divided in four parts: …

---

## [LazyFor → \`Kernel.SpecialForms.for/1\` but returning stream](https://forum.elixirforum.com/t/lazyfor-kernel-specialforms-for-1-but-returning-stream/25467)

<div class="topic-metadata">

**Author:** [@mudasobwa](https://forum.elixirforum.com/u/mudasobwa)\
**Replies:** 2\
**Last updated:** [December 7, 2023, 6:39am UTC](https://forum.elixirforum.com/t/lazyfor-kernel-specialforms-for-1-but-returning-stream/25467 "2023-12-07T06:39:09Z")

</div>

The stream-based implementation of Kernel.SpecialForms.for/1. Allows the same syntax as for and acts the same way. This is an initial release. It does not yet support :into , :uniq, and :reduce options and bits…

---

## [Koka-inspired local mutable variables for cleaner comprehensions](https://forum.elixirforum.com/t/koka-inspired-local-mutable-variables-for-cleaner-comprehensions/60093)

<div class="topic-metadata">

**Author:** [@josevalim](https://forum.elixirforum.com/u/josevalim)\
**Replies:** 70\
**Last updated:** [December 4, 2023, 4:47am UTC](https://forum.elixirforum.com/t/koka-inspired-local-mutable-variables-for-cleaner-comprehensions/60093 "2023-12-04T04:47:04Z")

</div>

UPDATE: This proposal has been retracted. Read the new proposal here: Local accumulators for cleaner comprehensions Hi everyone, This is a proposal for introducing local mutable variables to Elixir, inspired by the Kok…

---

## [Creating sublists in a for comprehension](https://forum.elixirforum.com/t/creating-sublists-in-a-for-comprehension/52575)

<div class="topic-metadata">

**Author:** [@jschepmans](https://forum.elixirforum.com/u/jschepmans)\
**Replies:** 16\
**Last updated:** [December 18, 2022, 1:08pm UTC](https://forum.elixirforum.com/t/creating-sublists-in-a-for-comprehension/52575 "2022-12-18T13:08:36Z")

</div>

Hello all, I’m going through the open source beta version of the Dockyard Academy curriculum (GitHub - DockYard-Academy/curriculum · GitHub) and I’m doing some exercises about for comprehensions. In one of these exerci…

---

## [Are comprehensions over ranges optimized by the compiler](https://forum.elixirforum.com/t/are-comprehensions-over-ranges-optimized-by-the-compiler/51299)

<div class="topic-metadata">

**Author:** [@Sebb](https://forum.elixirforum.com/u/Sebb)\
**Replies:** 2\
**Last updated:** [October 25, 2022, 4:12am UTC](https://forum.elixirforum.com/t/are-comprehensions-over-ranges-optimized-by-the-compiler/51299 "2022-10-25T04:12:01Z")

</div>

there is this exercism exercise: Given an input integer N, find all Pythagorean triplets for which a + b + c = N. For example, with N = 1000, there is exactly one Pythagorean triplet for which a + b + c = 1000: {200,…

---

## [How to handle very big enumerables in comprehensions?](https://forum.elixirforum.com/t/how-to-handle-very-big-enumerables-in-comprehensions/51036)

<div class="topic-metadata">

**Author:** [@heves](https://forum.elixirforum.com/u/heves)\
**Replies:** 13\
**Last updated:** [October 16, 2022, 2:48pm UTC](https://forum.elixirforum.com/t/how-to-handle-very-big-enumerables-in-comprehensions/51036 "2022-10-16T14:48:28Z")

</div>

For a school assignment, I need to esentially brute-force check every possible solution for a puzzle. This is acceptable, as the task is meant to teach us about prefiltering and optimizing. I have done both of those as b…

---

## [How to generate permutations from different sets of elements?](https://forum.elixirforum.com/t/how-to-generate-permutations-from-different-sets-of-elements/51003)

<div class="topic-metadata">

**Author:** [@heves](https://forum.elixirforum.com/u/heves)\
**Replies:** 12\
**Last updated:** [October 14, 2022, 2:35pm UTC](https://forum.elixirforum.com/t/how-to-generate-permutations-from-different-sets-of-elements/51003 "2022-10-14T14:35:41Z")

</div>

I’d like to generate a list from a variable length list of lists, choosing one element from each one. Like this: list = \[\[:a, :b\], \[1, 2, 3\], \[{4, 5}, {5, 6}\]\] magic\_function(list) Output: \[:a, 1, {4, 5}\] \[:a, 1, {5, …

---

## [Bitstring generator gives odd result for 6 and 7 bits](https://forum.elixirforum.com/t/bitstring-generator-gives-odd-result-for-6-and-7-bits/50365)

<div class="topic-metadata">

**Author:** [@Sebb](https://forum.elixirforum.com/u/Sebb)\
**Replies:** 3\
**Last updated:** [September 19, 2022, 12:02pm UTC](https://forum.elixirforum.com/t/bitstring-generator-gives-odd-result-for-6-and-7-bits/50365 "2022-09-19T12:02:23Z")

</div>

I understand these results: iex(1)\> for \<\<b::1 \<- \<\<0xff::16\>\> \>\>, do: b \[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1\] iex(2)\> for \<\<b::2 \<- \<\<0xff::16\>\> \>\>, do: b \[0, 0, 0, 0, 3, 3, 3, 3\] iex(3)\> for \<\<b::3 \<- \<\<0xf…

---

## [Enum.map\_join with a comprehension](https://forum.elixirforum.com/t/enum-map-join-with-a-comprehension/48477)

<div class="topic-metadata">

**Author:** [@cblavier](https://forum.elixirforum.com/u/cblavier)\
**Replies:** 7\
**Last updated:** [June 20, 2022, 11:27pm UTC](https://forum.elixirforum.com/t/enum-map-join-with-a-comprehension/48477 "2022-06-20T23:27:24Z")

</div>

Do you think a join option would be a good idea for comprehensions? Something like Enum.map\_join/2 that could map and join lists in a single pass. book\_ids = for book \<- books, join: ",", do: book.id If it looks like …

---

## [Update map in loop](https://forum.elixirforum.com/t/update-map-in-loop/47172)

<div class="topic-metadata">

**Author:** [@experimentix](https://forum.elixirforum.com/u/experimentix)\
**Replies:** 5\
**Last updated:** [April 14, 2022, 11:00am UTC](https://forum.elixirforum.com/t/update-map-in-loop/47172 "2022-04-14T11:00:35Z")

</div>

Hello I’m new to elixir and i’m struggling with this problem I’ve a map lets call it order and that has several products I want to loop through this products map inside the order map and append some info from another …

---

## [Option type compatible with comprehensions in Elixr?](https://forum.elixirforum.com/t/option-type-compatible-with-comprehensions-in-elixr/46077)

<div class="topic-metadata">

**Author:** [@Fl4m3Ph03n1x](https://forum.elixirforum.com/u/Fl4m3Ph03n1x)\
**Replies:** 24\
**Last updated:** [February 24, 2022, 4:53pm UTC](https://forum.elixirforum.com/t/option-type-compatible-with-comprehensions-in-elixr/46077 "2022-02-24T16:53:00Z")

</div>

Background I am trying to up my Functional Programming (FP) skills and one of the things that newcomers first learn in FP is the Option Type (aka, Maybe Monad). Option what? This construct is present in many languages, …

---

## [Can an elixir comprehensions return something other than a List?](https://forum.elixirforum.com/t/can-an-elixir-comprehensions-return-something-other-than-a-list/46059)

<div class="topic-metadata">

**Author:** [@Fl4m3Ph03n1x](https://forum.elixirforum.com/u/Fl4m3Ph03n1x)\
**Replies:** 11\
**Last updated:** [February 15, 2022, 5:10pm UTC](https://forum.elixirforum.com/t/can-an-elixir-comprehensions-return-something-other-than-a-list/46059 "2022-02-15T17:10:06Z")

</div>

Background For the longest time I have been estranged to the magic lands of comprehensions in FP languages. Now I am trying to pick it up. Comprehensions use the for expression in Elixir: https://elixir-lang.org/gett…

---

## [Elixir update a particular field in a list of maps](https://forum.elixirforum.com/t/elixir-update-a-particular-field-in-a-list-of-maps/46018)

<div class="topic-metadata">

**Author:** [@david234](https://forum.elixirforum.com/u/david234)\
**Replies:** 1\
**Last updated:** [February 13, 2022, 9:44pm UTC](https://forum.elixirforum.com/t/elixir-update-a-particular-field-in-a-list-of-maps/46018 "2022-02-13T21:44:30Z")

</div>

I have this list which has order and order\_details. I want to update the quantity field inside order details by subtracting it with the quantity\_kot field. \[ %Od.Orders.Order{ \_\_meta\_\_: #Ecto.Schema.Metadata\<:load…

---

## [For Comprehension: there's no assignment, only filters?](https://forum.elixirforum.com/t/for-comprehension-theres-no-assignment-only-filters/44930)

<div class="topic-metadata">

**Author:** [@csadewa](https://forum.elixirforum.com/u/csadewa)\
**Replies:** 9\
**Last updated:** [December 31, 2021, 7:37pm UTC](https://forum.elixirforum.com/t/for-comprehension-theres-no-assignment-only-filters/44930 "2021-12-31T19:37:16Z")

</div>

Hi all, recently i have been trying out for comprehension after reading Introducing \`for let\` and \`for reduce\` and realizing i am also have severly underutilized for. Just now i have spend quite sometime debugging code w…

---

## [Comprehension syntax](https://forum.elixirforum.com/t/comprehension-syntax/43075)

<div class="topic-metadata">

**Author:** [@Sebb](https://forum.elixirforum.com/u/Sebb)\
**Replies:** 10\
**Last updated:** [October 14, 2021, 3:50pm UTC](https://forum.elixirforum.com/t/comprehension-syntax/43075 "2021-10-14T15:50:55Z")

</div>

I can do: if true, do: :foo, else: :bar and: if true do :foo else :bar end And I can do: for i \<- 0..3, do: {i, i+i}, into: %{} but not: for i \<- 0..3 do {i, i+i} into %{} end :thinking: why is that?

---

## [How to end for comprehension early](https://forum.elixirforum.com/t/how-to-end-for-comprehension-early/31478)

<div class="topic-metadata">

**Author:** [@Jayshua](https://forum.elixirforum.com/u/Jayshua)\
**Replies:** 2\
**Last updated:** [May 13, 2020, 3:26pm UTC](https://forum.elixirforum.com/t/how-to-end-for-comprehension-early/31478 "2020-05-13T15:26:52Z")

</div>

I’m trying to figure out how to use Elixir’s version of for comprehensions. The Phoenix live view generator creates this code in the live/page\_live.ex file to get the search results: for {app, desc, vsn} \<- Applica…

---

## [The ? character in comprehensions](https://forum.elixirforum.com/t/the-character-in-comprehensions/25816)

<div class="topic-metadata">

**Author:** [@tio407](https://forum.elixirforum.com/u/tio407)\
**Replies:** 3\
**Last updated:** [October 4, 2019, 12:04am UTC](https://forum.elixirforum.com/t/the-character-in-comprehensions/25816 "2019-10-04T00:04:42Z")

</div>

I got an answer right on one of my exercises just by guessing but want to understand Elixir. Can you help me understand how this piece of code works defp converter do fn ?A -\> ?U ?C -\> ?G ?T -\> …

---

## [Basics....Swapping content in Liveview when value is changed](https://forum.elixirforum.com/t/basics-swapping-content-in-liveview-when-value-is-changed/25297)

<div class="topic-metadata">

**Author:** [@bishoy301](https://forum.elixirforum.com/u/bishoy301)\
**Replies:** 10\
**Last updated:** [September 15, 2019, 6:26am UTC](https://forum.elixirforum.com/t/basics-swapping-content-in-liveview-when-value-is-changed/25297 "2019-09-15T06:26:35Z")

</div>

Hello everyone, I’m new to Elixir, Phoenix, and Liveview so I’m hoping that this question isn’t too basic. I have a Liveview that currently should show a different set of HTML based on an assign that I update on click: …

---

## [Elixir big comprehension-list optimisation - takes too long](https://forum.elixirforum.com/t/elixir-big-comprehension-list-optimisation-takes-too-long/20908)

<div class="topic-metadata">

**Author:** [@sandeshsoni](https://forum.elixirforum.com/u/sandeshsoni)\
**Replies:** 9\
**Last updated:** [March 21, 2019, 1:01pm UTC](https://forum.elixirforum.com/t/elixir-big-comprehension-list-optimisation-takes-too-long/20908 "2019-03-21T13:01:05Z")

</div>

Need help! need to optimize. My terminal hangs when I run the code. I have a list of 1\_78\_000 words i.e 178k words I need pairs of words, such that when joined together, their length is exactly ten. # this is how I g…

---

## [Comprehensions - how to get Phoenix to render a list of buttons?](https://forum.elixirforum.com/t/comprehensions-how-to-get-phoenix-to-render-a-list-of-buttons/13775)

<div class="topic-metadata">

**Author:** [@Mnowak](https://forum.elixirforum.com/u/Mnowak)\
**Replies:** 10\
**Last updated:** [April 20, 2018, 2:55pm UTC](https://forum.elixirforum.com/t/comprehensions-how-to-get-phoenix-to-render-a-list-of-buttons/13775 "2018-04-20T14:55:15Z")

</div>

I am trying to get my head around comprehensions and enum.each I am trying to get phoenix to render a list of buttons. My data structure is : btn = \[ {"first", "red", "myfunc1()"} {"se…

---

## [Dividing enumerable by corresponding index in another](https://forum.elixirforum.com/t/dividing-enumerable-by-corresponding-index-in-another/9859)

<div class="topic-metadata">

**Author:** [@blksheephw](https://forum.elixirforum.com/u/blksheephw)\
**Replies:** 1\
**Last updated:** [November 5, 2017, 2:23am UTC](https://forum.elixirforum.com/t/dividing-enumerable-by-corresponding-index-in-another/9859 "2017-11-05T02:23:21Z")

</div>

Hi, What would be the recommended way to divide every item in an enumerable, in this case a list, by every item at the corresponding index of another list. Would like to return a new list with the result. ie: list1 = …
