Conversation
Notices
-
⚡Eineygður ☯ Flakkari⚡ (toiletpaper@shitposter.world)'s status on Friday, 20-Dec-2024 13:15:54 JST ⚡Eineygður ☯ Flakkari⚡ Elixir has a lot of strengths and elegance, but why do lists and loops have to be such a pain in the ass to implement?!? Most times having multiple layers of nested recursion with all the corresponding gymnastic contortions involving variable scope and indexed element values, etc, is just not the DX I'm aiming for. *sigh* -
⚡Eineygður ☯ Flakkari⚡ (toiletpaper@shitposter.world)'s status on Friday, 20-Dec-2024 13:15:50 JST ⚡Eineygður ☯ Flakkari⚡ @sun @shibao
So, I'm open to critique on this if either of you (or any other Elixers) feel up for it. This is part of the algorithm I mentioned for determining AKS primality which calculates coefficients based on Pascal's triangle. Below is the Elixir code followed by equivalent JavaScript for comparison. The Elixir seems to do what I wanted, but by Gods is it ever ugly and convoluted. FYI, I'm using a map instead of a list in Elixir because it's easier to access map values with their numeric key than list elements by their index.
---[ coef.ex ]---
defmodule X do
def coef(n), do: Enum.reduce(0..(n - 1), %{0 => -1},
fn i, coef -> Map.put(coef, 0, -coef[0]) |> Map.put(i + 1, 1)
|> (& Enum.reduce_while(0..i, { i, &1 }, fn _, {j, c} ->
j == 0 && { :halt, c }
|| { :cont, { j - 1, Map.put(c, j, c[j - 1] - c[j]) } }
end) ).()
end) |> (& Map.put(&1, 0, -&1[0]) ).()
end
[n | _?] = System.argv
Integer.parse(n) |> elem(0) |> X.coef |> IO.inspect
---[ coef.js ]---
function coef(n) {
const c = []
c[0] = 1;
for (let i = 0; i < n; c[0] = -c[0], i++) {
c[1 + i] = 1;
for (let j = i; j > 0; j--) c[j] = c[j - 1] - c[j];
}
return c;
}
console.log(coef(process.argv[2])) -
⚡Eineygður ☯ Flakkari⚡ (toiletpaper@shitposter.world)'s status on Friday, 20-Dec-2024 13:15:51 JST ⚡Eineygður ☯ Flakkari⚡ @shibao @sun
Much appreciated! I might take you up on that another time. For now I'm just gonna give my brain a rest and come back to it later. I'm just implementing an AKS primality test for a programming exercise (to learn), which isn't even all that complicated. My brain's just not cooperating at the moment and I felt like complaining into the ether for sake of catharsis. lol! -
Angry Sun (sun@shitposter.world)'s status on Friday, 20-Dec-2024 13:15:51 JST Angry Sun @toiletpaper @shibao People that use Elixir should be called Elixers -
⚡Eineygður ☯ Flakkari⚡ (toiletpaper@shitposter.world)'s status on Friday, 20-Dec-2024 13:15:52 JST ⚡Eineygður ☯ Flakkari⚡ @sun
*nods* I get it. Mostly I'm just griping out loud because I'm not particularly a fan of callback hell, especially when needing to pass around more than a few variables that share scope at various levels of iteration. At least it has function overloading and guard statements, but right now my brain is breaking and I think it's time to put my feet up and do something else for a while. -
受不了包 (shibao@misskey.bubbletea.dev)'s status on Friday, 20-Dec-2024 13:15:52 JST 受不了包 @toiletpaper@shitposter.world @sun@shitposter.world sounds like there might be a better implementation, i'd be willing to video call with you and figure out something better. imo "pure" functional and "pure" oop should end up looking very similar
-
Angry Sun (sun@shitposter.world)'s status on Friday, 20-Dec-2024 13:15:53 JST Angry Sun @toiletpaper because of immutability. yeah it's a pain but you get used to it -
受不了包 (shibao@misskey.bubbletea.dev)'s status on Friday, 20-Dec-2024 13:16:26 JST 受不了包 @toiletpaper@shitposter.world @sun@shitposter.world using a map is a good choice here! lists in elixir are implemented like linked lists, so you don't want to have to traverse linearly just to get to a certain index. I'm not too familiar with this problem, but the way that pascal's triangle seems to be calculated is relatively straightforward, and i think the missing element here to make it a bit more readable is using one of elixir's features to do it functionally instead of procedural like you were doing before. and since the functions rely on the result of other functions, the idea here is to do it recursively, which isn't the worst because elixir uses tail call recursion so it won't blow out your stack, as long as the last thing a function does is a call to another function. using this, i'm going to try my hand at rewriting this recursively, but you can try as well and we can compare our solutions :D
-