Sunday, December 15, 2013

How To: Set up Hotmail in Mutt

I like to think of Microsoft's email infrastructure as a bad bowl of spaghetti. Ever since they came out with outlook, live, and hotmail, doing anything with the server configuration is a pain. When using thunderbird, which configures your mail for you automatically, it's fine, but with a program like mutt things get a bit harder. This is how to set up hotmail and mutt.

The important parts of any mutt configuration file are imap_user, imap_pass, smtp_url, smtp_pass, from, and realname. Here is what that section of the file should look like for you:

 set imap_user = "YOUR_EMAIL@hotmail.com"
 set imap_pass = "YOUR_PASSWORD"
 set smtp_url = "smtp://YOUR_EMAIL@hotmail.com@smtp.live.com:587/"
 set smtp_pass = "YOUR_PASSWORD"
 set from = "YOUR_EMAIL@hotmail.com"
 set realname = "FIRST LAST"
 set ssl_force_tls = yes

Note, smtp_url has two "@" symbols. Without this, sending mail will fail with an SASL error. For the curious, the rest of my configuration file is below:

 # The editor to edit the plaintext of the email in
 set editor = "emacs"

 set folder = "imaps://imap-mail.outlook.com:993"
 set spoolfile = "+INBOX"
 set imap_check_subscribed
 set hostname = hotmail.com
 set mail_check = 25
 set timeout = 300
 set imap_keepalive = 300
 set postponed = "+[hotmail]/Drafts"
 set record = "+[hotmail]/Sent Mail"
 set header_cache=~/.mutt/cache/headers
 set message_cachedir=~/.mutt/cache/bodies
 set certificate_file=~/.mutt/certificates
 set move = no
 set include
 set sort = 'threads'
 set sort_aux = 'reverse-last-date-received'
 set auto_tag = yes
 ignore "Authentication-Results:"
 ignore "DomainKey-Signature:"
 ignore "DKIM-Signature:"
 hdr_order Date From To Cc
 alternative_order text/plain text/html *
 auto_view text/html
 bind editor <Tab> complete-query
 bind editor ^T complete
 bind editor <space> noop

 # Gmail-style keyboard shortcuts
 macro index,pager y "<enter-command>unset trash\n <delete-message>" "Gmail archive message"
 macro index,pager d "<enter-command>set trash=\"imaps://imap.googlemail.com/[GMail]/Bin\"\n <delete-message>" "Gmail delete message"
 macro index,pager gi "<change-folder>=INBOX<enter>" "Go to inbox"
 macro index,pager ga "<change-folder>=[Gmail]/All Mail<enter>" "Go to all mail"
 macro index,pager gs "<change-folder>=[Gmail]/Starred<enter>" "Go to starred messages"
 macro index,pager gd "<change-folder>=[Gmail]/Drafts<enter>" "Go to drafts"

Friday, December 6, 2013

Looping in Haskell

When I first started out with Haskell, some things were made very clear: it was a pure language, it was expressive, and it told what data was, not what it did. However, coming from imperative languages like Java and C, I found myself wanting to know how to loop in Haskell. This was a basic want, because looping is one of the big concepts in programming those languages (along with conditionals and blocking). So, here are some correct ways to "loop" in Haskell, using higher-order functions and recursion.


Tail Call Recursion
Tail Call Recursion is a particularly efficient method of traversing data. The basic structure of a tail-recursive function is:

  1. Check the base-case
  2. Perform some action
  3. Set up the tail call.
The base case is what happens when you no longer want to recurse. If you're moving over a list of Ints, adding 1 to everything, this could be when the list is [] (empty).

In this case, the action would be adding one to a value, and appending it to the next call of the function.

The tail-call would then be the same function, but with the head of the list cut off.

The add-one-to-all numbers function would look something like this:

add-one-to-all :: [Int] -> [Int]
add-one-to-all [] = []
add-one-to-all (x:xs) = 1 + x : add-one-to-all xs

Here, when the list pattern matches [] (an empty list), an empty list is returned. Only when this happens will the function "snap back", collecting the other values from the list.

Higher-Order Functions
The entire point of a higher-order function is to be able to pass functions as parameters to other functions, or to return them from functions. In traversing a list, for instance, you could make a function that takes a function and a list, and then apply that with a function that adds one to a number.

traverse :: (a -> b) -> [a] -> [b]
traverse _ [] = []
traverse f (x:xs) = f x : traverse xs

add-one :: Int -> Int
add-one x = x + 1

add-one-to-all :: [Int] -> [Int]
add-one-to-all x = traverse add-one x

Here, you can see that the function "f" of traverse (type: (a -> b)) is applied recursively to a list. a and b are just type parameters, which are helpful for generic abstractions like this. Then, add-one is defined, and finally mapped over the list in add-one-to-all. The function "traverse" actually already exists in Prelude as the function map.

A bit extra
Haskell does something called currying, which allows the partial application of functions. Discussion on currying is outside the scope of this simple tutorial, but here is a very small implementation of add-one-to-all

