Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a stream and list of dates

Tags:

elixir

I want to create a list of dates with the values from startdate to enddate

This is something similar to How to create a range of dates in R but in Elixir.

Since the list can be huge or sometime infinite (i.e., no end date), I want to also know how to create a stream of dates.

like image 424
shankardevy Avatar asked Jun 20 '15 07:06

shankardevy


1 Answers

 start_date = Calendar.Date.from_erl!({2014,12,27})
 date_stream = Stream.iterate(start_date, &(Calendar.Date.next_day!(&1)))
 Enum.take(date_stream, 10) 

 #=>
[%Calendar.Date{day: 27, month: 12, year: 2014},
 %Calendar.Date{day: 28, month: 12, year: 2014},
 %Calendar.Date{day: 29, month: 12, year: 2014},
 %Calendar.Date{day: 30, month: 12, year: 2014},
 %Calendar.Date{day: 31, month: 12, year: 2014},
 %Calendar.Date{day: 1, month: 1, year: 2015},
 %Calendar.Date{day: 2, month: 1, year: 2015},
 %Calendar.Date{day: 3, month: 1, year: 2015},
 %Calendar.Date{day: 4, month: 1, year: 2015},
 %Calendar.Date{day: 5, month: 1, year: 2015}]

thanks José Valim for pointing at right direction.

like image 192
shankardevy Avatar answered Oct 08 '22 13:10

shankardevy