Friday 30 July 2010

How do I love her?

I think I'm developing this unhealthy habit of mentally not working on fridays even though I'm there physically. If I was an employer of labour and I was paying you to work on friday then you better work. God, though, is His infinite wisdom has deem it fair to spare the world the indignity of my pride - and I'm grateful - at least for now.

Talking about God, yesterday my family and I read Jeremiah 3. Hindsight helps to further drive home the message in Jeremiah. You see Jeremiah lived in the time of the last 3 kings or thereabout of Judah. In chapter 3, God makes a plea - it's simple and it's direct. First he tries to make them realize that they've done Him wrong (cue babyface singing 'til u do my right...'). Then he gives them an option: say I'm sorry and mean it - and the threat of Nebuchadnezzar (i bet the spelling is wrong) would go away. God's ubercool - way cooler than I could ever be. I wish I could be like Him really - eternality would be a blast.

I'm on the verge of asking her out. I've always been impatient for a shy guy. I usually just watch non-chalantly but get me interested and it's like a fireworks show. Now, I think about her a lot. Sometimes, I'm scared that she'd say no. I'm convinced that there's nothing awesome or cool that I could give her. I'll totally be the lucky one. Other times, I'm scared that I might hurt her - if there's anything I know its that I can be vindictive. I'll 'overlook' the multitude of the wrongs till I snap one day (think live version of Dr Gru in Despicable Me). My communication, obviously, also sucks - just ask my previous and up until her, only, gf. She's sweet, God-fearing, caring, stubborn and defiant. I've prayed and I think God's answered but I'm at the point where I'm praying "I believe, please help my unbelieve". I bet Stephen Curtis Chapman would be proud of me - I'm living out the words of one of his songs (the same one this post is named after).

I heard Sade's 'babyfather' on last.fm then went looking for the video on youtube. Nice song. I should paste a link to it... but I won't.

Wednesday 28 July 2010

Guess who's back...

