2014-12-11

Language Comparison

Perl:

while (<>) { print "$. : $_" }

Perl:

while (<>) {
  print "$. : $_"
}

CSharp:

using System;
using System.IO;

class App {
	public static void Main(string[] args) {
		int line_number = 1;
		foreach (string arg in args) {
			foreach (string line in File.ReadLines(arg)) {
				Console.WriteLine(line_number + ":" + line);
				line_number++;
			}
		}
	}
}

Python:

import sys

for arg in sys.argv[1:]:
	for index, line in enumerate(open(arg).readlines()):
		print(index + 1, ":", line, end='')

Golang:

package main

import (
	"fmt"
	"os"
	"bufio"
)

func main() {
	line_number := 1
	for _, arg := range os.Args[1:] {
		file, _ := os.Open(arg)

		scanner := bufio.NewScanner(file)
		for scanner.Scan() {
			fmt.Printf("%d : %s\n", line_number, scanner.Text())
			line_number++;
		}
	}
}

Java:

import java.io.*;
import java.util.*;

class Linum {
        public static void main(String[] args) throws FileNotFoundException {
                int line_number = 1;
                for (String arg : args) {
                        Scanner s = new Scanner(new BufferedReader(new FileReader(arg)));

                        while (s.hasNextLine()) {
                                System.out.println(line_number + " : " + s.nextLine());
                                line_number++;
                        }
                }
        }
}

Ruby:

while gets
  puts "#{$.} : #{$_}"
end
Standard

2014-06-18

Food for Two (or three)

Jayne and I went to Olive Garden tonight and took Hiram. We all just ate soup and an appetizer of fried raviolis and fried zucchinis. It was one of the best meals I remember at Olive Garden. It was really simple, but everything tasted really good.

We are trying to cut back our spending on going out to eat, so it was nice to have a really enjoyable meal that was pretty affordable. Over the weekend we also went to La Hacienda Ranch to celebrate Father’s Day with Jayne’s dad. That is another restaurant where one can get a really enjoyable meal for two fairly cheaply. The veggie fajitas for 1 (but shared between two) seems like an excellent choice.

The One Thing I Don’t Like about Scala (so far)

The one thing I’ve found so far about Scala that I don’t like is a tendency to have syntactic shortcuts in certain situations that are not very regular with the rest of the language. One example specifically is in the way that you can define a curried function that takes 2 arguments and then call it with the second argument enclosed in curly brackets ("{" and "}") rather than parenthesis. This seems to be so that you can mimic the look of Scala’s native control structures. However, I believe you can only have one argument (one expression?) in between the curly brackets, which breaks the expectation that curly brackets usually enclose a block. That expectation is set not just by c based languages like C, C++, Java, C# and JavaScript, but also in most other cases by Scala itself.

This breaking of expectation is what frustrates me the most about Scala, because otherwise I really like the language. In general it does a really good job of unifying object oriented and functional concepts, so that semantically it reminds me of a cross between Smalltalk or Ruby and SML. In fact, along with Ruby, I think of it as one of the few languages to really get object orientation right. The frustrating thing about the ways that syntactic sugar seems to have been added for convenience rather than coherence is that it’s not obvious why the shortcuts are that much better than the desugared alternatives. Now this criticism isn’t applicable to all syntactic sugar in Scala, some of it is done really well. But there are enough places where it is rough, that it feels like there is a constant slight cognitive load to the language that doesn’t need to be there.

Even though I’ve noticed these little nits, I still plan to keep using it. Overall, I really like the language. I find it’s support for functional programming refreshing, and I think the way it deals with objects makes dealing with them a lot nicer than in C# or Java.

Standard

2014-06-16

Blathering about Blogging

I’m just blathering about blogging today. If I was going to try to do a blog I think it would be valuable to concentrate on doing actually valuable writing. Maybe it would make sense to do about 1 essay per week and then try to post something in the roughly 200 word range each day. Something like 200 words a day and a 1000 word essay per week. Something like that I think could actually be fairly valuable. I still don’t really know what platform I’d like to use for blogging.

