Erlang Thursday – queue:cons/2

Today’s Erlang Thursday digs a little into the queue module, and we cover queue:cons/2 from the Okasaki API.

queue:cons/2 takes a item and a queue, and will return a new queue with the item at the head of the queue.

queue:cons(7, queue:new()).
% {[],[7]}
queue:cons(3, queue:cons(7, queue:new())).
% {[7],[3]}
queue:cons(nil, queue:new()).
% {[],[nil]}
queue:cons(5, queue:from_list([7, 9, 13, 21])).
% {[21],[5,7,9,13]}

If we try to pass a list in to queue:cons/2, we see that it does want a queue, and will not do an implicit conversion of a list to a queue.

queue:cons(5, [1, 2, 3, 4]).
% ** exception error: bad argument
%      in function  queue:in_r/2
%         called as queue:in_r(5,[1,2,3,4])

As the queue is setup to be a double ended queue, the Okasaki API also provides a counter function queue:snoc/2, that adds an item to the tail of the queue passed in. Note that the argument order is swapped between queue:snoc/2 and queue:cons/2; queue:snoc/2 takes the queue as the first argument, and the item to add at the tail as the second argument.

queue:snoc(queue:new(), 5).
% {[5],[]}
queue:snoc(queue:from_list([7]), 5).
% {[5],[7]}
queue:snoc(queue:snoc(queue:new(), 7), 5).
% {[5],[7]}
queue:snoc(queue:from_list([7, 9, 13, 21]), 5).
% {[5,21],[7,9,13]}

–Proctor

,

Leave a Comment

Giving an Intro to Erlang workshop at LambdaConf 2015

Just a quick post letting anyone who is going to be at LambdaConf 2015 this weekend in Boulder, Colorado.

I will be there giving an Intro to Erlang workshop on this coming Friday, the 22nd of May, so if you are going make sure to look for me around there at least. 😀

Feel free to track me down and say hi, as I would love to meet you as well. The offer is open for you to chat me up about Erlang specifically, functional programming in general, or what ever else we might find interesting.

And if you are going and we meet up, I might just have some Functional Geekery stickers to give away.

Look forward to seeing you there.

–Proctor

, , ,

Leave a Comment

Ruby Tuesday – Array#push

Today’s Ruby Tuesday is on Array#push.

Array#push takes a item and appends that item to an existing Array.

[].push 1
# => [1]
[3, 7, 5].push 1
# => [3, 7, 5, 1]
[3, 7, 5].push []
# => [3, 7, 5, []]
[3, 7, 5].push nil
# => [3, 7, 5, nil]

Array#push also returns itself as the return value, allowing you to chain together calls.

[3, 7, 5].push(2).push(8).push(6)
# => [3, 7, 5, 2, 8, 6]
q = [3, 7, 5]
# => [3, 7, 5]
q.push(2).push(8).push(6)
# => [3, 7, 5, 2, 8, 6]
q
# => [3, 7, 5, 2, 8, 6]

You can also pass more than one argument to Array#push, and it will push each of those items to the Array in turn.

[3, 7, 5].push(2, 8, 6)
# => [3, 7, 5, 2, 8, 6]
[3, 7, 5].push(*[2, 8, 6])
# => [3, 7, 5, 2, 8, 6]

–Proctor

,

Leave a Comment

Erlang Thursday – filelib:is_file/1

Today’s Erlang Thursday is filelib:is_file/1.

filelib:is_file/1 takes a string representing a filename, and returns a true or false depending on if the name refers to a file or directory.

This can be useful if you are having to read from a configuration file and need to ensure that the file or directory exists before trying to process it, so that you can give a nice error message before quitting, instead of just causing a system error to be raised.

filelib:is_file("foo").
% false
filelib:is_file("junk").
% true
filelib:is_file("tmp").
% true
filelib:is_file("tempmp").
% false
filelib:is_file("temp").
% true
filelib:is_file("/usr/local/bin").
% true
filelib:is_file("/usr/local/var").
% true
filelib:is_file("/usr/local/vars").
% false
filelib:is_file(".").
% true
filelib:is_file("..").
% true

filelib:is_file/1 can also take a atom, or even a deeplist(), representing the filename as well.

filelib:is_file(foo).
% false
filelib:is_file(junk).
% true
filelib:is_file(["/usr", ['/local', '/bin']]).
% true

–Proctor

,

Leave a Comment

Ruby Tuesday – File::exist?

Today’s Ruby Tuesday is on File::exist?.

There are times when you want to know if a given file or directory exists; usually this is captured from some kind of configuration file, and you want to make sure the item exists before you want to try to process that path so you can give a nice error. Enter File::exist?.

File::exist? will check to see if the file or directory passed as an argument can be seen by Ruby.

File.exist? "foo"
# => false
File.exist? "junk"
# => true
File.exist? "/usr/local/bin"
# => true
File.exist? "/usr/local/junk"
# => false
File.exist? "."
# => true
File.exist? ".."
# => true

–Proctor

,

Leave a Comment

Erlang Thursday – calendar:iso_week_number/1

Today’s Erlang Thursday is on calendar:iso_week_number/1.

calendar:iso_week_number/1 takes a date tuple as an argument, and returns a tuple of the year and week number. The year is the year passed as the date tuple, and the week number is an integer between 1 and 53.

calendar:iso_week_number({2015, 05, 04}).
{2015,19}
calendar:iso_week_number({2015, 05, 03}).
{2015,18}

If we use this week as an example, we can see that a week starts on Monday (the 4th of May), where the Sunday before (May 3rd) was the previous week.