So, I read this article today about how Nigeria is in the top 10 countries in the world in terms of internet usage. Now this would be cool except for the fact that this honour makes the dishonourable more ... (I cant find the word - let's try odious). Sometime back I ran into this other article too about how very few of the major IT firms seem to a presence in Africa. Combine those articles together and the color you get is not pleasant to behold at all... at all. The only real explanation I can think of for the lack of corresponding IT advancement has to be that we are a nation of consumers - but dont take my word for it as I'm not particularly smart.

I did write up something on my iphone (notice how the I and the P isnt autmagically capitalized?) last week but I dont really dig the whole mailing posts to your blog idea. The good news is that I've received a brand new replacement N900 from Nokia and a new battery (not free, obviously) from RadioShack. So, hopefully, the whole TGIF blogging thingy can resume. To be fair, my original N900 took quite some bashing/malhandling so I'm extremely grateful to God (and obviously, the folks at Nokia) for letting it get spoilt before the warranty expired. I get the feeling that in this its second coming, the phone would be mainly used for python scripting and reading technical material.

I came across Bernard Harris' myspace page over the weekend. I remember those days (he was in college and I in high school) when he used to amuse us with his singing - a lot of us felt, back then, that there was more shouting than singing going on. But I'm glad he still sings and I sure as hell (no swearing intended) would pick up a copy of his album when I money to spare. I always loved his singing (note I didnt say ministry) but then again I guess something has to be said for the fact that I grew up listening to Take6. The video from his MySpace page is at the bottom of this post i.e. if you are in too much of a hurry so that you can't visit his site.

money

Let's get this straight - money doesn't make you happy. It never did, it never will. I forget this sometimes, myself, when life's rat race gets the better of me and I lose sight of God. The thing is Jesus Christ, the disciples, the prophets, and a large junk of the guys in the bible were, in their own times, below middle class by earth's material standard. The real issue is that God doesn't need money to provide my every need - the cattle on a thousand hills are His and His hands are not too short nor is He hard of hearing. Yes, He can choose to bless me financial as indeed He promises that He would (along with other things too like trials o) but it's the peace of mind I have in Him and the trust in him that constitute my happiness not how much - or how little - money is in my pocket.

I just had to put say that so that instead of explaining to my friends I can tell them to come read it up here. Of course, I won't tell them that it's my blog. I also had to write it to remind myself that's not the material but the invisible that determines true happiness.


The Worshiper's Diary- Alpha and Omega

F Bernard Harris | MySpace Video

Friday 23 July 2010

Ayo game using scala actors


Disclaimer:
This post is an introduction to Scala Actors. It's written in such a way that you'll need very little knowledge of scala and you don't really have to know how to play the game 'ayo' either (a variation of this game is also called mancala - I think). But if you want to know more about the game before starting visit this bbc post. If you want an introduction to scala, you are better off visiting scala language's home page




First off, the scala eclipse ide (for scala 2.8) has taken a HUGE leap forward since the last time I tried it. It's now good enough, I think, to be used in place of intellij's idea - the only thing I have against idea is that it is plain ugly. Even NetBeans looks like a pretty bride in mini-skirt beside it. In any case let's get serious...


Actors are scala response to multi-threaded programming. I hear they were borrowed from haskell or some other fancy language but if that's why you are here... there's an... a google for it. The cool thing about Actors is that it takes away the abstraction of threads and leaves you with fancy-dandy sending and receiving of messages.


package actor {

import scala.actors.Actor
import Actor._


The first thing to note about actors is the classes to import. Basically you import scala.actor.Actor and then you import the methods and classes within the 'Actor' class itself.


//messages to be sent to Actors
case object StartGame
case class Play(board:List[Int])
case class Stoned(count:Int)
case class Move(startAt:Int, player:Player)
case class GameOver(message:String)


Next, we declare the messages that we'll be sending to and from Actors. The usual thing is to use case classes/objects as Messages because the plumbing for pattern matching is built into them (Any class - string, int, etc - that supports case matching would work though). So we've got, for instance, 'StartGame' that represents a message we'll be sending to Referee to start the game. The others should be easy to guess. Alternatively, we might talk about them later.


class Player(val name:String) extends Actor{
var stones = 0

def act = loop {
react {
case Play(board) => reply(Move(getUserInput(board), this))
case Stoned(count) => stones += count
}
}

def getUserInput(board:List[Int]):Int = {
val choices = board.zipWithIndex.filter(_._1 != 0).map(_._2)
print(
"Player "+name+"'s choice "+choices.toString+": ")
val choice = readInt

//verify that the user hasn't select a 'zero' hole
if(!choices.filter(_ == choice).isEmpty)
choice
else{
println(
"Invalid choice :(")
getUserInput(board)
}
}
}


This perhaps is where the real fun begins - We define a Player. A player is used to getting user input and then sending a message to the Referee containing the player's input. The player would of course only get input when a Referee demands it. To turn a player into an Actor we simply extend 'Actor'. The next thing is that we have to write an 'act' function. In the 'act' function you would need a loop of some sort so that the 'Actor' doesn't quit listening after processing just one message. Fortunately, there's a built in 'loop' construct (note, I'm saying construct because I don't know if it's a function or an object or some other voodoo scala thingy) that we can use. In side the 'loop' you define a 'react' function with matching cases for each message that you want the actor to handle. In 'Player' we want to handle messages to increase the number of stones that the player has captured (Stoned) and we want the 'Player' to know when to make a choice on where to start his next move from (Play).


'reply' is a function that sends a response back to the Sender of a message. So we don't really have to know who sent the message, we can simply process the message and use the 'reply' function to send a response back to the whoever sent the message in the first place. For instance, when the 'Player' gets a 'Play' message from the 'Ref' the 'Player' simply calls 'reply(Move(getUserInput(board), ...))' so that a 'Move' message, containing the player's choice, is sent back to the 'Ref'.


As an aside, you'll notice that there's a 'getUserInput' function that actually does the job of getting the user's input. This method receives a list of stones corresponding to holes that the user currently owns (no pun intended). Basically, we keep requesting for the user's input until the user enters a choice that's valid. This way we get players that are honest to a fault - no Thierry Henry scoring hand goals or Luis Suarez' stopping clear cut goals with their hands - and apparently we can have even Graham Poll as the ref since we've removed the risk of a player receiving 1, talk less of 3, yellow cards.


Below we have the Referee or 'Ref'. I'll include comments within the class definition (remember that you can hide all the other with that link above) just so I don't lose my mind before this is all done.


object Ref extends Actor {
import scala.collection.mutable.Buffer
var players:List[Player] = List(
new Player("one"), new Player("two")) //always assumes 2 players
var holes:Buffer[(Int, Player)] = {0 to 11}.map(i => (4, players((i/6).toInt))).toList.toBuffer //always assume players have half the board to start with



We declare/define the total number of players as well as the holes (no pun intended... again!) here. Nothing special there, move along.



def act = {
loop {
react {
case Move(startAt, player) => { //TODO logic to carry out actual move and to keep score
move(startAt, -1, player)
val p = nextPlayer(player)
p ! Play(board(p))
}
case StartGame => {
players.foreach(_.start)
val p = nextPlayer(
null)
p ! Play(board(p))
}
case GameOver(s) => exit
}
}
}




So, the Ref is also an 'Actor'. We got that by having the 'Ref' extend an 'Actor'. This also means the 'Ref' has to have an 'act' method which we have duely done above. The most important thing here is that the Ref can respond to three messages - StartGame (which the application itself sends to it), GameOver - which the Ref sends to himself (ahhh... the thought of female referes), and Move - which is the response a player sends to the 'Ref' after you, the end user, have made up your mind what hole to start your move from.


It's easy at this point to see that this game is not complete yet. What's important is to keep the goal before your eyes and not succomb to the whole moving target chirade. In our case, the goal is understanding 'Actor' concept. Having a complete game is a side effect - a bonus at best! That said, the game - work!, it should! But I digress... again

    
/****
*
@params stonesLeft -1 means start counting, 0 means stop counting, > 0 means keep counting
****/

def move(startAt:Int, stonesLeft:Int, player:Player){
val hole = holes(startAt)
val next = (startAt + 1)%12

if(stonesLeft == 0){
var start =
if(startAt - 1 < 0) 11 else startAt - 1
var h = holes(start)

if(h._1 == 1)
Unit
else if(h._1 == 4){
player ! Stoned(4)
holes(start) = (0, player)
}
else
move(start, -1, player)
}
else if(stonesLeft == -1){
val stones = hole._1
holes(startAt) = (0, hole._2)
move(next, stones, player)
}
else{
holes(startAt) = (hole._1 + 1, hole._2)
move(next, stonesLeft - 1, player)
}
}



As far as the game is concerned, 'move' is the most important function of the game. It contains logic about carrying out the counting of the game, and capturing houses or holes (now that I think about it houses is a better word that holes but the damage is done).

stonesLeft is actually the number stones the player currently has in his/her hands. So if the user has 0 stones in her hands it's time to take stock

  • if where she stopped has 4 stones then she captures that hole as well as the 4 stones in that hole

  • if where she stopped had no stones then her move is complete and the next player gets to play

  • if where she stopped has neither 0 nor 4 stones then she pick up the stones from where she stopped and starting another round of counting


Actually, the explanation above is a little misleading because it's the Ref that does the counting on behalf of the 'Player'. The 'Ref' can send messages back to the Player like 'Stoned(4)' to tell the player that she has captured 4 more stones.



def nextPlayer(player:Player) = {
draw
scores
players.filter(_ != player)(0)
}

//board is a list of user stones in holes i.e. if the user doesn't own a hole, zero stones appear
def board(player:Player):List[Int] = holes.toList.map(x => { if(x._2 == player && x._1 != 0) x._1 else 0 })

def draw(){
holes.takeRight(6).reverse.foreach(hole => print(hole._1 +
"\t"))
println
holes.take(6).foreach(hole => print(hole._1 +
"\t"))
println
}

def scores(){
players.foreach(p => println(
"Player "+p.name+" has "+p.stones+" stones"))
println
}
}

object Ayo
extends Application{
Ref.start
Ref ! StartGame
}
}



