A Foolish Manifesto

fREWdiculous!

Archive for January, 2009

Vim Settings

Today I am just going to talk about my favorite vim “stuff.” A lot of this I have gathered over the past 3-5 years of serious vim usage. I used vim before that, but not with this heavy of customization. I’ll start with the simple stuff and move up from there.

Basic settings:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
" Enable Line Numbers
set number

" Ignore case for searches
set ignorecase

" Unless you type an uppercase letter
set smartcase

" Incremental searching is sexy
set incsearch

" Highlight things that we find with the search
set hlsearch

" This is totally awesome - remap jj to escape
" in insert mode.  You'll never type jj anyway,
" so it's great!
inoremap jj <Esc>

" If you have caps lock on disable too many J's
nnoremap JJJJ <Nop>

" Set off the other paren
highlight MatchParen ctermbg=4

" no longer press shift to enter commands
nnoremap ; :
nnoremap : ;
vnoremap ; :
vnoremap : ;

I also highly recommend the InkPot color scheme for gui mode and metacosm for console mode. Here’s some code to pull that off:

1
2
3
4
5
6
" Favorite Color Scheme
if has("gui_running")
   colorscheme inkpot
else
   colorscheme metacosm
endif

I also use some plugins to make my life easier.

  • matchit: allows you to match things like html tags and other complicated matches with %.
  • NERD_commenter: simple keystrokes to comment/uncomment any code.
  • project: this allows you to have a user defined file listing on the left (or right) which I find quite nice for navigation. You can also use it to have special mappings defined when you open a file in a specific project.
  • surround: surround anything with quotes, parens, braces, xml tags… Great for html editing, but I use this all the time for normal code. (And I just used it to put all of these items in a list easily!)

