Saturday, November 12, 2016

Installing Racer

Prologue 
Let's talk about Rust for a moment, folks. If C is akin to the Bourgeoise, then Rust is kind of like Robin Hood. It takes ideas from C, and implements them in safe and better ways. In my opinion, it's almost foolish to start a new C project today, with all the extensibility of the High-level languages today and the safety and relative ease-of-use of languages like Rust and Go. One front where Rust is lacking, however, is IDE integration. At the time of writing, there is an Active RFC for features in the Rust compiler that will make it easier for IDE plugin authors to add Rust integration. Thankfully, in the interim, there's the Racer project which does something similar.

What does Racer do?
Racer provides automcompletion for Rust code. It does this by sending incomplete symbols in your Rust source file to racer, searching the standard library for a candidate, and providing the name and type information as you go.

So, in order for Racer to work, we need the Rust source code.

How do I install Racer?
The easiest way is with Cargo, Rust's package manager. The following commands should get you up and running. You need to have cargo, rustc, and git installed for these to work.

**NOTE FOR ZSH USERS**
In the following commands, simply replace .bashrc with .zshrc. Everything else should stay the same.

Install racer from crates.io
cargo install racer

Add the cargo directory to your $PATH variable

echo "export PATH=\"\$PATH:$HOME/.cargo/bin\"" >> .bashrc

Download the rust source code 
git clone --depth 1 https://github.com/rust-lang/rust.git $HOME/.rustsrc

Set your RUST_SRC_PATH environment variable to look for your rust src checkout
echo "export RUST_SRC_PATH=\"$HOME/.rustsrc/src\"" >> .bashrc

Sunday, July 31, 2016

Setting up Emacs for Haskell

Haskell is a bit of a quirky language. It has been around for a very long time, yet hasn't nearly received the professional love that languages like Java, C#, or even F# have gotten. Professional IDE's for the language doesn't exist; good IDE plugins for the languages barely do. So, it has become standard within Haskell community that either Emacs or Vim be used. In this article we'll be examining how to start working with Emacs and Haskell effectively.

First Things First
Prerequisites 
We have to get Emacs and Haskell installed, if they're not already. If you already have emacs and ghc installed, then you can safely skip this step. Otherwise, you need to install the following packages:

emacs (Version 24 or greater)
ghc (Version 7.5 or greater)

Ubuntu
sudo apt-get install ghc emacs
Fedora
sudo dnf install ghc emacs
Arch
sudo pacman -S ghc emacs
Melpa
The next steps are much easier to perform if you already have Emacs set to look at the MELPA repositories. If you'd like instructions on how to set up MELPA, I've written a guide about it Here.

Emacs Packages

The Emacs package which controls Haskell can be found in the "haskell-mode" package. The two other helpful packages that are also in the MELPA repositories are haskell-emacs, which lets you make Emacs extensions in Haskell, and haskell-snippets, which will present a certain type of code completion into Emacs for haskell by the use of yasnippet.

To install the necessary packages, do:

M-x package-install [ret] haskell-mode [ret]
M-x package-install [ret] yasnippet
M-x package-install [ret] haskell-snippets [ret]
haskell-snippets and yasnippet are completely optional, but they will make your experience far better.

Configuration
 A bit of a frustrating trend with haskell-mode is that it's making more and more features opt-in, in order to reduce the memory overhead of using haskell mode. What this means for the average user is inconvenience in configuring haskell-mode.

In your ~/.emacs file (or wherever you prefer to extend Emacs from), put in the following lines:

