Archive for the 'Factor' Category

Forth brain injections for free

Sunday, June 22nd, 2008

The X Factor album coverImage via WikipediaForth is “a little” different than you everyday programming language. Basically it’s different on so many levels. If you ever wondered what it’s like, but didn’t have time or a reason to invest to get/learn/do-enought-to-get-the-feel it — now internets offer you a free ticket to get a taste of it.

Samuel from Falvotech made a video where he coded a HTML parser in gForth, he seems a very fluent Forth programmer and it’s a joy to watch him code (to me at least):

Click here for article and here for the video

I do some coding in Factor which is one of incarnations of Forth but it’s still different in many ways and was always interested how his raw-metal father Forth is. This video was super interesting to me. The core rules are very known to me as it’s the same as in Factor so I could easily follow it, I hope it will be approximately the same for you.

I hope you won’t focus only on “uh how low level is this” and “what crazy stuff is he doing with return stack?” (in few positions of the video) and so miss some things that are pure beauty of the concepts here (like what concatenation brings you). Well, your loss if you do..

— HIGHLY OT —

Hehe, this is above AI, this is AT (Artificial Transcendence).. I just installed the new Zemanta plugin, I am listening to Iron Maiden while writing this article and mentioned it no-where in it and it proposed me picture of Iron Maiden (that you see upthere) in suggestions.

Well it’s even worse, this is future look-up and self inducing prophecy by the plug-in. I was listening to the iron maiden and not mentioning them, it proposed the pic to me, now I did mention them so the picture has a logical place to be in this article and global order is restored once again. I am impressed by the new version guys ;)

Zemanta Pixie

Robi unhide!

Sunday, May 18th, 2008

I always think that programmers that hide what they code because someone is gonna steal it! People are evil!* aren’t totally up to date. But I was hiding this too… I was hiding it for long time and I felt stupid every time when I thought, “uh I mustn’t write the name! I must write ’something I am making’ or ‘project xxx’ or ..”.

I was hiding that I am making “Ask Robi”, yes ASK ROBI. I couldn’t dare to say ASK ROBI because I was amazed that for once the primary domain I want is not yet taken and askrobi.com wasn’t. So yesterday when I made the askrobi do a fullround client server trip, I bought the domain and now I can nag you about it! This is the first ugly screenshot of the client :)

askrobi screenshot

Client is made with Adobe AIR and server and brains with Factor. They communicate via STRPC. I will post more about it soon.

Not that I don’t think that no-one can steal ideas. Just look at casual games market. The thing is that most people think their ideas are cool and ideas of others well aren’t so why steal them. There are also exceptions but if someone is so stupid that it has to steal ideas I feel sorry for him anyway.

Many ways to skin a FAt CaT OR …

Sunday, April 27th, 2008

Some people go to church on Sundays and some find time to divulge to the non-urgent ie. play with Factor a little. Well, I am not playing, I started making something concrete in Factor. Something that was cooking in my head for 6 whole years, and if no one made it in this time I might just step out and do it.

Well the concept is still not fully cooked, but I decided it’s time to stop boiling it and put it out on a pan and burn it a little and see what we get. So today I made few more words and a bunch of unit tests for it. Ok, so I am coding a very concrete thing in Factor for the first time. I like when I see that the Forth philosophy about re-FACTORing (yeah, hence the name I think) words to smaller and smaller independent words showed it’s beauty.

My main words with very rich functionality basically needed very very little stack shuffling ( just a dup, swap or over here and there - and I didn’t use cleave combinators) but then I came to one very simple utility word that because of few specifics used them noticeably more.

It was no problem to write but because Factor is so versatile I was sure there are other more elegant solutions. So I pasted that word on Factor’s paste and asked on IRC and so far I have 4 solutions that used very different concepts each, so I think it’s an interesting example of Factor.

This is my original, made in the “primitive” way with just stack shuffling… The code takes assoc. array, extracts/processes few values from it and leaves 3 values on stack. Basically very simple unimportant stuff…

