Only one more project euler problem and I am level one
EDIT: YAY level 1
So my attempt to get my head around classes, has not been so successful.
I spent much time making the classes, it was quite boring. Nothing particularily interesting was involved except the always kinda fun part of fixing the bits that were wrong (however inane, oops, forgot to indent etc) and seeing it work when they’d been fixed.
Today I finally got the funnest bit done (which reminded me that I had indeed been bored of making definition upon definition to change object attributes). My main impression of classes from all this is, yes I can see how when you are doing a bigger project than I have ever done it will make it easy to reuse things and be modular and all that jazz, but for everything I have ever tried so far it basically entails doing all the most boring parts at once rather than in chronological order.
What I did today was get the final draft of the process to generate a schedule-todolist from a whole bunch of task objects and the majority of that comes down to variations on this:
who is tim? ….mysterious magic word
….|…………………………|
…\|/………………………..\|/
sorted(tasklist key=lambda task: task.priority)
(1. Randomise list so that when all else is equal the order is random.
2. Sort by priority.
3. Duplicate daystilldeadline/occurancestillcompletion < 1 until = 1
4. Sort by dtd/otc
5. Sort by Fixed (can only be 1 or 0))**
This statement gave me a few things to think about… it also exemplifies what I mean about just looking up magic words to do this. But I am a curious girl, so I am not just going to leave it at that…
Two things I have drawn attention to, sorted and lambda. Sorted, apparantly (if the internet is to be believed) uses something called a “timsort”. I don’t know how those work, but that will ceirtainly be something I want to look into. However I am more intrigued by the mysterious “lambda”.
An excruciatingly intelligent girl in irc mentioned this lambda thing to me a few days, maybe a week ago… she said then it was something about inline functions, she started telling me about functional programming and map and reverse. I like to be challenged but I’m gonna be honest and say… I didn’t really get it. I didn’t think too much about it either because I figured, I need to worry about more basic things for now and I can worry about lambda and map when I am more educated
. However, there it is, lambda, in my sorted… it is helping me somehow to say for each of these objects sort according to this attribute… but HOW does it do that, WHAT is it doing, and on a deep level HOW does it work?
My adventure begins with Wikipedia,
In programming languages such as Lisp and Python, lambda is an operator used to denote anonymous functions or closures, following the usage of lambda calculus. An example of this use of lambda in the Python language is this section of computer code that sorts a list alphabetically by the last character of each entry.
Python’s limited support for anonymous functions is the lambda construct. Since the availability of full anonymous functions is non-existent then named functions is the primary use of functions in Python. Lambdas are limited to containing expressions rather than statements, although control flow can still be implemented less elegantly within lambda by using short-circuiting.
http://en.wikipedia.org/wiki/Anonymous_function
Esp. http://en.wikipedia.org/wiki/Anonymous_function#Python
Next stop the Python documentation,
Lambdas
*******lambda_form ::= “lambda” [parameter_list]: expression
old_lambda_form ::= “lambda” [parameter_list]: old_expressionLambda forms (lambda expressions) have the same syntactic position as
expressions. They are a shorthand to create anonymous functions; the
expression “lambda arguments: expression“ yields a function object.
The unnamed object behaves like a function object defined withdef name(arguments):
return expressionSee section *Function definitions* for the syntax of parameter lists.
Note that functions created with lambda forms cannot contain
statements.
…wikipedia was more helpful imho
Python lambda forms cannot contain statements because Python’s syntactic framework can’t handle statements nested inside expressions. However, in Python, this is not a serious problem. Unlike lambda forms in other languages, where they add functionality, Python lambdas are only a shorthand notation if you’re too lazy to define a function.
Functions are already first class objects in Python, and can be declared in a local scope. Therefore the only advantage of using a lambda form instead of a locally-defined function is that you don’t need to invent a name for the function – but that’s just a local variable to which the function object (which is exactly the same type of object that a lambda form yields) is assigned!
OK… I have a reasonable idea WHAT lambda is now (and map to my surprise!) – I am still not at all sure how it is implemented (indeed I think the first step there would be to understand the nature of first class objects as opposed to any other kind).
…and so you see I did have fun with this after all (and it was a lot less confusing that I’d expected)
Other interesting websites I came across on my travels:
http://code.activestate.com/recipes/361452-anonymous-functions-without-lambda/
http://fundae.wordpress.com/2008/05/14/functional-programming-101-lambda-forms/
http://www.ymeme.com/python-vs-lisp.html
http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/
http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html
* There are probably far better way of doing this I don’t know but go easy on me I am just starting out with this stuff.
I am doing my tamagotchi virtual pet program in C and passing my strings around and changing them between functions and all sorts, and getting compiler warnings galore in my tests. And google says to me “you have to allocate memory you n00b”.
So I am going to allocate some memory. Then (hopefully) free it at the right time.
It’s so exciting! It seems like such a powerful and deep thing to be able to do. Mwahahaha. Wow…
I love this…
I’ve been messing around a bit with C lately. So I have decided to make a little virtual pet type thing in it. I have no clue how to deal with a lot of things, such as, how in a text based ascii character drawn application, do you make it constantly receptive to input without being constantly demanding of input.
Secondly some more difficult aspects of my ascii animation, do I have to rwdraw everything every frame? Is it not possible to only redraw the actual moving part?
I am having a lot of fun though. So far of all the programming languages I tried (in this incredibly short space of time) I have had most fun with C. I think this is because it’s simple. Like, python was not so simple, it threw a whole load of concepts at me before I’d really grasped the basics. Because it had simpler syntax it seemed like I should be able to grasp more concepts at once, but concepts involve more cognitive load than syntax.
C had at first intimidating looking syntax, but its nice and regular and pedantic. So it quickly floated into the background.
I also think partly its because I really don’t understand this object oriented thing yet. C is not confusing me with object oriented stuff that I don’t understand (although I am told it can :p).
Anyway, even though its quite simple really, it is very satisfying to see my little ascii creature bobbing around the screen. All thanks to Linux without whom I never would have discovered the coolness of programming :p