(add-hook 'haskell-mode-hook 'haskell-indentation-mode)
(add-hook 'haskell-mode'hook 'interactive-haskell-mode)
(add-hook 'haskell-mode-hook 'yas-minor-mode)
Or, if you plan to use all of these features:
(add-hook 'haskell-mode-hook (lambda () (interactive) (haskell-indentation-mode) (interactive-haskell-mode) (yas-minor-mode)))
This makes it so that when you enter haskell-mode (which you do when you open any .hs file), the haskell indentation mode, interactive haskell mode, and the yas minor mode are all enabled.

haskell-mode is what provides syntax highlighting for haskell, amongst a few other features.
haskell-indentation-mode is what allows you to automatically indent haskell source with text
interactive-haskell-mode is what lets you run Haskell code right in Emacs to check for errors, make tests, etc.
yas-minor-mode lets you insert snippets of Haskell code into the file you're in, which makes working with Haskell far easier, due to the immense amount of syntax sugar that Haskell makes use of.

Using Emacs for Haskell
Let's make sure that we have everything working. In Emacs, open up a file called "test.hs" and add the following lines:

myFunction :: (a -> b) -> a -> b
myFunction f a = f a
myFunction2 :: Int -> Int
myFunction2 a = myFunction (+ 2) a
Now, do C-c C-l (hitting c and l while holding control). If everything is installed correctly, it should show you a lambda and a greater-than symbol. This is a ghci prompt, which will evaluate your current file and let you interact with the functions and data types you've defined in this file or any that are imported into it.

Just to see what would happen when there's an error, change the first line to this:
myFunction :: (a -> b) -> a -> c 
And try to run C-c C-l again. Notice that it leaves a white exclamation point to the right of the problem in the source code, and underlines exactly where the error is in red.
  as well as showing you the error in the bottom part of the window
This is how your Emacs workflow will go.

Further Reading
There is plenty more to do with Emacs and Haskell, but this will get you started. You can activate true code completion, get linting messages, etc. through the use of an extra extension called ghc-mod. However, ghc-mod is currently broken in cabal... Whenever the version in the cabal repositories is fixed, I will add information about ghc-mod to the guide.

If your distribution has ghc-mod, it's would betroth you to read about installing it in Emacs here.

How To: Change your desktop background in BSPWM

Howdy! So, you've installed bspwm and you noticed that it's not as fancy as what you're used to. No fancy settings for changing your desktop environment, no 'settings', nothing. We're left to our own devices.

Thankfully, Linux is full of devices for us to use. One of which, feh, will let us change what wallpaper is used by bspwm.

Install feh

The first step is to install feh. Feh is small program used to view images without using a ton of resources. Luckily, feh has been adapted to work as a background setter.

Ubuntu
    sudo apt-get install feh
Fedora
    sudo dnf install feh
Arch
    sudo pacman -S feh

Use Feh
In order to simply use feh as a photo viewer, you can call "feh picture.jpg", where "picture.jpg" is any image file that ImageMagick can open. In order to use feh to create a desktop background, call this command:

feh --bg-center picture.jpg
Once again, where picture.jpg is just about any image file.

And there you have it! A desktop background... for now. The thing is, after this session, the background will revert to normal. In case you want this to remain your desktop background, we need to edit out bspwmrc file.

Configure feh
 In your bspwmrc file, which is usually located in ~/.config/bspwm/bspwmrc, we need to call feh on startup. On mostly any line, preferably towards the end of the file, add this line:
feh --bg-center ~/Pictures/background.jpg
where the filepath is the path to the file you plan to use for your background. Now, when you start bspwm, it should set up the background for you automatically.

Further Configuration

Now, there are further ways that you can configure feh. Right now it's set to center the image in the middle of the desktop, surrounding it with a black border. If you'd prefer to have it stretch, or tile, you can use these flags in place of --bg-center:

--bg-fill : This will preserve the aspect ratio of the file, and optionally cut off whatever part of the image doesn't scale.

--bg-max : this will scale the image to its maximum size, and put black borders on one side.


--bg-scale : Will distort the image to fit exactly within the confines of the desktop. Since this doesn't preserve the aspect ratio, stretching may be a result.

--bg-tile : Will repeat the image until it fits within the monitor.

Advanced Configuration
So, now you know how to get the desktop set up in bspwm. But, bspwm "configurability" is a bit more than your average configurability. We can do some pretty cool stuff with the fact that we're using a shell to set the desktop background.

How about instead of the earlier command, we put this in the configuration file:

feh --bg-center $(find ~/Pictures/Backgrounds -type f | shuf -n 1)
So that it randomly chooses a background out of a "Backgrounds" folder in your Pictures directory?

Anything like this is possible in bspwm. You can have it change based on the time of day, or the type of music playing, or... anything really. The choice is always yours with BSPWM!

How To: Install bspwm

Are you a Gnome or KDE user who is tired of the ordinary? Are you an i3 or herbstluftwm user who has heard the benefits of using bspwm? If you are, you have probably looked into it. And, furthermore, when you looked into it you may have found that it's a bit more involved than you had hoped. No worries! Following this guide should get you up and running with your very own bspwm configuration.

Install the Packages
Installing the packages is, unfortunately, a little but harder than just calling apt-get. We have to compile them. Now, a bit more fortunately, all of the dependencies for bspwm are readily available in your repositories.

Dependencies
On Ubuntu:
sudo apt-get install xcb libxcb-util0-dev libxcb-ewmh-dev libxcb-randr0-dev libxcb-icccm4-dev libxcb-keysyms1-dev libxcb-xinerama0-dev libasound2-dev libxcb-xtest0-dev

On Fedora:
sudo dnf install xcb-util-devel xcb-util-keysyms-devel xcb-util-wm-devel alsa-lib-devel dmenu rxvt-unicode terminus-fonts


Not too bad. What these commands do is install the dependencies that will be needed when we're building bspwm.

Build Tools
Since we're manually building bspwm, we need certain build tools. Libraries, compilers, etc. 
Ubuntu
sudo apt-get install gcc make
Fedora
sudo dnf groupinstall "Development Tools"
Alrighty. So, we have the dependencies and the build tools. Now, we need to download BSPWM.

The Packages

Let's download the two packages using git, now.
cd Downloads
git clone https://github.com/baskerville/bspwm.git
git clone https://github.com/baskerville/sxhkd.git
If all went well, you should have two folders in your 'Downloads' folder - "bspwm" and "sxhkd"

Compilation
The final step to installing the bspwm system itself is compiling it. To do this, go into the Downloads folder and type 

cd bspwm
make
sudo make install

   Now, if you're not used to compiling things, this process probably looks a tad scary. There should be a lot of text scrolling along in the terminal - that is okay. Unless you see "Error, exiting" during this step, everything is working fine.

After this, issue:

cd ..
cd sxhkd
make
sudo make install
If everything went well, we're ready to start doing some of the initial configuration. Awesome!

Configuration

Since bspwm and sxhkd are both installed, let's take a minute to analyze exactly where we are. We've installed bspwm, the window manager, and sxhkd, the keyboard daemon. How bspwm works this:

bspwm manages your windows. Where they're placed, their layout on your desktop, etc. It doesn't handle anything else, unlike traditional window managers.

bspc tells bspwm what to do. When you see bspwm moving windows, closing windows, etc. it's because bspc told bspwm over its socket file.

sxhkd listens to the keyboard for keyboard shortcuts. These keyboard shortcuts can execute any arbitrary command, although they are commonly used to issue bspc commands, such as the command that tells bspc to close a window, etc.

Running - With a display manager
Display managers control your X11 / Wayland (what makes your computer graphical, instead of all commands) sessions, as well as logging in. When you start your computer and see "username" and "password", you're looking at your display manager. Some common display managers on Linux are lightdm (commonly ubuntu), gdm (commonly gnome), lxdm (commonly lxde), and kdm (commonly kde).

bspwm comes with the files necessary to add the window manager to the display manager you already use. In order to use your display manager to log in, you need to copy the "bspwm.desktop" file (from bspwm/contrib/freedesktop). In the Downloads folder:

sudo cp bspwm/contrib/freedesktop/bspwm.desktop /usr/share/xsessions
Now, you can restart your display manager, or choose to reboot. bspwm should be an option in your display manager. You can skip the "Without a display manager" section and move onto configuration.


Running - Without a display manager

Without a display manager, things are a bit more advanced. I'm assuming that if you're choosing to forego using a display manager, that you're a fairly advanced user, so I'll keep it short. Change the contents of your ~/.xinitrc file to say
exec bspwm
And you'll be good to go. But wait! We have to configure the keybindings.

Keybindings and Autostart
Now, you should have bspwm completely installed on your system. However, if you tried to log into bspwm right now you would be greeted with... well, nothing. And, for the moment, that's fine. But the bad part is you won't be able to do anything either. In order to fix this, we need to set ourselves up some keybindings.

Bspwm comes with files with default configurations. Let's get started with the window manager by copying these default keybindings and behavior's over to their respective directories. First, we must create those directories.

mkdir ~/.config/bspwm
mkdir ~/.config/sxhkd

bspwm's configuration:

cp ~/Downloads/bspwm/examples/bspwmrc ~/.config/bspwm/
chmod +x ~/.config/bspwm/bspwmrc
sxhkd's configuration:


cp ~/Downloads/bspwm/examples/sxhkdrc ~/.config/sxhkd
Further configuration
Now, it may be preferable to edit either of these files. The function of bspwmrc in .config/bspwm is essentially an "autostart" file. It lists commands that bspwm should run when it starts up. Now, remember, bspwm is controlled via commands, so rules which govern how bspwm behaves are located in this file.

Things such as how monitors should be handled, window borders, rules for allowing certain windows to float, etc. are all configured here, as well as any programs you want to run when bspwm starts up (like, say, your bluetooth daemon).

sxhkdrc in .config/sxhkd controls keybindings. Keybindings for bspc are common, and important to make note of. By default, some of the most important default commands are:

super + return | urxvt
super + space | dmenu_run
super + escape | [reload sxhkd configuration]
super+alt+Escape | logout of bspwm
super+m | Change the desktop mode

These commands, especially super+return should get you started.
Note: super is the mod4 key on the keyboard, commonly called the "Windows" key as it usually dons the Windows logo
Now, with all of these files in place, we should be ready to start bspwm for the first time. If you don't have urxvt already installed, it's important to issue:

Fedora
sudo dnf install rxvt-unicode
Ubuntu
sudo apt-get install rxvt-unicode
So that you're able to use the terminal when you start using bspwm. The terminal is essential, as it will allow you to modify your sxhkd configuration once you're in bspwm.

If you'd like dmenu (it's an application launcher), issue:

Fedora
sudo dnf install dmenu

Ubuntu
sudo apt-get install suckless-tools
Using bspwm
 Now, start bspwm. If you installed bspwm into your display manager, you can select it from the menu there. If you chose to use .xinitrc, then use "startx" at a tty.

It may seem like it's not working when you start it. It will either show a black screen or the "splash screen" of the display manager when you first log into it.


This is how it looks on Ubuntu

Since we installed urxvt, we can launch the terminal with super+enter, or whatever you changed it to.

By default, it takes up the entire screen like this. But, notice that when you open another program, it will split the total space in half.


This is showcasing bspwm's tiling ability. However, bspwm can do so much more than the default sxhkd and bspwmrc files allow you to do.

Conclusion
Well, there we have it. We now have a working bspwm configuration, and we're ready to get tinkering! Typing "man bspc" at a terminal will show you all of the awesome things that you can do with bspwm, so there's plenty to try out in the new desktop.

This is the beginning of a series of articles about bspwm and sxhkd on this blog. For more, see below, where I'll be adding additional content as it is made:

Modifying keybindings in bspwm : [In Progress]

Happy hacking!

Huge thanks to https://github.com/windelicato/dotfiles/wiki/bspwm-for-dummies by windelicato, where most of this guide was sourced from. I paraphrased much of this guide from his original writeup here for the purpose of clearing up the process, and fixing one mistake he made in the original guide.

What is a Monad? What is a Higher Kinded type?

If you're stumbling upon this document, I'm guessing that you're just getting into a statically typed programming language and are (rightfully) confused about what all the kerfuffle regarding Monads and Higher-Kinded types are. This blog article will clear all the confusion you have about what monads and higher kinded types really are, but how to use higher kinded types (in, say, Haskell) is not covered here.

Background - Static Types
Before we can really talk about what a monad is, let's take a step back. In terms of programming, monads only exist in programming langauges which support strong static typing. That means, that the type system of the programming language follows a mathematical type system. See: Wikipedia.  The reason that this is so neat is that Type Theory is the basis of all of mathematics. Every other branch, including Calculus, Algebra, Trigonometry, Statistics, etc. can all be defined in terms of Type Theory operations.

In programming languages, types denote the type of data you're working with. Numbers may be classified as "int" or "double" in a lot of languages, which are different types which represent numbers. Text may be of type "string", etc. Languages that have "strong" static types take this a step further, being a direct representation of their type-theory counterparts.

Background - Type Theory
Types in type theory work as such: there are two variations of information. 'Types' and 'Kinds'. Kinds are, in a way, the father of types.

The kind, denoted "*", is literally pronounced "type". It is used to describe any type that doesn't take a type argument (more on this later). This can include things like functions, ints, doubles, strings... pretty much every type that you're experienced with, outside of generics.

The kind * -> * is where things begin to be interesting. This is about as far as Haskell and many other strongly-typed languages truly go (with the exception of kind (* -> *) -> *, the higher-order kind). This is the kind of any type that takes one type parameter... a little unclear? Read on.

Higher Kinded Types and Monads
* -> * you can think of as a "template template". Think of, in Java, this class:

class myClass<T>{
  T myItem;
}

In this case, the type "myClass" is the first * and the type "T" is the second *. In Haskell, which is strongly statically typed, a data type declaration, such as

data List a = Node a | Rest (List a)

In this case here, List a has the kind "* -> *" because it takes one type parameter (a). 

The important thing to realize about this, is that kinds of this type can be generecized within the type declarations of other functions. As an example of exactly what that means:

myFunction :: l a -> [a]

would take any * -> * that takes 'a' as a type paramter, and returns an array of that type from it.

Where do Monads fit into the equation?
Monads are a concept from category theory. There, monads are an endofunctor paired with two natural operations. The details of this are interesting, but outside the scope of this quick run-down of HKT's and monads. Check out Category Theory if you'd like to know more.

The function declaration of monad's function is as such:

Monad m => m a -> (a -> m b) -> m b

So, as you can see, Monads are only possible with the use of Higher-Kinded types ((m a) and (m b), make the kind * -> *). Monads also define particular functions which follow the 'monadic laws', however, these are unrelated to their basis in the type system.

Conclusion
Type theory is a gigantic branch of mathematics, and this quick run-down of how types work in statically typed languages does not even closely begin to scratch the surface. Hopefully you've taken away a bit more about what it means to be "higher kinded", and a bit how monads use the type system to exist.

If you'd like to learn more or correct me on something I said wrong up above, by all means feel free to comment!


Thursday, July 28, 2016

Comparison of Java IDEs

Why use an IDE?
If you're writing Java programs, you are without a doubt using a Java IDE of some sort. The way that Java runs, that is, using managed bytecode on a virtual machine means that the compilation step is a bit more rigorous than a call to gcc as you would do for a C source file. In any given Java program, you go through so many steps to compile and run the bytecote that it's nearly nonsensical to use the language without an IDE.

Additionally, the language's complex foundation (meaning, it's extensive standard library, OO model, and verbose nature) make it even more perfect of a fit for a language to use along with an IDE. So, you can't be too informed about what IDE is good for you.