: unpack-answer-to-me ( array -- type_subj answer id )
    "type" over at "subject" pick at " " prepend append
    "answer" pick at
    "id" roll at ;

#! Example of use..
{ { "type" "typ" } { "subject" "subj" } { "id" 12 } { "answer" "ans" } }
unpack-answer-to-me .s
! returns
"typ subj"
"ans"
12

Itvar on IRC came to this solution, he used locals. I think it’s much more readable than mine..

:: unpack-answer-to-me ( array -- type_subj answer id )
  "type" array at
  "subject" array at " " prepend append
  "answer" array at
  "id" array at ;

Then Itvar made a word using the new cleave combinators. Cleave combinators are new “invention” (I think) in stack based world and I didn’t see them in action until this. This code looks very good to me and nicely reflects the logical structure of code.. The cleave stuff is that bi and tri

: rev-at swap at ;

: cleave-unpack-answer ( array -- type_subj answer id )
  [ [ "type" rev-at ] [ "subject" rev-at ] bi " " prepend append ]
  [ "answer" rev-at ]
  [ "id" rev-at ] tri ;

Before Itvar showed this code I was thinking that I could make some sort of “extract” combinator myself that could be useful in all such cases. I made modify-values combinator before and really liked what it allowed. Well it was too interesting to-not-do it so I made it and this is the usage..

: unpack-answer-to-me ( array -- type_subj answer id )
    { { { "type" "subject" } [ " " prepend append ] }
      { { "answer" } [ ] }
      { { "id" } [ ] }
    } extract-values+ ;

Well I am biased and I don’t know Factor well yet, but I like this solution the most :) . Cleave is cool too. I will post the code for this and some other combinators soon-ish. This post is getting too long already.

update

johnnowak , a guy who is making his own stack based language Fifth (5th — website should be coming soon) came up with yet another solution. I like this one because it creates/uses 2 very general purpose and useful combinators to get a very straightforward solution.

: 2dip -rot 2slip ;

: extract swap [ at ] curry each ;

: unpack-extract
  { "type" "subject" "answer" "id" } extract
  [ " " prepend append ] 2dip
;

Here is more readable formatted code for all 5 examples.

NullCMS - my first webdev test in Factor

Sunday, April 20th, 2008

I didn’t have real time to further experiment with Factor for the last month or more. I only took one Sunday weeks back and wrote a really mini mini CMS like thing to see my still very crude and young web-dev Factor libs in action for the first time.

Factor has it’s own webdev framework/libraries (and you should check it out), but I am experimenting a little here so I am making a sort of lower level and minimalistic libraries of my own. I don’t have time to install a VPS with Factor currently so I just made a quick video of it and posted the code at Factor’s paste. Here is the video..

And here is the Factor code + HTML templates.

It consists of roughly 27 lines of Factor code, 14 lines of HTML template code and 19 lines of JavaScript. For a cms to be somewhat useful I would need to add some user authentication also. Basically you can display, create, edit and delete pages and you get a flat menu for navigating them.

Factor & shit, I am that naive sucker

Friday, March 14th, 2008

Factor gets a lot of bashing at reddit. That is not that bad because traction always produces more energy and output than a soapy you are OK, I am OK, we are all OK stuff.

Few days back certain Factor non-fan amongst many other things wrote:

Because such Forth-like untyped languages have been around for decades and I was using them around 1990. Adapting the concept to 2008 may be fun for you and help you suck in a few naive developers but it doesn’t make it a serious platform; read up a little

While people on factor’s irc channel look highly educated programmers to me I recognised myself in this. Not that I am proud, but reality is I am sort of naive developer and I did get sucked into Factor. I am not formally educated as a programmer. I am more like the guy who once jumped into the river and managed not to sink, and then just stayed there, gradually improving his swimming, but never took time to learn the official butterfly stroke because he was too busy catching the fish.

Well I am “naive developer” in some way but I am also very pragmatic because the “useful working maintainable output” vs. the “sh*t that went into making it” is the only thing that matters to me at the end. I don’t code to learn, I learn to code.