add-one-to-all :: [Int] -> [Int]
add-one-to-all = map (+1)

Tuesday, December 3, 2013

Take a Function as a parameter in D

D is an improvement over C++. However, because of the lack of documentation, you need to be studious to use these improvements. One of the features that D introduces is an alias parameter, which we can use to make higher-order functions. If you're familiar with function pointers, these can be used as well, however, there is an alternative in D called delegates.

Without further adieu, here is an implementation of map, a function that takes another function and applies it to an array.

T[] map(alias func, T)(T[] data}{
if(data.length == 1) return func(data[0]);
else return [func(data[0])] ~ map!(func,T)(data[1..$]);
}

The noteworthy chunk of this code is "(alias func, T)". This is a declaration to "capture" an alias and some type when the function is called, and to take and return a dynamic array of type T, whatever type it is. The generic is important in this instance, since dynamic arrays are a container for any type.

Calling higher-order functions in D
To call this function, one would use "map!(name_of_function,type)(list)". Where name_of_function is either the name of a function or a function literal, and type is any type, user-defined or not. An example, done completely with literals:

Calling with literals
map!(((y) {return 10-y;}),int)([1,2,3,4,5,6,7,8,9]) // Returns [9,8,7,6,5,4,3,2,1]

Here, the syntax ((y) {return 10-y;}) is a function literal, called a lambda is some languages.

===tags(ignore)===
higher-order functions in D
D higher-order functions
D lambdas
lambda D

Sunday, December 1, 2013

Is D worth learning?

D has been intriguing to me lately. The language saw a surge in popularity in October of 2013, when Andrei Alexandrescu commit 5112 lines of D to Facebook's repository, and posted about it on the dlang forums. The forum users were ecstatic, going as far of calling this commit the "new age of D". It was hard to ignore that there was something big happening in the D world, so I've taken four days to learn the basics of the language. This is my experience, and my opinion on the practicality and roadmap for D and its future.

Why use D?
D is an attempted improvement over its competitor, C++. C++ was made to stop the C preprocessor hacks that were dividing the language in the same way that the Lisp world was fragmented before Common Lisp. Because of this, C++ became a hodge-podge language, where everything under the sun was thrown in in no particular order to please everyone. D is a re-imagining of C++, with some added features from Java and the functional paradigm.

D is fast: In a trivial program, D executed only .001 seconds slower than C++, while the source code was 30 bytes smaller.

D is coherent: Lambdas, a much-loved feature in C++, were an afterthought. In D, the syntax for lambdas integrates well with the language, and how it handles blocks and values. There are tons of examples of afterthoughts from C++ being first-class citizens of D.

D is compatible: Concepts from C++ carry over well into D. Generics, pointers, structs, classes, etc. It's all there. D can also call C or C++ code with an extern statement, or embed assembly.

D is innovative: Supercompilation, the complicated process that D uses to compile itself, introduces some interesting features. Alias types that refer to any named D object (even functions) can be passed in to other functions as parameters, or instantiated. This, combined with mixins (the ability to turn any string known at compile-time into executable D code) gives the user levels of customization only rivaled by Template Haskell or Lisp.

What drawbacks are there to D?
If you read the last section about D's merits, you may be thinking that it sounds too good to be true. There must be some drawbacks to D, or else everybody would already be using it! Well, it turns out that there are some downsides to the usage of D, but not the language itself.

In the past, the D community was fragmented between two standard libraries. This fragmentation hurt D. In D2.0, the latest revision of the language, massive changes were made to the language, making obsolete the already sparse amount of tutorials on D out there. That leads into the next downer with D.

D has mainly been an enthusiast language. There is little information out there to help if you hit a snag, so you'll often have to turn to human resources to get the help you need. The amount of tutorials is minuscule compared to languages like Java or C.

D is not hard to learn. It's a familiar language, and the book "The D Programming Language" is a wonderful introduction to D, as well as one of the best books about object oriented programming ever written (in my opinion, but this is shared by many, even outside of the D community). However, if your program doesn't compile, and just spits out some obscure dmd error message, you could be out of luck finding another user who has run into your exact problem. With D, you're on the "last frontier" of meaningful documentation.

Due to the previous lack of interest, there is also a lack of third-party support. An emacs mode exists, and so do plugins for popular IDE's, but I found both to be lacking. It took about 200 lines of lisp additions to d-mode.el before I felt D was ready to be used with my editor.

So, should I learn D?
If you are looking to venture into the world of Object Oriented programming, and want to use D as a way of doing that, absolutely! If you are already an object-oriented programmer, and want to expand your horizons, D will give you the power to do just that. If you are a project manager, looking to modularize dying C++ code, or catch the wave of new technology, go ahead.

The only people I would not recommend D to are very new programmers. For me, it took a background of functional programming and Object-oriented design to be able to read the tutorials and books that are out there. There's no "A gentle introduction to programming, using D" style book out there. If you are a new programmer and feel adventurous, you really won't go wrong with D. If you can learn it, you'll utilize it a hell of a lot more than Java as a first language.