Personal IDE Ranking
Quickly, here are the IDE's that I've used and most like for Java development:

#1 - IntelliJ
Download

#2 - Eclipse
Download

#3 - Netbeans
Download

These are the basic Java IDE's. For a more in-depth look, check out this list here.

Strengths and Weaknesses

IntelliJ
IntelliJ, Jetbrain's Java IDE, has become the de-facto Java IDE in the industry. As a matter of fact, many of Jetbrain's products are used all across the software industry (most famously, their Resharper plugin for the Visual Studio ecosystem).

Its strength lies in a very strong out-of-the box experience with heaps of features missing from the other two popular Java IDE's. Some of these features include:

Out-of-the-box experimental technologies

With Netbeans or Eclipse, you can feasibly use technologies such as groovy and maven. With Eclipse, you can integrate Scala, Clojure, kotlin, etc. into the IDE with the use of addons. However, IntelliJ IDE comes with support for these supplemental JVM languages out of the box. Support includes first-party support, syntax highlighting, code completion, etc. They are first class members of the IntelliJ ecosystem.

Unbelievable code completion and Linting

IntelliJ's code completion is good. Creepily good. It will never fail to resolve the name of some class's methods, if it's your class or some other library's class. It supports sensible completion for the newest of Java features, such as generating lambdas.