We see that January 1st falls on the first week of the year, no surprise there, and that the 31st of December for 2015, is on the 53rd week of the year.

calendar:iso_week_number({2015, 1, 1}).
{2015,1}
calendar:iso_week_number({2015, 12, 31}).
{2015,53}

Having a 53rd week of the year sounds surprising at first, because everyone talks about 52 weeks in a year, until you realize that sometimes December 31st sometimes falls at the very beginning of a week, causing it to be the 53rd week, since it is only a partial week.

–Proctor

,

Leave a Comment

Ruby Tuesday – Date#cweek

Today’s Ruby Tuesday is on Date#cweek.

Date#cweek returns a integer value representing the week of the year that the date falls on. The return value is an integer between 1 and 53.

Date.new(2015, 5, 3).cweek
# => 18
Date.new(2015, 5, 4).cweek
# => 19

If we use this week as an example, we can see that a week starts on Monday (the 4th of May), where the Sunday before (May 3rd) was the previous week.

We see that January 1st falls on the first week of the year, no surprise there, and that the 31st of December for 2015, is on the 53rd week of the year.

Date.new(2015, 1, 1).cweek
# => 1
Date.new(2015, 12, 31).cweek
# => 53

Having a 53rd week of the year sounds suprising at first, because everyone talks about 52 weeks in a year, until you realize that sometimes December 31st sometimes falls at the very beginning of a week, causing it to be the 53rd week, since it is only a partial week.

–Proctor

,

Leave a Comment

Erlang Thursday – calendar:is_leap_year/1

Today’s Erlang Thursday is on calendar:is_leap_year/1.

calendar:is_leap_year/1 takes a non-negative integer value representing a year, and will return true if that year is a leap year, or false otherwise.

calendar:is_leap_year(2015).
% false
calendar:is_leap_year(2012).
% true
calendar:is_leap_year(2017).
% false
calendar:is_leap_year(2000).
% true
calendar:is_leap_year(1900).
% false
calendar:is_leap_year(0).
% true

By having a built in function as part of the core Erlang libraries, it means you don’t have to code up the rules, or even go lookup the rules to remember how the century years are determined to be leap years or not.

And if you do pass in a negative number for the year, Erlang will raise an exception, as there are no clauses which match a negative number for the year.

calendar:is_leap_year(-1).
% ** exception error: no function clause matching calendar:is_leap_year(-1) (calendar.erl, line 183)
calendar:is_leap_year(-4).
% ** exception error: no function clause matching calendar:is_leap_year(-4) (calendar.erl, line 183)

–Proctor

,

Leave a Comment

Ruby Tuesday – Date#leap?

Today’s Ruby Tuesday is on Date#leap?.

Date#leap? operates on a Date object, and returns true if the year is a leap year in the Gregorian calender, or false if not.

Date.new(2015, 1, 4).leap?
=> false
Date.new(2012, 1, 1).leap?
# => true
Date.new(2017, 1, 1).leap?
# => false
Date.new(2000, 1, 1).leap?
# => true
Date.new(1900, 1, 1).leap?
# => false

By using Date#leap?, you don’t have to code the rules to check if a year is a leap year, or even have to double check the rules about when the century is a leap year.

There is also a class level version Date::leap?, that will take a integer value for the year, and return a true or false value.

Date.leap? 2015
# => false
Date.leap? 2012
# => true
Date.leap? 2017
# => false
Date.leap? 2000
# => true
Date.leap? 1900
# => false

Both the instance and class level versions of ‘leap?` can handle Year Zero, and negative years.

Date.leap? -1
# => false
Date.leap? -4
# => true
Date.leap? 0
# => true
Date.new(-4, 1, 1).leap?
# => true

I wouldn’t know if that is valid myself, but that is why it is useful to have this a built in method on Date instead of having to code it up myself.

–Proctor

,

Leave a Comment

Erlang Thursday – calendar:valid_date/3

Today’s Erlang Thursday is calendar:valid_date/3.

Originally, I was thinking it was going to be calendar:time_difference/3, but then I looked into the Erlang documentation for the calendar module. and saw that it was marked as obsolete, so today I present calendar:valid_date/3.

The arguments to calendar:valid_date/3 are an integer for the year, integer for the month, and an integer for the day. calendar:valid_date/3 returns the atom true if the day passed in is a valid date, and the atom false if it is not a valid date.

calendar:valid_date(2015, 04, 31).
% false
calendar:valid_date(2015, 04, 30).
% true
calendar:valid_date(2015, 02, 29).
% false
calendar:valid_date(2012, 02, 29).
% true
calendar:valid_date(2015, 11, 31).
% false
calendar:valid_date(2015, 11, 76).
% false
calendar:valid_date(2015, 17, 13).
% false

Just a quick check for our sanity that the day this post was published is a valid date as well.

calendar:valid_date(2015, 04, 23).
% true

Now let’s try to break this a bit and test to see how it can handle 0‘s and negative integer values.

calendar:valid_date(-1, 04, 23).
% false
calendar:valid_date(2015, -7, 21).
% false
calendar:valid_date(2015, 7, -13).
% false
calendar:valid_date(0, 0, 0).
% false

As one might hope, unless you deal with B.C. era dates often, a date with a negative value is not a valid date.

Erlang also provides a calendar:valid_date/1 that takes a tuple of the year, month, and day values as well.

calendar:valid_date({2015, 11, 76}).
% false
calendar:valid_date({2015, 04, 23}).
% true

–Proctor

,

Leave a Comment