spizzy
August 29, 2022, 7:28pm
1
So I want to print the alphabet but in this format
aa
ab
ac
… az
I was able to write that in JavaScript like this
const alphabet = "abcdefghijklmnopqrstuvwxyz";
for (const a of alphabet.split('').map(b => `a${b}`)) console.log(a);
what methods do I need in Elixir to get the same results?
1 Like
Sebb
August 29, 2022, 7:38pm
2
'abcdefghijklmnopqrstuvwxyz' |> Enum.map(&"a#{[&1]}")
there is quite a lot going on here:
3 Likes
spizzy
August 29, 2022, 7:40pm
3
whats up with all the {} and [] around {[&1]}
? + Can I simply Enum.join behind the map? like |> Enum.join(" ")
Since op wants to print each pair, I would do it like below. Also your use of the charlist is a bit non-idiomatic imo.
alphabet = ~w(a b c d e f g h i j k l m n o p q r s t u v w x y z)
Enum.each(alphabet, fn letter ->
IO.puts("a#{letter}")
end)
4 Likes
Sebb
August 29, 2022, 7:43pm
5
yes, here some insight with dbg (needs Elixir 1.14)
'abcdefghijklmnopqrstuvwxyz'
|> Enum.map(&"a#{[&1]}")
|> Enum.join(",")
|> dbg
'abcdefghijklmnopqrstuvwxyz' #=> 'abcdefghijklmnopqrstuvwxyz'
|> Enum.map(&"a#{[&1]}") #=> ["aa", "ab", "ac", "ad", ...]
|> Enum.join(",") #=> "aa,ab,ac,ad,..."
2 Likes
spizzy
August 29, 2022, 7:47pm
7
the &"a#{[&1]}"
is a bit confusing but thanks!
Sebb
August 29, 2022, 7:48pm
8
spizzy:
a bit confusing
Its extra-confusing. sorry. Wanted to pack some nice features into that. For a nice solution look at Stefans post.
1 Like
spizzy
August 29, 2022, 7:53pm
9
what exactly is “w” here at the first line?
https://hexdocs.pm/elixir/1.14/Kernel.html#sigil_w/2
It’s just a nicer way of writing ["a", "b", "c", ...]
1 Like
elt547
August 29, 2022, 7:56pm
11
FYI, you can use a range for that instead of a string with "abcdefg......."
etc. The ?
operator gets the codepoint of a character in a char list. From there you can do anything as you would with a list.
?a..?z
2 Likes
Sebb
August 29, 2022, 8:05pm
13
yes.
iex(21)> ?a..?z |> Enum.to_list == 'abcdefghijklmnopqrstuvwxyz'
true
iex(22)> 0..10 |> Enum.to_list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
So this should be the most confusing solution to the problem.
iex()> ?a..?z |> Enum.map_join(",", &"a#{[&1]}")
"aa,ab,ac,ad,ae,af,ag,ah,ai,aj,ak,al,am,an,ao,ap,aq,ar,as,at,au,av,aw,ax,ay,az"
8 Likes
spizzy
August 29, 2022, 8:08pm
15
alphabet = ?a..?z
Enum.each(alphabet, fn letter ->
IO.puts("a#{letter}")
end)
hmm, but when doing this, im getting a weird output
a97
a98
a99
a100
a101
a102
a103
a104
fmn
August 29, 2022, 8:11pm
17
for glyph <- 97..122, do: <<97, glyph, 10>>
2 Likes
elt547
August 29, 2022, 8:11pm
18
It’s because you’re using a binary instead of a char list. Char lists will display all codepoints in the shell if they represent printable characters, but they’re still stored as numbers. When you convert to string with the interpolation, you’re calling the String.Chars protocol. Basically you’re converting the integer to a string.
Sebb
August 29, 2022, 8:13pm
19
that one is nice, but you can do:
for glyph <- ?a..?z, do: <<"a", glyph, "\n">>
2 Likes
Eiji
August 29, 2022, 8:17pm
20
There are many solutions depending on use case, but the one closest to JavaScript
code would be:
alphabet = "abcdefghijklmnopqrstuvwxyz"
alphabet |> String.graphemes() |> Enum.map(& "a" <> &1)
# or alternatively:
alphabet |> String.split("", trim: true) |> Enum.map(& "a" <> &1
Helpful resources:
String.graphemes/1
String.split/3
6 Likes