Speaking of lambdas, IntelliJ's linting process will often tell you to replace lines of code. It will automatically transform uncessary loops into lambda calls to tidy up your source, or report sources of possible error as you write them.

Stellar Android support
Android Studio, the de-facto IDE for creating Android programs at the moment, is based on IntelliJ. So, in order to install Android Studio, you need only install one plugin for this IDE, retaining all of your other settings you have within it.

The Android plugin includes a drag-and-drop GUI designer, cached emulation for fast in-pc testing of android apps, and the same great code completion that it has for all java source files, which is even more important for Android.

Unlike some other IDE's which support Android, IntelliJ's XML editing leaves almost nothing to be desired.

Next-level debugging
To emphasize just how good the built-in debugger in IntelliJ is, understand that I am no stranger to debugging, inside and outside of IDE's. I've been mucking around with debuggers since it meant crawling through assembly outputs to identify your issue. In that time, I have never come across an IDE as featureful and powerful as IntelliJ's. The debugger is my personal favorite feature of this IDE.

Breakpoints support more feature's than a lot of other IDE's.
You can choose to sleep  just the thread where the breakpoint is set, or the entire thing. You can choose to not even suspend execution on your breakpoints. And, most importantly, you can set a condition for the breakpoint to break on. Even cooler is the fact that the condition line supports code completion.

The debugging process itself is just as featureful as the breakpoints are. It supports all the standard features that a debugger should have, such as "Step Into", "Step out", "Step Over", etc. But it also has some advanced features like "Drop Frame" (for recursive functions), and "Force Step" for situations resistant to debugging... which, surpirsingly, do come up from time to time for some reason or another.

As you debug, the values of local variables are displayed alongside of your code as gray text.

As is the case with many IDE's, IntelliJ supports evaluating expressions at any point during the debugging process.

Integrated VCS management
IntelliJ has top-notch VCS integration. You can pull changes, push changes, edit commit logs, and even apply patches from diff to your project directly.