I think I’d like to modify my blog layout so that there is a clear delineation between daily posts that can be just blather and essays and tutorials that should be better written.

Standard

2014-05-22

Testing

I’m just trying out the mobile optimized version of the web post editor on my Blackberry.

Mobile WordPress

I’m just testing the mobile optimized version of the WordPress website to see how well the web based editor and dashboard look on my BlackBerry Q10.

I’d probably prefer a native app for the BlackBerry, but since there isn’t one currently, I think this will be adequate.

Standard

2014-02-20

inactive

It’s been almost a year since my last post. I’ve been journaling mostly offline for a while. I’ve been trying to write 1000 words a day each day. A lot of the time I achieve that, some days I don’t. But I haven’t been good at going back through my writing to pull out things and put them up here on the blog. I’d like to start doing a better job of that.


Thoughts on Lua

I’ve been messing with Lua for the past week or so. I’ve been fairly successfully using it to go through the challenges on code eval (sea6ear on the leaderboard).

So far I really like Lua. I’m kind of a sucker for simplicity and performance, and the abstraction abilities of Lua are pretty good for a language that isn’t Scheme, Common Lisp or Haskell.

However, I’ve used it enough recently that I’ve started to see what are probably the most obvious annoyances. [note: I’ve only been using it steadily for a week, I’m still a newbie. these are mostly first impressions.] Also these are more gotchas than showstoppers, I think once you hit these and are aware of them, you can avoid them.

The most obvious annoyance once you start writing code is the need to explicitly declare local variables. It’s easy to forget and end up with global variables when you didn’t intend for them to be. This is pretty much the same way that JavaScript or Perl handle things, so this isn’t unique to Lua.

A more important annoyance is the interaction between two features that don’t seem dangerous in themselves. Lua automatically coerces values between numbers and strings. usually that’s something I find convenient. However, Lua’s tables also switch between acting like an array and acting like an associative array (dictionary/map/hash etc) based on whether integer indexing or string based (or other value) indexing is used.

What this means is that you have to be very careful when dealing with data that could be either a string or number and may be used as an index of a table. If you accidentally use it in the wrong format, or accidentally switch between trying to index with an integer vs string format, you will put or try to access data in the wrong part of the table. This can make it look like your data has disappeared when really it’s just been shuffled off to another part of the table that you weren’t trying to use right now.

On a different topic, and really just a minor annoyance, arrays are 1-based instead of 0-based. One based array indexing wouldn’t really be an annoyance except that the rest of the computer world has standardized on zero based indexing (except for awk, a language with some similar goals to lua). It’s not too bad in practice, except when you need to be translating between a zero-based scheme and the one-based scheme.

Overall I really like lua. I’m really impressed with it. It seems to have almost everything that I want to have in a language as far as semantics. I like that it has tail-call optimization so well constructed recursive functions don’t have to blow the stack. I like that functions can be passed as values. I like (overall) that tables are the central data structure and that you can use them to build all the other kinds of data structures that you desire. I think I will enjoy it’s lightweight approach to object orientation.

I wish that it was more evidently useful as a general class scripting language instead of being primarily thought of as a language for embedded scripting. On the other hand, I don’t want to change it so that it looses that focus. What I’d like to see is a real secondary distribution that had all the libraries to allow Lua to function in cross-platform general purpose fashion. (luarocks might be that, but I’m not sure how comprehensive it is or how widely it’s used. luadist seems like a similar attempt) so basically, I’d like the core distribution to stay small and focused, but there to be some kind of additional library or platform that would allow one to get all the libraries on needed for doing stand-alone work with lua.

I’m not sure how much work that would be. I’d like to see that happen. I want that enough that I’m willing to work to try to make that happen.

On the other hand, it looks like other people have occasionally tried similar things, and it doesn’t look like any of the other attempts that I saw (outside of luarocks, and maybe luadist?) have gotten persistent traction. Part of that may be that each other attempt at bundling additional functionality has been focused on some fairly narrow niche. I don’t know how much work it would be to create a curated library of various modules that would give you the basics you would need to do real work on various platforms. I’d like it to be something that you would download in addition to the standard lua download rather than instead of.