One of the most important bits to the whole 'Actor' magic is the 'start' function call. You've got to call start on an 'Actor' in order to get the party rolling. In the main class 'Ayo' above we not only start the 'Ref' actor using 'start' but we also send a 'StartGame' message to the 'Ref' actor. When the 'Ref' receives the 'StartGame' message, it in turn calls 'start' on all the 'Player' instances and then sends a 'Play' message to the first Player instance. All in all, Actors sure bits Threads in java. One last thing though, all the messages we've been sending were done using the ! (bang). Messages sent in this way are asynchronous. In other words, the 'Ref' doesn't block for a response from the 'Player'. Instead he continues doing whatever else he can do (in our case mostly nothing) until he receives the response from the 'Player'. To send synchronous messages use !? instead of !.




And that's all folks.

I'm convinced that I should create a second blog for posts like this but I'm too lazy to maintain 2 blogs. Time (and hits - anything more than 5 hits and I'm creating a new blog) would tell - this here blog is kind of sacred to me, you know ;)

Saturday 10 July 2010

brides, bride-grooms, 42s

"A man can receive nothing, except it be given him from heaven" John 3:27

"He that hath the bride is the bridge-groom". I'm at the point in my life where I'm trying to clearly identify what life is all about. I'm pretty sure now that 42 isn't the answer but then again it's not that far off. Today, I read John 3 - you know that Nicodemus chapter but not really the Nicodemus part. Down the chapter we find John's disciples talking to him about Jesus and the baptisms that Jesus was "supervising" over on the other side of the Jordan. And John uttered the immortal phrase "A man can receive nothing, except it be given him from heaven". He goes on to say some other cool stuff including the sentence "He must increase, and I must decrease". That's what life is really about - decreasing while God increases. There are different ways we'll get there and different ways we'll touch those around us while traveling there - some of my friends are already PhD holders, some are public health workers and some don't know what they are meant to be in life - but the destination is the same - "All of God and none of us".

John doesn't say a lot in the cause of the entire new testament but I'd like to have a bunk next to him in heaven. I get the feeling he'll sound just like my grandfather - Really smart/wise yet gentle and patient - I love them, both.

This last week wasn't bad by any means. Work has slowed down lately - it feels like when I first started working there as an intern. It's given me time to try and learn new stuff. I'm working on my next liftweb based website already and I'm planning when that's done to submerge myself in as much bio-informatic material as I can find on book24/7. The GC too is ended - woohoo - and with it it's many distractions. Yes, I ended up greeting all those naija dudes I was hoping to avoid and no, for some strange reason, they all didn't act surprise that the little boy in pants they once knew is now taller than the bulk of them. Or maybe they did but the amount of mental preparation for the onslaught over compensated for whatever discomfort such remarks would have otherwise caused.

Honestly, though, the GC was fun. There was family around especially since it coincided with July 4 and there were all these kids running around my place. I wish all weekends were like that (minus the financial aspect, of course).

I've been thinking about starting a second blog for programming, IT related posts. This here blog was meant to be that but it has somehow morphed into more of a personal, thank-God-it's-friday, soul searching blog. The thing is I'll have to blog the IT stuff under another name so I can get my friends to read up stuff there. Alternatively, I could come out of my shell here and just put IT stuff here too. Of course it would mean occasionally pissing off one audience of readers but it could actually end up breding a 3rd type of audience. Funny, though, cause right now the only audience is me... and perhaps a google spider or bot or whatever they call them these days.

Last time I ended up asking about forgiveness and what it really means. I think that eternity beats any other benefit. Sometimes, I bother too much with technicalities - curse of my trade perhaps. What I'm trying to say is that what really matters is that God gives me another chance in the eternal scheme of things. In the physical realm, I think life is more of an art than a science and that God is constantly having to retouch some part of the picture daily because of my foibles. Sometimes, he just completely dabs out the blot I made with the right about of color, and sometimes he lets time take care of the blot. Sometimes, I still suffer the physical consequence of my sins yet sometimes, in His mercy and infinite wisdom, he takes away the consequence of my failures so that the eternal goal won't be missed.

I wish I trusted God more each day. Life would be so much more fun that way - each day becomes one really cool roller coaster ride where you don't know where the next turn leads but you know that you'll get home safe. Now who won't want that kind of life!

Friday 2 July 2010

No long thing!

Not a long one today. My N900 is going in for repairs and typing on an
iPhone just isn't as much fun. I'll say this though:

1. I can't honestly count things I've been faithful to the way I've
been to this blog this few months. Apart from the phone, knowing that
nobody reads it or that the ones that read don't know it is I is
certainly a blessing there (I'm so shy I will quit the day I am outted).

2. It sure is easier to type on the N900 than on an iPhone. My first
ever phone was a Nokia 3300: big, ugly but efficient. My N900 fits
that bill too but for one thing - it's not just a phone to me where as
I still classify my jailbroken, unlocked iPhone as.... just a phone.

3. Forgiveness is a beautiful thing. What intrigues me is how when He
forgives He also forgets. As in no more remembrance of your sins
whatsoever. In fact they never happened as far as as He is concerned.
One of these days the measure of the Holy Ghost in me will make it
possible for me to actually forget the wrongs committed against me (I
plan to make heaven after all).

Wait a minute sef... I don't get it. Or, I get parts of it but the
parts I understand only make understanding the whole harder. Like
since God is omniscient he must know all things - past, present and
future, then forgetting is either a conscious choice on his part or
forgetting means even though he remembers he doesn't count it against
me (the latter isn't really forgetting now is it?). Add Karma (or
you'll reap what you sow) into the mix and things get a little more
confusing...

So apart from been absolved of the "eternal" consequences of my sins
(and the apparent fringe benefits of the aforementioned absolution),
what exactly is forgiveness?