Other
A feature that, for me, sums up the type of IDE IntelliJ is is "Create gist"
When I saw that the context menu had an option to paste to gist, I was excited. Partly because it makes it easier to ask for help via IRC, but mostly because it shows the type of IDE that IntelliJ is. It is truly the frontrunner in keeping up with the latest in the software industry.

Weaknesses
As biased as I may be for this IDE, I truly feel that IntelliJ doesn't have anything wrong with it as far as the software itself is concerned. I've used this IDE for a bit over one year, at one point for a pretty major project (hundreds of thousands of lines of source, 100+ files). In that year, it has never crashed for me, or behaved in a way that it would not allow me to accomplish my work.

The IDE itself, or some addon, always had whatever feature I was looking for in the Java IDE.

The only problem with the product, in my opinion, is the fact that the non-community edition (which has features that do make it worth it) costs quite a bit of money, and the subscription doesn't particularly make sense. In addition, Jetbrains products are not Open Source or Free, so you can't modify them if you do need to integrate deeply with the IDE, fix some bug, or just see how it's running.

Eclipse
Eclipse, for many years, was considered the "professional" Java development environment. Interestingly, Eclipse is as much a plugin framework as it is a desktop environment. Behind Firefox, it is the second most extensible desktop application ever created. Quite literally, "eclipse" is just a plugin framework that has a lot of good Java editing addons pre-installed. Or C++ editing addons. Or brainfuck editing addons. Or, really, anything you can conceive.


Extensibility
Extensibility within Eclipse is no joke. It gives you the tools to fine-tune everything about the IDE. You can transform the IDE to do anything you want to. It has very good "official" addons for it, such as Android, C++, and Java. On top of that, there are tens of thousands of community plugins, which can turn Eclipse from a Java development environment to, say, a Lisp environment, a Ruby environment, a D environment, or a development environment for just about any language imaginable.

It's rather easy to create a plugin for Eclipse. You do so with plain Java and XML, and you're able to install plugins that you make yourself to fine tune anything about Eclipse that you would like.

Featureful Project management
Eclipse, would like to make sure that it has every possible use-case covered. The user should barely have to do anything with setting their project up manually. This includes things like inheritance and interface implementation. Take a look at the "New Class" dialog box:

This makes it very simple for somebody new to Java to generate a ton of code. Or, if you're extending a large class or implementing a large interface, it streamlines the experience by quite a bit.

Other
Like IntelliJ, Eclipse supports code completion very well (not quite as well as IntelliJ, mind you). It supports debugging, but, once again, not as well as IntelliJ can debug.

However, one key advantage that Eclipse has over IntelliJ is the fact that it is Open Source and completely free to use. You can edit it, or add one plugin to the tens of thousands that already exist. 

If you're a programming multiglot, you should seriously consider Eclipse, since it could become your environemnt for all of your programming languages if you choose for it too. However, if you're looking to purely write Java programs, IntelliJ may be the better option.

Weaknesses
Eclipse has quite a few weaknesses. As it is so extensible, you will often get the feeling that the IDE is "held together with duct tape", as I heard my professor say about Eclipse. 

It uses something called "Perspectives" to display information to the user. These perspectives are essentially windows, and Eclipse is the window manager. That leads to odd behavior like this: 
where you may have to resize your IDE to fit the entire Eclipse window, since the IDE features are themselves plugins. This is a good display of just how truly extensible Eclipse is, however, it's a bit cumbersome to work with if all you're looking to do is edit some Java.

The fact that Eclipse is so extensible also makes the memory usage quite high, even by Java program standards. This is not a problem on most systems, but on slower computer the overhead of being so extensible can really slow things down.

Unlike IntelliJ, there is no baked-in support for version control. However, there are many git / subversion plugins available in the Eclipse repositories to fit these needs.


Netbeans

Netbeans is another traditional Java IDE, like IntelliJ. It's been around for years, and is the default Java IDE (meaning, that Oracle develops it and bundles it with their Java downloads). Despite Oracle having control over Netbeans, the IDE is completely open source.

And, although it is most comparable to IntelliJ, it shares some similarities with Eclipse, including its numerous addons. However, most of the features that you would end up using Netbeans for are pre-installed.

Simplicity
Something can be said about Netbeans that cannot be said about Eclipse. It's dead-simple. It's a traditional editor, reminiscent of code::blocks and the like. Its RAM usage tends to be quite low, and the learning curve for Netbeans is high, making it a great instructional or introductory IDE.

When you create a Java project, it will create a default package and the main class automatically. Unlike IntelliJ and Eclipse, you are ready to execute your java program the second you create it.

Enterprise Support
When you think of Oracle, you think "Enterprise", and Netbeans has features that back this up. It has features, such as allowing you to automatically Globalize your applications with international strings. It allows you to do most things you may need to do in Java through the IDE in pre-defined ways as well. Inheritance, encapsulation, code formatting, comment generation, etc. 

Code Completion
Netbeans has powerful code completion, but it falls short of both Eclipse and IntelliJ. It can automatically fix issues from the context menu, but it won't provide this option as well as Eclipse or IntelliJ does, and it will mostly be able to fix warnings and errors only; it won't improve already working code like IntelliJ. 

The downside with the code completion is that many times it incorrectly assumes what you want. If you put the line import javax.swing.UIManaer; where you actually want javax.swing.UIManager, it will suggest that you create a new local package called javax.swing.UIManaer and write the class yourself.

VCS support
Netbeans has support for all of the major Version Control systems out of the box, including mercurial, git, and subversion. This is one clear advantage it has got over Eclipse, however, the VCS integration doesn't offer all of the features that IntelliJ's does.