And that’s why I now think that Factor will be my web-dev tool? Because I have tested and got very good results with 3 sides of web-development so far:

As my first Factor lines of code I tried to see how I could extract some data out of text. This simple and small vocab is what I came up with, and was amazed by cleanness and obviousness. So I see that making nice vocabularies that will help me parse strings will reall NOT be a problem with factor.

"i:1;name:Jon;age:3;surname:Wu;;i:2;name:Jill;age:9;surname:Huan;;"
      "i:2" cut-to "name:" cut-off ";" snatch-to
      "surname:" cut-off ";" get-to .s
! "Jill"
! "Huan"

So factor can read, but can it generate? Next, I wanted to se how I can generate SQL, this came out so far:

: get-users ( -- )
   SELECT*
      "users" FROM
      "enabled = 1" WHERE
      "username" ORDER get-rows ;

get-users
! SELECT *  FROM users
! WHERE enabled = 1  ORDER BY username ;

: get-users-of-group+ ( orderby group fields -- )
   SELECT
      "users" FROM
      swap "enabled = 1 AND group = ##" ##1' WHERE
      swap ORDER ;

"email" 51 "email, username" get-users-of-group+
! SELECT email, username  FROM users
! WHERE enabled = 1 AND group = 51  ORDER BY email ;

: change-email ( id email -- )
   "users" UPDATE
      "email = ##" ##1' SET
      swap WHERE-id exec ;

123 "j@a.com" change-email
! UPDATE users  SET email = 'j@a.com'
! WHERE id = 123 ;

Then I tried to make the XHTML forms generating vocabulary:

: show_user-form ( -- )
   "post" "./user" start-form
      "your info" start-fieldset
         "Email" label (*) endlbl
            "email" "" "class='big'" text-input endrow
         "Name" label  endlbl
            "name" "" "size='12'" text-input endrow
         no-label endlbl
            "action_add-user" "Save" "" submit-input endrow
      end-fieldset
   end-form show! ;

The code creates the first form on this page.

Any part of my web-dev toolkit I tried to create in Factor so far was better looking than the ones I had developed in other languages. That’s why I will continue with Factor. I know and used only the “primitive” stuff of Factor until now so I am excited to see what’s ahead.

I just got smarter by a Factor!

Wednesday, February 13th, 2008

I finally took some time to play with Factor. I approached it with some scepticism because after looking at various examples I wasn’t sure if I really want to shuffle the stack instead of use the ( old boring ) variables.

This is an much repeated example of sq (square) word (function) in Factor.

: sq dup * ;

Well, I came with scepticism but left enthusiastic. When I was thinking out cutoff word I thought to myself that factor is cool as a brain exercise, like tetris, but that it’s probably more practical to just use something less exciting where you don’t have to think so much to write a simple function. At 3-rd word getto code sort of fell together in few seconds and I tried it and it even worked. From then on things just got nicer and nicer (see the source file below).

: cutto ( sub str1 -- str2 ) dup >r dup length >r start r> r> subseq ;
: cutoff ( sub str1 -- str2 ) dupd cutto dup >r length swap length swap r> subseq ;
: getto ( sub str1 -- str2 ) tuck start 0 swap rot subseq ;
: cut-to swap cutto ;
: cut-off swap cutoff ;
: get-to swap getto ;
: snatch-to dupd get-to swap ;

What I want to do here? I want to prepare few words that will help me extract stuff out of a string in a reasonably elegant way.

( scratchpad ) "<A c=123&..><B c=234&..><C c=345&..><D c=456&..><E c=567&..>"
 			"<B" cut-to "c=" cut-off "&" snatch-to
 			"<D" cut-to "c=" cut-off "&" get-to .s
"234"
"456"

I don’t know, but this seems very elegant to me.

click to see the full progress, comments and usage examples of these words

After I posted it on IRC channel #concatenative elasticdog quickly posted a lot more elegant variations of my tree base words:

: cutto tuck start tail ;
: cutoff swap split1 nip ;
: getto swap split1 drop ;

Beauty!!!