I’m not sure what kinds of additional functionality (outside the lua core) would be necessary for lua to be able to easily handle the same kinds of tasks that one would do in Python, Ruby or Perl.

Here’s what I can think of:

  • gui bindings
  • file system manipulation
  • http request libraries
  • web server capabilities
  • process interaction and control

Many of these exist in some form currently, but they are scattered and I’m unsure of the level of documentation or consistency of maintenance.

I wonder how many people focused on making it happen it would take to make Lua into a viable language for general purpose computing. I think it could probably be done with less than 20.

I think if I was involved in starting a company I would be tempted to use Lua for the scripting level (along with c for low-level, and erlang for very high-level tasks) in hopes that the company could contribute code to making Lua a viable language for general use outside the game industry and niche areas. Even though lua wouldn’t be as useful as python or ruby starting out, I think it would be worthwhile to focus on it and make libraries that did enable it to be used in a wide range of areas.

Lua could also use some more beginner oriented books. Programming in Lua is awesome, but it’s a pretty advanced text in terms of the language concepts it deals with. I would be interested in writing one of those beginner books once I am more familiar with it.

Standard

2013-03-17

Language Choices

I think that if things were up to me I’d have one list of languages that I concentrate on for continuous study and application development, and a list that I bounce around in for fun and study and making toys and stuff.

The core languages I’d concentrate on for continous study and industrial application development:

  • c++
  • perl
  • common lisp
  • ocaml

The languages for prototypes, utilities, fun, exploration and experimentation:

  • c
  • lua
  • javascript
  • scheme
  • tcl
  • smalltalk
  • forth
  • prolog
  • erlang
  • sml
  • awk
  • assembly

I think the core languages are strong enough that any of them could be used for almost any task, but there’s enough flexibility in the list to pick a good match for any particular use.

I’d use the larger list for single purpose apps and experimentation. Each of them is small enough that I think I could get into a partincular language for a month or so and either learn something useful or develop something small but real using it.

Standard

2013-01-24

Languages for Learning

Here is a list of languages that seem to me useful to learn in the sense that between them they cover all the major concepts that exist in other languages, yet they are all individually small enough that you can easily learn the particular language and keep it in your head.

I think that one should be able to become somewhat proficient in any of these languages with about a month of reasonably serious study. Experimentally, I seem to be able to keep about 3 of these going as far as casual study at the same time.

I believe that most interesting language concepts are expressed in some way by at least two languages in this list. So each major concept should have at least two potential implementation to compare.

  • c
  • scheme
  • forth
  • prolog
  • erlang
  • sml
  • lua
  • tcl
  • ruby
  • awk
  • javascript
  • go

I think that from this base one could pick up almost any industry language with a decent understanding of the concepts. So given about a year of exploratory study of these languages (perhaps 1 per month?), one should be well equipped to handle learning any of the more complex languages currently used.

other things to learn

To be an effective programmer, here are a few other things it really would help to have a grounding in as far as general use / concepts.

  • html / css
  • sql
  • assembly language (I think any one would give you the concepts that are useful)
  • command line (probably preferably unix based)

An argument can be made that these are / aren’t languages in the same sense of programming language concepts covered by the earlier list of languages. However, ones life as a programmer will be subtly constrained to the extent that one is not familiar with these technology areas.

Standard

2013-01-23

The Surprising Use Case For Vim

I’ve spent a lot of time trying out different text editors. among the reasons that I continue to come back to Vim, there is one that is surprisingly powerful, and somewhat unexpected.

The reason: I can print from Vim on every computer I’ve installed it on.

Most other cross-platform editors fail as far as printing documents on some OS or another (often Windows). Vim is the only one that I’ve seen reliably print everywhere I’ve tried it. As a bonus, it can print just as well from the terminal as from the gui.

Standard

2013-01-18

coloring vim after 80 characters or more

from this stackoverflow question,

to enable columns to be colored differently in vim for certain columns or ranges:

for everything after column 80 to be colored:

let &colorcolumn=join(range(81,999),",")

for column 80 to be highlighted, and everything after 110:

let &colorcolumn="80,".join(range(111,999),",")
Standard