Multi-Project support
Netbeans has my favorite multi-project support out of any IDE. It lets you quickly zip back and forth between various projects from the project browser. 
Weaknesses
Netbeans is, in my opinion, the weakest of all the 3 major Java IDE's. It will allow a beginner to get started in Java, but it lacks  so many features that Eclipse and IntelliJ have that it's not worth trying to work around it. The linting is non-existant. The code completion doesn't work 100% of the time. The interface is surprisingly error-prone in contrast with Eclipse, which, since everything is a perspective, you may imagine that it would have a lot of errors.

I stopped using Eclipse a few versions ago, but at one point it crashed and refused to open the project I was working on again. I recall this happening twice during one semester of a java course, which means that it may be common with the IDE. At the time, we were just working with classes for educational purposes. That is, no external libraries or unit tests, which may increase the chances of it crashing.

Other Java IDE's
The only other Java IDE that I have experience with is BlueJ, which I have nothing good to say about. The interface is built around the idea that a problem that literally does not exist should be solved (that using user-defined classes is too difficult). It provides syntax highlighting and rudimentary code completion / debugging / unit test generation, but that is literally all. It isn't worth it to look into BlueJ unless you're going to do so for the novelty of the experience.

Comparison
Here, I will list the scores for some goals you may have for your Java IDE.

Extensibility
Eclipse - 10
IntelliJ - 8
Netbeans - 5

Stability
IntelliJ - 9
Eclipse - 7
Netbeans - 4


Code Completion
IntelliJ - 8
Eclipse - 6
Netbeans - 5

Code Generation
Eclipse - 9
IntelliJ - 8
Netbeans - 4

Debugging
IntelliJ - 10
Eclipse - 5
Netbeans - 3

Unit Tests
IntelliJ - 8
Eclipse - 6
Netbeans - 4

Version Control
IntelliJ - 8
Netbeans - 7
Eclipse - 2 (must be used via addon)

Open Source
Eclipse - 10
Netbeans - 5
IntelliJ - 1

Android
IntelliJ - 10
Eclipse - 8
Netbeans - 4

HTML, CSS, Javascript
IntelliJ - 9
Eclipse - 9
Netbeans - 3

Kotlin, Scala, Clojure
IntelliJ - 10
Eclipse - 9
Netbeans - 0

Learning Curve
Netbeans - 8
IntelliJ - 7
Eclipse - 5

Totals
IntelliJ: 96 / 215
Eclipse: 67 / 215
Netbeans: 52 / 215

You should use IntelliJ if...
You just straight-up want to edit Java source. You don't need insane amounts of extensibility or the ease and official support of using Netbeans. Additionally, if you're going to be working with Android, you want IntelliJ.

You should use Eclipse if...
You need an open-source IDE that is as extensible as it is deep. If your project using many languages and technologies, Eclipse may also be the best bet for you.

You should use Netbeans if...
You are just starting out in the world of Java, or, for some reason, you're not comfortable with IntelliJ or Eclipse. If you are an instructor, you may also want to use Netbeans for your students.

You should use BlueJ if...
You have a mental defect, but don't want it to get in the way of your programming fun.

Hopefully this article helped you make your choice. Good luck!


Friday, July 1, 2016

How to Fix: No Audio with a bluetooth headset in Linux

Hopefully I can help some of your with this problem that I had a few months ago. Here was the scenario: everything seemingly worked fine with my bluetooth headset. It connected, played music flawlessly and everything on first connect... but as soon as I rebooted the headphones, nothing.

They still connected as a media device. The volume control on the computer still worked. But no sound at all would transfer between my computer and the headphones. Weird.

First Things First
Let's make sure they're configured in the first place. Consult your manual on how to sync your device. Then, from your desktop environment's settings (if you're not using a Desktop Environment, check out blueman), click "Bluetooth"

It should say "Connected". This is a sign that the bluetooth device is properly connecting to your Linux box, but not that audio is properly being transmitted. 

A Lesson in Linux Audio
Linux Audio is notirously painful to configure. There are 3 sound systems that distributions tend to use. These are Alsa, PulseAudio, and OSS, which really isn't used all that much.

How To fix
Okay, so now you have to install a package called pavucontrol, which is short for Pulse Audio Volume Control. It's not installed by default on most Linux distributions because it provides some more advances facilities for managing your audio. Once it's installed, run "pavucontrol" from a terminal to open it.

First, make sure that the audio isn't muted on your headset here (either on the bar or the speaker mute button). You should have your channels locked together, and it should be a fallback. Now, if all of this is set up (it was for me), there is one last step you need to do to fix your audio.

In the "configuration" tab at the top, go down to your headset. As misleading as it is, change your headset from either "Off" or "Headset head unit" to "High Fidelity Playback (A2DP Sink)"
Now, after changing that, you should be good to go. If you're not, there is one last thing you can try.

Install alsamixer if you don't already have it and run it in a terminal. If you see "MM" at the bottom, hit "M" to unmute that channel, and use the up arrow to turn the volume of it all the way to 100%. If it's possibly reading your sound card wrong, you can use "F6" here to change that.

Good luck. Audio issues are a true pain to work through, and are seemingly impossible to fix. Hopefully these steps solve your audio problems as they did mine.

Installing Minecraft in Fedora 23 and 24

Minecraft on Fedora can be a bit of a pain. You may even have experience working with .jar files, download Minecraft, and it still doesn't work! This post will explain why that is, and how to get the game installed on your computer. You'll be minin' and craftin' before you know it.

Installing Minecraft is as easy as these steps:


  • Download Minecraft
  • Install Java
  • Run
