Needless to say, these come up a lot. How could they not?
Fledgling programmers need to know how these are handled, and I remember a day when I didn't have a flying clue how do deal with them. I mean heck, who would ever use command-line args? Who even uses the command-line? (said he, who at the time who had not even yet compiled a java program out of an IDE)
I was ignorant, to say the least.
Github :: Command-line arguments
The C and Haskell implementations are straight-forward. They're more of an "Oh, okay" rather than any sort of programming revelation. My Python implementation, however, is a big more stylised for real, well-defined use. It should be noted that it is also available in my python-libs repo on Github.
Thursday, August 4, 2011
Wednesday, August 3, 2011
Algorithm-a-Day : Day 03 : cat
Everyone's favourite shell command. Goes well with pipes. And sugar.
Github :: cat
All three are fairly simple, with the C implementation probably being the most so (which is to be expected). The Haskell version still makes me giggle, though.
Github :: cat
All three are fairly simple, with the C implementation probably being the most so (which is to be expected). The Haskell version still makes me giggle, though.
Tuesday, August 2, 2011
Algorithm-a-Day : Day 02 : Line Count
Counting the number of lines in a given file, in C, Python, and Haskell.
Quite simple today, but I needed some easy stuff right off the start to balance the more interesting algorithms to come later.
Github :: Line counting
Quite simple today, but I needed some easy stuff right off the start to balance the more interesting algorithms to come later.
Github :: Line counting
Monday, August 1, 2011
Algorithm-a-Day : Day 01 : Reversing words in a string.
This seems to come up as an interview question more often than not. Here is the solution in C, Python, and Haskell.
Github :: Reversing words in a string
While the Python solution is pretty trivial, Haskell wins out of the three.
Nothing beats a one-liner built from library functions.
I think there's sense in "Don't reinvent the wheel" except when you're reinventing it as a nice exercise, as I did with the C solution.
EDIT (8/11/2011) : Fixed link, and bug in code.
Github :: Reversing words in a string
While the Python solution is pretty trivial, Haskell wins out of the three.
Nothing beats a one-liner built from library functions.
I think there's sense in "Don't reinvent the wheel" except when you're reinventing it as a nice exercise, as I did with the C solution.
EDIT (8/11/2011) : Fixed link, and bug in code.
Monday, July 25, 2011
Algorithm-a-day :: Starting in August.
Come August first I'm going to be releasing an interesting algorithm once a day, in Haskell, Python and C. Not only will this be a good repertoire of common algorithms, but it'll also be good practice for me to keep me on my toes in the various languages.
I focus on Haskell mainly these days, but I can't let my most fluent language (Python) and my batter-on-deck (C) get rusty.
Please look forward to A.a.D!
I focus on Haskell mainly these days, but I can't let my most fluent language (Python) and my batter-on-deck (C) get rusty.
Please look forward to A.a.D!
Tuesday, June 28, 2011
Mind Blown by a Save Icon
My Mind Blown by a Save Icon
I was writing a short article on the use of 'it' in the English
language earlier today. I happened to be on my Linux Mint boot, and
was using the (very compliant so far) Libre Office Writer to get my work done.
I wrote a section of decent length before I realised I hadn't saved yet,
and for no reason in particular I decided to hit the save button on the tool
bar instead of 'Control + s' like any badass would've normally done.
I was surprised at first when I couldn't find the icon. Where the hell was
the little floppy? I need my doc sa-...
...which was the moment a saw an icon; a green arrow over-top a hard-drive.
"It couldn't be..." I thought vainly as I brought my cursor over the icon
to bring up its mouse-over text. Sure enough, "save" appeared before my eyes.
From a picture of an arrow and a hard-drive.
My foolishness hit me then. Why the hell would a save icon be indicated by a
floppy disk? When was the last time I even TOUCHED a floppy disk? I chuckled
to myself at how much sense it made. This was followed by rage, for the real
party at fault here was the developers who persist in maintaining
familiar icons for the sake of convenience and "ease of use."
Well ease-of-use can walk the plank as far as I'm concerned here. I have not
saved anything on a floppy in years, and neither have those developers.
My hat goes off to the champions at Libre Office for being true to what
is realistic.
We're big kids. We can handle change.
I was writing a short article on the use of 'it' in the English
language earlier today. I happened to be on my Linux Mint boot, and
was using the (very compliant so far) Libre Office Writer to get my work done.
I wrote a section of decent length before I realised I hadn't saved yet,
and for no reason in particular I decided to hit the save button on the tool
bar instead of 'Control + s' like any badass would've normally done.
I was surprised at first when I couldn't find the icon. Where the hell was
the little floppy? I need my doc sa-...
...which was the moment a saw an icon; a green arrow over-top a hard-drive.
"It couldn't be..." I thought vainly as I brought my cursor over the icon
to bring up its mouse-over text. Sure enough, "save" appeared before my eyes.
From a picture of an arrow and a hard-drive.
My foolishness hit me then. Why the hell would a save icon be indicated by a
floppy disk? When was the last time I even TOUCHED a floppy disk? I chuckled
to myself at how much sense it made. This was followed by rage, for the real
party at fault here was the developers who persist in maintaining
familiar icons for the sake of convenience and "ease of use."
Well ease-of-use can walk the plank as far as I'm concerned here. I have not
saved anything on a floppy in years, and neither have those developers.
My hat goes off to the champions at Libre Office for being true to what
is realistic.
We're big kids. We can handle change.
Monday, June 27, 2011
My Coding Rules
Rules I follow when coding. They keep me on track for solid code.
- Don't clutter the namespace. Use as few variables as possible,
while still maintaining clear code. Aim for low amounts of
plain-coloured text in your editor!
Turn this,
def two_n_four():
result = 2 + 4
return result
into this,
def two_n_four():
return 2 + 4
- Use as many built-ins as possible. They will be faster than anything
you can write.
- Early / multiple returns. This reduces indentation and increases readability.
Turn this,
def the_func(best_arg):
if not isinstance(int, best_arg):
result = 'Hey stop that'
else:
x = foo(best_arg)
y = bar(x)
result = baz(y)
return result
into this,
def the_func(best_arg):
if not isinstance(int, best_arg):
return 'Hey stop that'
return baz(bar(foo(best_arg)))
- Low line numbers. Multiple returns can aid this. Multiple returns DO NOT
necessarily make code harder to follow!
Remember, the easiest way to do something is the easiest way to do something.
- Functional programming style = beauty.
Turn this,
def factorial(num):
'''I'm pretty fast and typically procedural!'''
result = 1
while num > 1:
result *= num
num -= 1
return result
into this,
def factorial(num):
'''Look how many lines I didn't take!'''
return reduce(lambda acc, n: acc * n, range(1, num+1), 1)
PYTHON RULES
- Write docstrings.
- Bind specific imports to local names!
e.g.,
from functools import reduce as _reduce
- Use iterators. Then use some more. A lot of functions accept iterators
as arguments and they can really reduce the amount of work that needs
to be done before an answer is arrived upon.
- Use decorators to rid your functions of 'administrative work'.
Like this:
def non_empty(func):
'''Does not allow an empty list to be passed to a function.'''
def inner(arg):
if len(arg) < 1:
raise ValueError('Empty list given.')
return func(arg)
return inner
@non_empty
def mean(items):
'''Calculates the mean of a list of numbers.'''
return sum(items) / len(items)
This keeps the function clean and to the point. Anyone looking at the
decorator should also be able to tell what it does by its name.
---
My personal goal in programming is a harmonious mix of beauty and
efficiency. I believe programming to truly be an art (I often do it
to calm down) and I employ the rules above to make sure my code is
as clear and elegant as I can possibly make it.
- Don't clutter the namespace. Use as few variables as possible,
while still maintaining clear code. Aim for low amounts of
plain-coloured text in your editor!
Turn this,
def two_n_four():
result = 2 + 4
return result
into this,
def two_n_four():
return 2 + 4
- Use as many built-ins as possible. They will be faster than anything
you can write.
- Early / multiple returns. This reduces indentation and increases readability.
Turn this,
def the_func(best_arg):
if not isinstance(int, best_arg):
result = 'Hey stop that'
else:
x = foo(best_arg)
y = bar(x)
result = baz(y)
return result
into this,
def the_func(best_arg):
if not isinstance(int, best_arg):
return 'Hey stop that'
return baz(bar(foo(best_arg)))
- Low line numbers. Multiple returns can aid this. Multiple returns DO NOT
necessarily make code harder to follow!
Remember, the easiest way to do something is the easiest way to do something.
- Functional programming style = beauty.
Turn this,
def factorial(num):
'''I'm pretty fast and typically procedural!'''
result = 1
while num > 1:
result *= num
num -= 1
return result
into this,
def factorial(num):
'''Look how many lines I didn't take!'''
return reduce(lambda acc, n: acc * n, range(1, num+1), 1)
PYTHON RULES
- Write docstrings.
- Bind specific imports to local names!
e.g.,
from functools import reduce as _reduce
- Use iterators. Then use some more. A lot of functions accept iterators
as arguments and they can really reduce the amount of work that needs
to be done before an answer is arrived upon.
- Use decorators to rid your functions of 'administrative work'.
Like this:
def non_empty(func):
'''Does not allow an empty list to be passed to a function.'''
def inner(arg):
if len(arg) < 1:
raise ValueError('Empty list given.')
return func(arg)
return inner
@non_empty
def mean(items):
'''Calculates the mean of a list of numbers.'''
return sum(items) / len(items)
This keeps the function clean and to the point. Anyone looking at the
decorator should also be able to tell what it does by its name.
---
My personal goal in programming is a harmonious mix of beauty and
efficiency. I believe programming to truly be an art (I often do it
to calm down) and I employ the rules above to make sure my code is
as clear and elegant as I can possibly make it.
Subscribe to:
Posts (Atom)