Mostrando entradas con la etiqueta Io. Mostrar todas las entradas
Mostrando entradas con la etiqueta Io. Mostrar todas las entradas

miércoles, 25 de agosto de 2010

Using AUTOLOAD to build html code

Last week I found a new blog about Io. It's been a long time since I used Io, but maybe this blog will make me look at Io Language again.


In his last post, Dennis Ferron comments on _why's web server yown written in Io. One thing he comments is the slot 'forward'.

As he says, forward is a kind of catch that gets called when a message has been sent to an object and that object didn't have a selector with that name. _why exploits this feature to write an html builder based on that.

What he doesn't mention (I think he's leaving this for another post) is about lazyness. The way _why wrote forward method.

If you define a method without arguments but you call it with some, you can still access them, but with the difference that they won't be evaluated by default. That's called lazyness, and allows you to define the evaluation order of things.

So in the code

html(
title("O HAI")
strong("KTHXBYE")
)


forward will trap html message before title or strong.

In Perl, there's a 'lightning rod' function too, called AUTOLOAD. You can do pretty much the same, but the difference is that perl will ALWAYS evaluate parameters sent to a function before calling it, so the same code would trigger AUTOLOAD on the 'title' call, then 'strong', and last, 'html' and you have to fill the string from the inner tag to the most generic one.

Here's a gist code that emulates Yown Builder.io in Perl 5.




Good luck Dennis with your new blog!

--
raig

jueves, 14 de mayo de 2009

Explore Io's internals

I haven't touched Io in some time, but about a month ago, I came with this piece of Io code, to explore the Io written methods in default Io installation.

For example, to explore Number blocks (all are written in Io, CFunctions aren't), you just have to type this in Io REPL.

Number slotNames foreach(i,
if(Number getSlot(i) type == "Block" ,
(i .. " -> " .. Number getSlot(i) code) println))

miércoles, 11 de marzo de 2009

installing SGML addon for Io and using it to get latest xkcd image

Io can be a PITA to install. SGML addon is an example of it.

things to try:
- install libsgml (from http://www.hick.org/code/skape/libsgml/ )
- cd /usr/local/lib
ln -s sgml sgml-1.1.4

- cp sgml/include/Variant.h sgml/Variant.h

- cd steved..../addons/SGML/
ln -s sgml sgml-1.1.4

It doesn't work unless you run io from /usr/local/lib
^ This is only a draft, tomorrow I'll clean it... sure.....
------

Ok, say you already have SGML installed along with your Io instalation.

Today's toy problem is HTML parsing. I thought I'd do something web related (like my last squeaky google fight) . Well, This is the result. a Tiny script to get latest xkcd png and put it as a wallpaper using feh. The code has too many hardcoded things, but, you know... it's only a demo, right?
#!/usr/local/bin/io_static
# updates wallpaper with latest xkcd image
#
# kidd
#

XKCD := Object clone do (
      url := "http://www.xkcd.com"
      fetch := method(
         html := URL with(url) fetch asString asHTML
         urlImage := html elementsWithNameAndClass("div", "s") at(2) elementsWithName("h3") at(1) allText split(": ") at(1)
         File with("/tmp/sc.png") setContents(URL with(urlImage) fetch)
         return ("/tmp/sc.png")
     )
)

SGML
xkcd := XKCD clone
System system("feh --bg-scale " .. xkcd fetch .." ")

Let's hope next Io post will use some of Io's cool features, not just 'standard' code

Seeya!

miércoles, 28 de enero de 2009

Net simulator toy problem in Io

Another little test in Io language.

Packets are generated in a node with a content and a destination. Nodes are linked in a ring and passOn packages unless the package is for themselves.

Next versions to come, with subclassed nodes and more tests.

Syntax highlighted version in http://pastie.org/373095

Packet := Object clone do(
content := nil
destinatary := nil
)

Node := Object clone do (
id ::= nil
next ::= nil
generate := method(content, dest,
a := Packet clone
a content = content
a destinatary = dest
#a destinatary println
passOn(a)
)

receive := method(packet,
("I'm ".. self id .. ". The packet goes to".. packet destinatary .. "") println
if(packet destinatary == self id,
("I've received a package with the message:".. packet content .. "") println ,
#if (packet content asString containsSeq("sleep") and self id == 4, wait(2)) #nonblocking? anyone?
passOn(packet)))

passOn := method(packet,
#self id asString println
if(self next != nil, r:= self next receive(packet), "WTF?! I've no next node!" println))
)

lista := List clone
lista append (Node clone do (setId(0)))

for(i,100,1,-1,
lista append(Node clone do(setId(i) setNext(lista at(lista size -1)))))

lista at(0) next = lista at(lista size-1)
l1 := lista at(0) generate("matat1",45)
#l2 := lista at(0) generate("matatsleep",65)
#l3 := lista at(0) generate("matat3",20)
Future Work is Handling packages in a thread (and learning how to use futures), to be able to block some packages in certain nodes but be able to continue passing nodes along while processing (blocking) a given package.

That would allow having many packages navigating the 'net'.

jueves, 25 de diciembre de 2008

Io language, a consistent scripting language

Some days ago I discovered a new language called Io (that's capital 'i' and 'o'). It's inspired by smalltalk, but with few features of other languages like self, lisp or lua. FYI, I discovered it among other cool languages. There are even more esoteric languages here.

First thing to say about Io is it's minimalist. Really minimalist. Only look at it's site to see it. As a minimalist language, its doc is also minimalist, so you have to understand the little doc fully, and I mean *really* understand. Io is a Prototype-based language with a syntax similar to smalltalk, but more minimal (assignation is a message too). Its introspection is astonishing aswell, and it introduces a concept of context which I'm still not very confortable with.

Anyway, I've already written my first program (a typical guessing number game) for Io. Here's the code.

guesser := Object clone
guesser toGuess := Random value(100) ceil
file := File standardInput
guess := file readLine asNumber
while(guess != guesser toGuess,
   if(guess < guesser toGuess,
     "higher" println,
     "lower" println)
   guess = file readLine asNumber
)
"ok, el numero es " print
guesser toGuess println


Ah! comunity is very friendly and willing to help (you can find them at #io @freenode