and has one optional step that I highly recommend

  • Create a Desktop file

I've tried to put together a small script to do this for you. If it doesn't work, then follow along with the steps below and tell me which part is giving you trouble:

x86: wget "http://s3.amazonaws.com/Minecraft.Download/launcher/Minecraft.jar" -O ~/Downloads/Minecraft.jar; wget "http://download.oracle.com/otn-pub/java/jdk/8u91-b14/jre-8u91-linux-i586.rpm" -O ~/Downloads/jre.rpm; sudo dnf install ~/Downloads/jre.rpm; wget "https://raw.githubusercontent.com/natepisarski/tutpi-sources/master/install-minecraft-fedora-23-24/Minecraft.desktop" -O ~/Downloads/Minecraft.desktop; sudo cp ~/Downloads/Minecraft.desktop /usr/share/applications/;wget "https://github.com/natepisarski/tutpi-sources/blob/master/install-minecraft-fedora-23-24/minecraft.png" -O ~/Downloads/minecraft.png

x86_64: wget "http://s3.amazonaws.com/Minecraft.Download/launcher/Minecraft.jar" -O ~/Downloads/Minecraft.jar; wget "http://download.oracle.com/otn-pub/java/jdk/8u91-b14/jre-8u91-linux-x64.rpm" -O ~/Downloads/jre.rpm; sudo dnf install ~/Downloads/jre.rpm; wget "https://raw.githubusercontent.com/natepisarski/tutpi-sources/master/install-minecraft-fedora-23-24/Minecraft.desktop" -O ~/Downloads/Minecraft.desktop; sudo cp ~/Downloads/Minecraft.desktop /usr/share/applications/; wget "https://github.com/natepisarski/tutpi-sources/blob/master/install-minecraft-fedora-23-24/minecraft.png" -O ~/Downloads/minecraft.png

After using these scripts, you should be able to play Minecraft by typing:

cd /usr/java/jre*; bin/java -jar ~/Downloads/Minecraft.jar 

at the terminal, or by rebooting and selecting "Minecraft" from your applications menu.

Download Minecraft
This is probably the easiest step. Assuming you already have a Minecraft account, you can just download the game to get started with this command:

wget "http://s3.amazonaws.com/Minecraft.Download/launcher/Minecraft.jar" -O ~/Downloads/Minecraft.jar
This will put Minecraft inside of your "Downloads" folder in your home directory.


Download and Install Java
Thankfully, because you run Fedora, Java provides a .rpm file for their JRE. This makes the installation far easier.

Also note that we must install Oracle's propriety JRE in order to run Minecraft, as there is (at least at the time of writing) an incompatbility between swing on OpenJDK and the official JDK.

x86 (32-bit) system users can download their rpm from this link.
x86_64 (64-bit) system users can download their rpm from this link.

By downloading either file you are accepting Oracle's license. If those links don't work or are outdated, visit the website directly here: http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html

Find where Java was Installed and Run Minecraft
The .rpm will automatically install Java. But... where. If you tried to do "java -jar Minecraft.jar" right now it would fail still, because it's still using the OpenJDK version of java. In order to launch Minecraft, you have to do this:

cd /usr/java/jre*; bin/java -jar ~/Downloads/Minecraft.jar 

because Java is installed in /usr/java/ in a directory name that corresponds to its version, which is bound to change. If you're comfortable with leaving it at this, you can. However, having to use that command every time you want to play Minecraft is a bit cumbersome, so we're going to make a .desktop file to do it.

Make the Desktop File
.desktop files are what control launching desktop applications on Linux. When you see "Files" and a picture of a filing cabinet that launches nautilus, for instance, that's the result of a Desktop File. We can make our own desktop file to launch Minecraft with that code above. One caveat is that Desktop files aren't run like normal terminal commands, so we have to give our command up there a shell, since it has it change its working directory.

The content of the Minecraft.desktop file should be as follows: 

#!/usr/bin/env xdg-open

[Desktop Entry]
Version=1.0
Terminal=false
Type=Application
Name=Minecraft
Exec=bash -c "cd /usr/java/jre*; bin/java -jar ~/Downloads/Minecraft.jar"
Icon=~/Downloads/minecraft.png
NoDisplay=true

Name[en_US]=Minecraft

which you can write in almost any text editor. This expects there to be an icon for Minecraft in your Downloads folder, but it will launch the game even if there's not.

Great! Now...

Install the Desktop File
Move the file into /usr/share/Applications/. This will install the Minecraft launcher system-wide. It may take a reboot for this to actually work.

And you're ready to roll! Have fun

How To: Set up timestamps in Emacs Documents

This will be a pretty straightforward how-to guide on how to implement timestamping in Emacs. This will insert the time directly into the document, under the cursor, so if you want something fancier than unfortunately the GNU Emacs Documentation will have to hold you over.

The Command
The command to actually insert the time into the document is this:

(insert (current-time-string))

The Keybind
Now, if it doesn't exactly sound like your cup of tea to have to copy-and-paste that Lisp command every time you want to insert the timestamp, open up your .emacs file (or wherever you store your Emacs configuration file. If you don't have one, you can just create a file called ".emacs" in your home directory).

Insert this line:

