Erlang

Another Attempt At Python

3 min read

I tried Python out a while ago, but stopped trying it to learn it after some major frustrations. Maybe I didn’t dig deep enough into it. I found the documentation hard to read, and the module layout seemed a little random at times. For some reason I found executing an external process and getting the results to be a little convoluted. (Since then I’ve learned to use popen(..).communicate())

I ended up messing with other languages to try to find one that suits my tastes, like Erlang and D. I read through 7 languages in 7 weeks, and not really getting a lot out of it. I didn’t fall in love with ruby at all and I’m not going to actually use Prolog anywhere, even if I thought it was pretty cool. I never liked Java, and I wasn’t impressed with Scala.

erlang java python
Read more

Combining iWatch with Reloader

1 min read

I had written a post about how to set up reloader.erl about a year ago. Reloader.erl is an incredibly useful library included in mochiweb for automatically reloading erlang libraries if they’ve been recompiled.

I had also written a post about iWatch and phpunit to follow source directories and automatically run a unit test (or other command) when src files change. Really great for rapid development - you can code in one window and just glance over to know if things are working as expected.

erlang
Read more

Erlang Code Auto Reloader - Auto Run Unit Tests and Hot Load Code after a Build

2 min read

I wrote this post a few months ago when I was developing a new Erlang application. I eventually ended up using the reloader I mentioned at the end, but never documented it. Now I’m about to start writing a new app, and I’m on a different machine.

First, get the reloader. It was developed as part of mochiweb but functions just fine on it’s own. Just put it somewhere accessible - I use the ebin directory in my home folder. (yes, I just copied & pasted the source) Then compile it like so:

erlang
Read more

Smarter Erlang Programming with Emakefile Options and user_default

2 min read

Say you’ve got an Erlang app with the standard file system layout, with your source code in src/ and your compiled code in ebin/. I’ve found this makes using the interactive shell interpreter a little harder.

Lets use the Emakefile that Erlang supports to let us recompile and reload our code on the fly.

Create a file called Emakefile, and put the below line in it. There’s other options, but this keeps things simple.

erlang
Read more

Debugging with Erlang

1 min read

First, make sure you have the compile flag (+debug_info) set when compiling your source, then fire up the debugger:

1> i:im().

My Erlang Makefile:

EBIN_DIR := ebin
SRC_DIR := src
EXAMPLES_DIR := examples
INCLUDE_DIR := include
ERLC := erlc
ERLC_FLAGS := +debug_info +native -W -I $(INCLUDE_DIR) -o $(EBIN_DIR)

all:
	@mkdir -p $(EBIN_DIR)
	$(ERLC) $(ERLC_FLAGS) $(SRC_DIR)/*.erl

clean:
	@rm -rf $(EBIN_DIR)/*
	@rm -f erl_crash.dump

Read up on the Erlang docs.

erlang
Read more

'Running Erlang Code from the Command Line '

1 min read

This was really useful for me in scripting TextEdit to run my unit tests, as Erlide has been crashing every time I use it.

erl -run mymodule myfunc -run init stop -noshell

[http://www.trapexit.org/Running_Erlang_Code_From_The_Command_Line](Running Erlang Code From the Command Line)

erlang tips
Read more

'Erlang: Understanding gen_server'

3 min read

Gen_server is a great way to create simple servers without having to write a lot of code at all. Here’s a brief overview to get you started.

For some reason, figuring out how the gen_server behavior works in erlang was kind of a pain for me. I think it’s because I can’t just implement something, I need to know why it works. Well, now I know.

So, lets get started. Gen_server is built into erlang. It abstracts the message passing away. As far as I can tell, yes, you could write this functionality yourself. But why would you want to when it’s done for you?

erlang gen_server
Read more

'Erlang: Create schema before starting mnesia'

1 min read

If you’re running into trouble with Mnesia not writing your data to disk, make sure you create the schema BEFORE you start mnesia. Otherwise you’ll get errors like

opt_disc. Directory "c:/Documents and Settings/jhaddad/workspace/Mnesia.localhost@whatever" is NOT used.

erlang
Read more

Erlang Records Cheatsheet

1 min read

I hate looking stuff up. I just like having this type of thing 1 click away, in a nice summary. This post is mostly for me.

Define a record:

-record( rule, {ruleid, site, rule, original} ).

You can define a record in the shell using rd:

99> rd(rule, {ruleid, site, rule, original} ). rule

Create an instance of a record:

100> A = #rule{ ruleid=1, site=2, rule=2, original=3}. #rule{ruleid = 1,site = 2,rule = 2,original = 3}

erlang
Read more

'Erlang: Installing Leex'

1 min read

Leex is an erlang version of Lex, a Lexical Analyzer Generator written by Robert Virding. Robert (and several others in #erlang on freenode) were incredibly helpful and considerate in helping me understand these tools.

Leex is a tokenizer. It breaks the pieces of your file or text into tokens. You can then use a tool like yecc to take these tokens and generate useful parsers.

First, you’ll need the source. Leex source is available here. Go down to the last post in the forum, download the attached file.

erlang leex
Read more

Erlang - Working with ets:select_count

1 min read

In my last post, I covered using guards with Erlang’s ETS select/2 functionality.

However, what if you’re looking to select a count of the number of matches for a given pattern? Lets use select_count for that.

You can use the same pattern matching I covered in my previous post.

There’s some weird behavior though, with the last parameter of the Result part of the record. It seems that using ‘$$’ returns 0. Instead, it looks like you need to use true.

erlang
Read more

'Erlang: ETS Matching with Guards (select/2)'

2 min read

Not every time we want to pull data out of an erlang ETS table is it as straightfoward as the previous example. Sometimes we want to get all values that are greater than zero, rather than just constants. We’ll need to use the ets:select function, which has support for guards.

Here’s a basic setup.

Tmp = ets:new(tmp, []).
ets:insert(Tmp, {bob, 0}).
ets:insert(Tmp, {jim, 1}).
ets:insert(Tmp, {jon, 2}).

% Returns all results
ets:select(Tmp,[{{'$1','$2'},[],['$$']}]).

% Returns results where $2 > 0 (jim and jon)

ets:select(Tmp,[{	{'$1','$2'},	[ { '>', '$2', 0 } ],['$$']	}]).

What’s going on in the select? There’s a LOT of syntax for something that’s so simple, but once we break it up a bit it starts to be easier to look at.

erlang
Read more

'Erlang: Super Basic ETS Matching Tutorial'

2 min read

I hate having to look stuff up to get examples, especially when I have to click on more than the first google link to figure things out. As a result, here’s a very, very basic intro do doing matching with ETS and erlang. It’s similar to a SELECT in SQL.

Here’s some simple matching.

44> Tmp = ets:new(bacon, []). 26 45> ets:insert(Tmp, {joe, {fish, 1}}). true 46> ets:match(Tmp, {’$1’, {fish, ‘$2’}}). [[joe,1]]

erlang
Read more