Hi all,I'd appreciate it if anyone would take a look at this question on Stack Exchange Code Review and share any comments anyone would care to share.For those who don't care to go out and look at the code, here it is:def generate_all_valid_dates_in_range(_start_date, _end_date) when _start_date <= _end_date do(:calendar.date_to_gregorian_days(_start_date) .. :calendar.date_to_gregorian_days(_end_date))|> Enum.to_list|> Enum.map (&(:calendar.gregorian_days_to_date(&1)))end
Mainly I post the question on Code Review to help get exposure for our favorite language.Again, would appreciate any comments if anyone cares to share. Mainly I'm wondering if there's already some built-in OTP function to do this (my Google Fu isn't so very good these days).--Onorio
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
defmodule TimexTest douse ExUnit.Caseuse Timex@moduletag :all@moduletag :unit
def gen(acc, _, _, -1), do: accdef gen(acc, cdt, edt, _) do
Hi,
A couple thoughts, as I'm learning Elixir. Please feel free, anyone, to step in and clear up any misconceptions that I have.
On Thu, Nov 6, 2014 at 3:55 PM, Onorio Catenacci <cate...@gmail.com> wrote:Hi all,I'd appreciate it if anyone would take a look at this question on Stack Exchange Code Review and share any comments anyone would care to share.For those who don't care to go out and look at the code, here it is:def generate_all_valid_dates_in_range(_start_date, _end_date) when _start_date <= _end_date do(:calendar.date_to_gregorian_days(_start_date) .. :calendar.date_to_gregorian_days(_end_date))|> Enum.to_list|> Enum.map (&(:calendar.gregorian_days_to_date(&1)))end- Using a leading _ on names is used to indicate to Elixir that you won't be using those values in your code. You give it a name, so there is some documentation, instead of just a naked _
- Your map can be written as
|> Enum.map (&:calendar.gregorian_days_to_date/1)Just turning the function into something that map consumes, rather than building an anonymous function around it, which is what I think the &1 is going to do.
- It looks like the range operator (..) returns something that Enum.map can consume, so you don't seem to need Enum.to_list.
- Also would be nice to put a catch-all clause that raising an error for invalid inputs.
use timex it is awesome.defmodule TimexTest douse ExUnit.Caseuse Timex@moduletag :all@moduletag :unitdef gen(acc, _, _, cr) when cr == 0 or cr == -1, do: accdef gen(acc, cdt, edt, 1) dondt = Date.shift(cdt, days: 1)gen([cdt | acc], ndt, edt, Date.compare(edt, ndt))endtest "s" do{:ok, s} = TimexParsers.Parser.parse("2014-07-21T00:20:41.196Z", "{ISOz}"){:ok, f} = TimexParsers.Parser.parse("2014-07-29T00:20:41.196Z", "{ISOz}")dates = gen([], s, f, 1)9 = length(dates)IO.puts "\n\n#{inspect(dates)}"endendall the erlang date/time functionality makes me want to vomit.
I have a habit (albeit maybe not the best habit) of starting with all my parameters named with leading underscores so I don't get the unused variable warnings as I'm working through the code. While the snippet I posted above may seem simple and obvious, it was actually the product of four or five failed attempts to figure out how to generate a range of valid dates between a given starting date and ending date. Of course before I move anything into production, I remove the leading underscores because I want the compiler warning if I've forgotten and left in an unneeded variable.
I would advise using proper named variables immediately, ignoring the warnings during the dev cycle, and then once you're happy with the behavior, cleaning up the code by making unused variable anonymous.
I would advise using proper named variables immediately, ignoring the warnings during the dev cycle, and then once you're happy with the behavior, cleaning up the code by making unused variable anonymous.
In fact, with time you may even start using the unused variable warning to detect early bugs in your code: "wait, that varible is not being used? something is wrong…"
Also a good point but maybe not the way you want Elixir code to work. Generally want things to fail fast; if someone calls the function with invalid dates you probably want things to fail right there rather than raising and catching an exception.
I'd appreciate it if anyone would take a look at this question on Stack Exchange Code Review and share any comments anyone would care to share.