First a warning, I am not a Ruby programmer, barely ever touched it, I find it a horrid language that mutates all willy-nilly with monkey-patching (to use a python term) running utterly rampant in every ruby project Iāve seen, so following thatā¦
Greetings, first of all this forum uses Discourse as its hosting software, thus it supports github-like markdown, so your code block could appear like this by surrounding it with ``` fences.
def week_trend(days, histogram)
weeks = days/5
week_nr = 1
while week_nr <= weeks - 1
week_trend = histogram[week_nr] - histogram[week_nr - 1]
week_nr += 1
end
end
And since Elixir is immutable the problem will need to be reformed a touch, so letās start.
def week_trend(days, histogram) do
weeks = round(days/5)
week_nr = 1
# snip
end
We are good up until the snip part. Now the āwhileā loop is just iterating from weeks_nr
to weeks-1
, which can be shown in elixir as week_nr .. (weeks-1)
to make a list (technically range) of integers starting with week_nr
and ending with and on weeks-1
. I will assume histogram
is a map. Next, I have no clue what the week_trend
variable is or where it comes from, but letās assume it is a zero and will be returned, however it looks like it is being looped over and thus only the last iteration is being returned, Iām not sure that was intended so for example purposes at least I will assume that it will be adding to it each time, thus it might be best to carry it āforwardā in a calculation via Enum.reduce
. Now reduce
is defined as reduce(enumerable, acc, fun)
, where enumerable
is something that can be iterated over, like a list/range. The acc
is the accumulator, it is passed from call to call to āhold stateā, so we will put the week_trend
into it. And lastly the āfunā is the function that will be called with two arguments, the accumulator from the last iteration or the initial one if the first run, and the value in the enumerable that is being enumerated over at that time, basically this is a standard loop (internally implemented via recursion, which you could do here too but that is not usual and far more wordy and verbose and often harms clarity), so letās make the code now this:
def week_trend(days, histogram) do
weeks = round(days/5)
week_nr = 1
week_trend = 0
Enum.reduce(week_nr .. (weeks-1), week_trend, fn(week, week_trend) ->
# 'loop' body
end)
end
Now in the 'loop' body
section we will alter the week_trend and return it, of which the last iteration will return the result from reduce directly, which since it is in the last place it will be returned from the function too:
def week_trend(days, histogram) do
weeks = round(days/5)
week_nr = 1
week_trend = 0
Enum.reduce(week_nr .. (weeks-1), week_trend, fn(week_nr, week_trend) ->
week_trend + histogram[week_nr] - histogram[week_nr-1]
end)
end
And putting that into a module named Tester
and running it in iex I get:
iex> histogram = Enum.zip(0..30, 0..30) |> Enum.into(%{})
%{0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8,
9 => 9, 10 => 10, 11 => 11, 12 => 12, 13 => 13, 14 => 14, 15 => 15, 16 => 16,
17 => 17, 18 => 18, 19 => 19, 20 => 20, 21 => 21, 22 => 22, 23 => 23,
24 => 24, 25 => 25, 26 => 26, 27 => 27, 28 => 28, 29 => 29, 30 => 30}
iex> Tester.week_trend(30, histogram)
5
So if I read the ruby right (entirely possible that I did not), then the above should be good I hope?