Do you have any suggestions for killer additions to vim?

  • 4 Comments
  • Filed under: Uncategorized
  • Here’s some sexy code:

    1
    2
    3
    4
    5
    6
    7
    var boolArr = parseInt(localEnabled, 16).
       toPaddedString(16,2).
       split('').map(
          function (v) {
             return v === "1";
          }
       );

    It should be clear what it does from the title. The how is clear from the above. But I will explain how so that I can explain the why for each step.

    So first we start with a string something like “43c9″.

    parseInt(Str, 16) will parse that string into the actual number it represents. That’s not too complicated. So now we have 17353.

    Next we use the toPaddedString given to us by prototype. I originally used toString, but the problem there is that if your result is “0001″ it turns into “1″, which is not ok. So we have toPaddedString which gives us a string of length 16, in base 2. So now we have “0100001111001001″.

    The last part is easy, the split turns it into an array of single characters: ["0","1",0"...], and then map maps each item to something else, in this case a boolean expression. So our function above in the map just gives us an array which is based on some code applied to each item in the first array. So notice that the function is v === “1″; if our value is “1″ we get true, otherwise it’s false.

    Also note: the reason that we use === is because in javascript, much like other scripting languages, 0, “”, and null all evaluate to false; so if something has a chance of being one of those things === actually checks for equivalence. If I were to write this code again I’d use the regular == because we aren’t saying “” == 0 (which is true).

    Hope you found this interesting!

  • 0 Comments
  • Filed under: Uncategorized
  • Ruby1.8 vs. Perl6

    First off let me say that I love ruby. Ruby more or less taught me functional programming, which I love. But I do think that perl6 (which you may think is vaporware) is better. I only post about features which I can use right now in rakudo. With that said we shall move onward.

    Update: the rest of this post, although still correct, is flawed. See comments for the Correct Ruby solution :-)

    Fjord asked me about how to iterate over two lists at the same time in perl6. I have only had to do this a couple of times and I usually just end up doing a ghetto c-style for loop. In perl6 there is a better way. Check it!

    Perl6:

    1
    2
    3
    my @a = 1,2,3;
    my @b = 4,5,6;
    for @a Z @b { say "$^a $^b" }

    prints:

    1 4
    2 5
    3 6
    

    You may think, “fREW, ruby can do this and it does it exactly the same, if not better!”

    Ruby1.8:

    1
    2
    3
    a = [1,2,3]
    b = [4,5,6]
    a.zip(b).each { puts "#{x[0]} #{x[1]}" }

    Do you notice the subtle difference? In ruby we get [[1,4],[2,5],[3,6]] vs perl’s (1,4,2,5,3,6).

    That may not seem like a big deal, but what if you want to iterate over three lists? Here’s perl6:

    1
    2
    3
    4
    my @a = 1,2,3;
    my @b = 4,5,6;
    my @c = 7,8,9;
    for @a Z @b Z @c { say "$^a $^b $^c" }

    prints:

    1 4 7
    2 5 8
    3 6 9
    

    and Ruby:

    1
    2
    3
    4
    5
    6
    a = [1,2,3]
    b = [4,5,6]
    c = [7,8,9]
    a.zip(b).zip(c).each {
       puts "#{x[0][0]} #{x[0][1]} #{x[1]}"
    }

    That’s a drag! Anyway, I once read that there is a way to do this nicely in ruby, but I never could figure it out. I’d say the perl6 solution here is much nicer. Can someone prove me wrong?

  • 4 Comments
  • Filed under: Uncategorized
  • OLOTD

    perl5

    1
    join(' ', map { ucfirst } split(/s+/, lc($words)));

    perl6

    1
    $words.lc.split(/s+/).map({ ucfirst }).join(' ')

    This really should be a builtin. It is in perl6 and (I think) ruby.

    *Update*: So I was on the perl6 mailing list and this happened:

    19:42 <@TimToady> std: $a ==>>= $b
    19:42 < p6eval> std 25080: OUTPUT«00:05 84m␤»
    19:42 <@TimToady> oops
    19:42 < frew> nice
    19:43 < pugs_svn> r25081 | lwall++ | [STD] catch ==>>=
    

    p6eval is a bot that you can check your perl6 code on. pugs_svn says when someone checks into the source repository. It was then that I realize that TimToady was Larry Wall.

    He has actually spoken with me numerous times, which is pretty awesome. A lot of times I would ask a question and he would answer and add extra details. So that’s pretty great. I asked him a lot of questions about various things and he kindly answered. One of the things he told me was a shorter implementation of the code above in perl5:

    1
    $words =~ s/(S+)/uL$1/g

    How many of you have spoken to your language’s benevolent dictator?

  • 0 Comments
  • Filed under: Uncategorized
  • One liner of the day

    1
    test=29-array/pairs; cp ~/tmp/pugs/t/spec/S$test t/spec/S$test.t  && make t/spec/S$test.t

    Maybe I was doing it wrong, but this sure did make it nicer!

  • 0 Comments
  • Filed under: Uncategorized
  • Join = reduce

    I was driving today and I realized that join is just a form of reduce. Here’s some perl6:

    1
    2
    3
    sub join(Str $string, @array) {
       @array.reduce: { $^a ~ $string ~ $^b }
    }

    It works exactly as expected.

  • 1 Comment
  • Filed under: Uncategorized
  • I just committed my first change to the perl6 spectest suite. It’s exciting because perl6 has all of the great functional chaining that I love about ruby, but it also has killer awesome features that extremely few modern languages have (AST based macros anyone?) But it’s been in active development for almost four years now and people have talked about it for almost nine! So what do you do when you see these amazing things that are just outside of our reach? Jump in and help!

    Helping with perl6, especially the test suite, is not hard at all. The first thing you will want to do is find something to do. One good place to look is here. But that’s really not all. If you read through the spec files (a really great way to learn perl6 if you learn by examples) and look at the generated pod files you will surely find some discrepancies in the tests.

    Once you find something you are confident that you can do join the irc channel (irc.freenode.net/#perl6) and ask for a commit bit for pugs. Then you’ll just download the source,

    svn co http://username@svn.pugscode.org/pugs/

    make changes (probably in t/spec), and check them in.

    And now you have helped implement the spec for perl6!

  • 0 Comments
  • Filed under: Uncategorized
  • Definitions

    This is what I did in high school:

    fREW => fROOH Represents Encephelon Welkin

    fROOH => fRUE: Robotic Ominous Ossified Herald

    fRUE => fRIOUX’s Rectitude is Underpinned by Equivalence

    fRIOUX => fiSMBoC RESEARCHes IMAGINATIVE ORGANIC UNIFICATIONS like XUOIRf

    fiSMBoC => fREW is Station’s Most Bodacious Creation

    RESEARCH => Robots Eagerly Sailing Epic Artificial Rhythmic Cyclical Homonyms

    IMAGINATIVE => Insane Mimicries of Amazingly Gorgeous, Incomplete Networks, Axiomatic Theorems, and Immortally Vivacious Ecstasy

    ORGANIC => Original Renditions of Genetic Art Naturally Increasing in Complexity

    UNIFICATIONS => Unions Normally Identified From Initial Characters; Aesthetically Tailored to Infer Other Notions Subconsciously

  • 0 Comments
  • Filed under: Uncategorized
  • Can’t Sleep

    Dreamhost told me to update WordPress. The newer version is awesomes!

    I am trying to help with perl6 (specifically the spec. test) so that’s pretty sweet.

    Both of my paternal grandparents are in the hospital; grandpa probably won’t make it (spinal meningitis) but nana probably will.

  • 0 Comments
  • Filed under: Uncategorized
  • Why Object Oriented Programming Rocks (today)

    I am in the beginning of writing a web application with ExtJS. ExtJS is a javascript ui framework that’s extremely object oriented. I read once that it’s a good idea to predefine your user interface objects as (effectively) classes. One of the reasons for this is that it uses far less memory in our browsers. That’s a pretty good reason. Another reason is that you end up with smaller bits of code to work with at a time, thus allowing you to focus better on the task at hand.

    Well just now my boss asked me to add an image beneath a treeview. That basically involves making a panel that has a treeview and an image. Well, instead of having to go through the code and basically do it, all I had to do was change my ACDRI.ui.LeftPanel from a special treeview to a Panel that has an ACDRI.ui.NavigationPanel (what it used to be) and an image. The original code was actually entirely unchanged except for the name. Furthermore, if we decide to go back it will require exactly one line of code to change. Excellent!

  • Comments Off
  • Filed under: Uncategorized