(global-set-key (kbd "C-c C-c") (lambda () (interactive) (insert (current-time-string)))

This tells Emacs that whenever you press Control-C, Control-C, it should insert the default time string (which looks like: 

Fri Jul  1 10:57:35 2016

Note the (lambda (interactive()). This is only required because global-set-key expects interactive commands.

Modifying the Time
Obviously, having a string that long doesn't work for everybody. Maybe you don't want the day of the week to show, or you only care about the year, etc. Luckily, Emacs comes with a command called format-time-string which allows us to control what kind of format the date will be output as. So, let's change up that (current-time-string)!

(global-set-key (kbd "C-c C-c") (lambda () (interactive) (insert (format-time-string "%I:%M:%S%p"))))

This will give you just the 12-hour time (i.e "10:57:35pm"). To see a list of all the options you can use with format-time-string, either run man strftime (on linux) or go to The GNU Documentation on the function.

Only in Certain Modes
Now, in my case, I only want time-stamping to be enabled when I'm in Markdown Mode. The code (still using the 12-hour time format) would be this:

(add-hook 'markdown-mode-hook (global-set-key (kbd "C-c C-c") (lambda () (interactive) (insert (format-time-string "%I:%M:%S%p")))))

There are a ton of hooks out of the box, and even more in the thousands of MELPA packages, so you can modify 'markdown-mode-hook to be anything.

Thursday, June 30, 2016

Deploying a ASP.NET MVC Application on Mono

Ever tried to deploy an ASP.NET MVC application before? Well, it's the bee's knees... on Windows, where most Visual Studio project types are first class citizens of the ecosystem (sorry, F#). However, things get a tad trickier on Linux / Mono, so let's run through the very basics of setting up an environment like this.

This post will not cover advanced topics, such as setting up an SSL certificate or linking with a database. It will just get the barebones ASP.NET server deployed. Note, however, that there are better ways to deploy an ASP.NET MVC service than what this post describes (through fastcgi or mod_mono), so if you're concerned about the RIGHT way of doing things rather than the FAST way of doing things, follow that link.

Structure - Skip if you have a project
Let's make ourselves an ASP.NET MVC application in Monodevelop. If you don't already have monodevelop, you'll need to install the following packages:

  • monodevelop
  • xsp
  • mono-devel
from your distribution's repository. If some of those aren't found, it may be okay, as both tend to be bundled with monodevelop itself. Now, make an ASP.NET MVC project with Razor



this should create an ASP.NET MVC project that's ready for deploying.

Compiling
Now, it's time to compile the project. This is where a program from mono-devel comes into play. xbuild, an implementation of msbuild (link)  is what will generate what the web server runs. The binaries are generated from the .sln or .csproj file itself.

So, open up a terminal and go to the project root, where your project file is.
Then issue the "xbuild" command on your file. This will build all targets for the project, which are Debug and Release by default in Monodevelop

xbuild {file.sln}

Wonderful! If everything in your project passes, you have successfully built your ASP.NET MVC application

Deploying
Now, here are where the tough decisions are made. If you are truly deploying a production service, you should do the following things:

  • Secure a webserver, preferably a dedicated virtual machine
  • Generate SSL certificates / other cryptographic keys
  • Create a user for running your service
  • Install the fastcgi-mono or mod_mono (apache) modules and run your service from Apache


However, the quick-n'-dirty way I do it is with xsp4. xsp is yet another pet project of Mono's, that aims to be a fast ASP.NET web server for development. However, there is nothing that says you can't use it in production. It allows SSL certificates, variable ports, etc. so you won't find yourself missing too many features from Apache.

Running xsp4 is dead simple. Go to the build directory (the one under your project root) and run "

xsp4 --port 80"

if all goes well with this, you should have a full-fledged ASP.NET MVC 4 application deployed under Linux, and available on the standard http port. This is, of course, the unsecure, bare-bones deployment with xsp4. But it's a good starting point. To find out how to make xsp4 handle errors, set up your certificates, etc. issue the command 

xsp4 --help

Hope you found this useful!

Wednesday, June 29, 2016

Using SQLite server with C# / Mono on Linux

If you've arrived on this post, it means that you're using Mono on Linux and attempting to use SQLite, and having a bit of difficulty. That's understandable. This is not a tutorial on SQL / Mono / Entity or anything of the sort. This will get a barebones implementation of a server working with mono.

The Problem
The problem is that Mono comes with Mono.Data.Sqlite. Yet, if you're looking for information on using SQLite in C#, you will be directed to use System.Data.Sqlite, which is included in the nuget library. You can add this using Xamarin's tools successfully. Intellisense should work, and most functions in System.Data.Sqlite should work as well... except for one: SQLiteConnection, which is used to connect to the database.

I'm not 100% positive on the nature of the problem, as it stands, in mono right now. I can narrow it down to (at least in my case) some kerfuffle regarding sqlite.interop.dll not playing nicely with my architecture of choice (x86_64).

The Fix
The best fix for this is to... use Mono.Data.Sqlite. However, this has some downsides.
  • Using a Mono library breaks compatibility with .NET... ouch.
  • Mono.Data.Sqlite, for whatever reason, is not a drop-in replacement of System.Data.Sqlite.
with these things in mind, let's do it.

using System;
using Mono.Data.Sqlite;
namespace SQLITETEST {
  class MainClass {
    SqliteConnection db = new SqliteConnection("Data Source={filename.sqlite};Version=3;");
    db.Open();
    SqliteDataReader reader = new SqliteCommand("SELECT x FROM y WHERE z = zz;", db).ExecuteReader();
    while (reader.Read()) { Console.WriteLine(reader.GetString(0));}

and there you go. This code will connect to a database {filename}, run a query, and return the results. If you have experience with System.Data.Sqlite, you should notice that the SYNTAX of Mono.Data.Sqlite doesn't differ from it too much... but the spelling does. System.Data.Sqlite prefers to use classes spelled as "SQLiteDataReader, SQLiteConnection", etc. while Mono uses "SqliteDataReader"... 

While this seemingly unnecessary difference is annoying, it's negligible. Enjoy kludging through Sqlite and Mono further!