Idiot's 3 minute solutions for cooling down the reactors in Japan

In AltMe chatroom they were talking about the situation in Japan:

Person A: Seems simple enough to me. Wanna cool a nuclear plant? Throw water on it

Person B: And the heavier it is the better?

Person C: It's interesting to me that we don't have anything better or more efficient to actually throw the water with.

Person B: Perhaps liquid helium is not available in large enough amounts?

Me: The height of our hi-tech is low.

Me: I don't get it. Can't they bring in like 10 remote controlled fire-trucks that would spray water and go out for refiling while next one comes? (they made ad-hoc remote controlled cars in myth busters so it can't be that hard)

Me: and don't japanese have all kinds of robots? (I know radiation can screw electronics, but it seems it's not that hard, those chopers and fire-trucks that they use now surely rely on electronics too)

Me: or just construct the pipes and pumps that would use seawater near to constantly water it? They transport oil across continents in pipes, so no matter how far the water is (and the sea is near), they sould be able to get a constant suply in there without any high tech.

Me: or probably 100 other things that seem better than driving in with human controlled firetrucks and flying over with a helicopter pouring water on it.

It is unimaginable to me what people of Japan are enduring right now. All power and luck to them. I called this an idiot's solutions because there must be something I don't understand here. I know this is a serrious thing. But I am rather a huge idiot than keep these very simple and obvious things for myself.

 

 

While we are at idiot's ideas about nuclear: Why don't they build nuclear reactors in big holes surrounded by concrete so even if exposure happens radiation can only go Up in the fucking sky (we know enough mass (concrete, soil stops radiation)), and if the need to cool it they just need to fill the hole with water (and refill as its boiling away)?

(I know this doesn't solve all problems with radiations (like particles that get airborne) but still)

To person A, B and C.. by idiot I meant Me (just to be clear).

 

RT Webcamp Ljubljana je bil kul

Hekovnik (t.i. hackerspace kjer delam) in Ljubljanska Barcamp/Webcamp ekipa (@gandalfar) so organizirali Real Time Webcamp. Kot zgleden udeleženec campa sem tudi jaz pripravil predavanje. In sicer z naslovom: "Programiranje strežnikov za real-time internet", kjer sem predstavil kako se spiše 3 minimalne "custom" TCP strežnike. V treh različnih paradigmah (in jezikih). Uporabil sem Node.js ki uporablja "events/callbacks" pristop, jezik Lua kjer sem uporabil "corotines" in Erlang z "Actors/Message passing".

Na žalost o drugih predavanjih ne morem povedat veliko, ker sem zamudil večino webcampa. V celoti sem si ogledal le improvizirano predavanje @b0j3 o Appcelerator platformi, ki zaradi tega ni bilo nič manj zanimivo. Zamudil sem, ker sem hkrati ko sem pisal slajde še programiral storitev qwikislide v kateri sem te slajde delal.

Lahko pa povem nekaj o splošnem vzdušju in after-camp občutku. Bilo je super!! Vsaj meni se je zdelo veliko bolj domače in sproščeno kot npr. zadnjič. Razlog vidim v tem, da je bil ta camp manjši (le 50 možnih obiskovalcev proti 100 na prejšnjem) in ne-fensi DIY atmosfera hekovnika je oddajala veliko bolj "cosy" vzdušje kot nove, sodobne a sterilne predavalnice v novem TP-ju.

Del čarovnije je bil tudi, da so z manjšim številom udeležencev posamezniki prišli do izraza in tu moram omenit ekipo @preona z doma pečenimi čokoladnimi cookiji (ki so njihov logo), in prav tako improviziranim "funny videos" projektorjem na hodniku. Zakon!

(qwiki)slide mojega predavanja najdete s klikom tukaj.

Vso kodo serverjev in demo-odjemalca pa na githubu (vem tipično).

Nekaj o webcampu je napisal tudi moj Hekovnik sosed Swizec, videi pa so se že začeli pojavljati na kiberpipinem arhivu.

Filed under  //   realtime   talk   webcamp  

Comparing Rebol and "The Power of Lisp Macros"

An interesting web-page THE POWER OF LISP MACROS appeared on Reddit and Hacker News last week. It was written by Dr. Edmund Weitz. Of course I knew for Lisp macros and their "magic" for a long time, but I rarely saw their better use cases, so I couldn't compare them to anything. This article finally gave a chance for that.

Rebol does not have macros (nor a compilation step), but it has very flexible and reflexive run-time capabilities. Code is data / Data is code holds as much for Rebol as it possibly can. In this post, I want to see, how a Rebol doing the same as Lisp macros would look like.

my-if

One macros use-case that I've heard of many times is the "if". Lisp needs macros to be able to have if, because otherwise it would evaluate true and false blocks. And this is the first case Dr. Weitz shows:

Lisp:

(defmacro my-if (test-form then-form else-form)
  ;; in "reality" IF is a "special operator" and COND is a macro
  (list 'cond
        (list test-form then-form)
        (list t else-form)))

Because of the way Rebol blocks are evaluated or reduced. Rebol doesn't need macros for this. If is an function like any other.

Rebol:

my-if: func [ cond then-form else-form ] 
            [ do either cond [ then-form ][ else-form ] ]

BrianH on Rebol chat proposed another solution with eiher:

my-if: func [ cond then-form else-form ] 
            [ either :cond :then-form :else-form ]

He also showed what he thinks is most effective and robust (with types) solution that uses only basic functions of Rebol:

either: func
    [[throw] cond then-form [block!] else-form [block!]]
    [ do get pick [else-form then-form] not :cond]

with

Dr. Weitz continued with the with- convention. This basically intrigued me to write this post, because the with-* convention is one of things I really like that I can do with Rebol. I learned about it while programming in Factor.

Lisp:

;;; Wrap a body of code with a prologue
;;; and a (guaranteed) happy ending

(defun begin-transaction ()
  (format t "Starting transaction~%"))

(defun commit-transaction ()
  (format t "Committing transaction~%"))

(defun abort-transaction ()
  (format t "Aborting transaction~%"))

(defmacro with-transaction (&body body)
  `(let (done) ; variable capture - see below
     (unwind-protect
         (prog2
             (begin-transaction)
             (progn ,@body)
           (setq done t))
       (if done
         (commit-transaction)
         (abort-transaction)))))


I think the Rebol version of this is very elegant:

Rebol:

begin-transaction: does [ print "Starting transaction" ]

commit-transaction: does [ print "Committing transaction" ]

abort-transaction: does [ print "Aborting transaction" ]

with-transaction: func [ code ] [
    begin-transaction
    either error? try [ do code ] 
        [ abort-transaction ] [ commit-transaction ]
]

We don't use any variable "done" as Lisp uses it in our with-transaction function, so the next chapter "Ensuring discretion" isn't applicable here. Rebol has many ways to limit the effects of words inside a block to it's outside.


Unit test framework

This is a little larger example. I steered away of somewhat following the example in Rebol and just wrote what I would write to do the same. If my code doesn't do something that the author's code does please let me know.

Lisp:

(defvar *test-thunks* (make-hash-table)) ; <http://en.wikipedia.org/wiki/Thunk>
(defvar *test-sources* (make-hash-table))

(defmacro define-test (name (&optional condition) &body body)
  (with-unique-names (c)
    `(setf (gethash ',name *test-sources*) '(progn ,@body)
           (gethash ',name *test-thunks*)
           (lambda ()
             (handler-case
                 (format t "Test ~A ~:[FAILED~;passed~].~%"
                         ',name (progn ,@body))
               ,@(when condition
                   `((,condition () (format t "Test ~A passed.~%"
                                            ',name))))
               (error (,c)
                 (format t "Test ~A FAILED, ~
                              condition ~S was signalled.~%"
                         ',name ,c)))))))

(defun run-tests ()
  (dolist (test-name (sort (loop for name being the hash-keys of *test-thunks*
                                 collect name)
                           #'string-lessp))
    (format t "~%~%Starting test ~A...~%" test-name)
    (let ((*print-pretty* t))
      (format t "~S~%~%" (gethash test-name *test-sources*)))
    (funcall (gethash test-name *test-thunks*)))
  (values))

; let's do some testing

(define-test simple-plus-test ()
  (= (+ 1 1) 2))

(define-test wake-up-after-sleep ()
  (sleep 1) t)

(define-test division-by-zero (division-by-zero)
  (let ((a 42) (b 41))
    (incf b)
    (/ 1 (- a b))))

(define-test unknown-file ()
  ;; this is a Windows laptop...
  (open "/etc/passwd"))

(run-tests)

Why I steered away?

  • I don't want to use globals for this
  • I don't like (imperative) feel of: first *setup-state*, then *run*
  • Lisp example must return true/false/exception for correct/wrong/wrong . I prefer unit test to return result/exception where result is compared to a expected value. So you can show "Test A FAILED. expected 100 got: 101" in case of a failure.


Rebol:

run-tests: func [ ts /local str res err exp ] [ 
    forskip ts 3 [ print rejoin [ "--" newline "Test " first ts " "
        either not error? err: try [ 
            res: either equal? got: do second ts exp: third ts 
                [ "PASSED :)" ]
                [ rejoin [ " FAILED, expected: " exp ", got: " got ]]
        ] [ res ] [ join "Failed with ERROR: " disarm err ]]]]

; let's do some testing

run-tests [ 
    sum [ + 1 1 ] 2  
    notsum [ + 100 1 ] 200 
    zerodiv [ / 100 0 ] 1 
    file [ read %somefile ] false ]

---

Disclaimer: I am not a REBOL guru, nor a CS. If anyone has any comments about what I did wrong, he/she is welcome to correct me.

---

Update: I talked to the Lisp-er colleague Simon and he said that article "The power of Lisp macros" isn't too good at showing the *power of* Lisp macros. He also said rebol examples here are very clean and cool. On the other side a Rebol-er said that he feels sorry for me if this is how I format the Rebol code ;))

Filed under  //   lisp   macros   rebol  

Flash is dead and other wet dreams

I know every word written here is a waste of time because you can't really change the mind of fan/hate-boys. And why would I even want to. I just need to express few observations, so I don't have to process them any more. I hope I will be able to keep it short.

The pattern

The general pattern observed on twitter, reddit, HN or blogs goes like this:

"bla bla HTML5 video bla" -- "Yee! Flash is dead!!! Die MF die!!"

"bla bla Canvas techdemo bla" -- "Yee! Flash is dead!!! Die MF die!!"

"bla bla websockets bla" -- "Yee! Flash is dead!!! Die MF die!!"

"bla bla full screen bla" -- "Yee! Flash is dead!!! Die MF die!!"

"bla bla bla" -- "Yee! Flash is dead!!! Die MF die!!"

So HTML on some browsers is 50% as capable as Flash was X years back?

  • Input?
  • Sound?
  • Camera?
  • Mic?

Flash the Proprietary

Let's look at one tweet:

"that's exactly why proprietary crap does not belong on web... This is the best decision Apple made sice iPod. Down with Flash!"

It's funny to see the proprietary crap, Apple and iPod in one line :). Just few weeks ago we had Open Source and Proprietary platforms. And now, thanks to Apple we have Open Source, Proprietary and brand new Locked-In platforms.

Flash sure is proprietary, as is everything that is not public domain or open sourced, but how closed it really is:

  • Flash had a Open Sourced AS2 compiler MTASC for a looong time (made by Nicolas Cannasse)
  • Nicolas also made a MTASC successor HaXe, a very interesting project on it's own, that I used to make my flash games
  • Even REBOL, a not so massively used language has a Flash dialect, with which game Machinarium was built by
  • There are numerous tools to pack resources into SWF-s  (it started with swfmill)
  • Adobe open sourced their actual AS3 compiler (written in Java) when it released Flex, so even if you don't use community compilers you can create flash apps with official free and open source tool.
  • There is much used Red5 Flash server that does (realtime) Audio/Video Streaming, Remoting, Shared objects (also HaxeVideo)
  • There is also official SWF file format specification here

This is the huge list of OS comunities/projects and momentum behind flash platform.

I always prefer Open to Closed. It's funny how naively people think that proprietary multimedia plugin (that sadly so far hasn't got an alternative) for some reason really doesn't belong on the web, while the essential part of "the web" ~the serverside~ is not only closed source, but runs on computers out of your control, and even controls your data. OK, I am too critical, some web services ****even**** let you export your data.

Why the hate?

I was to some degree an indie game developer. Back in the days we indie developers hated Flash. It came and disrupted our world. It made our in blood and tears built OpenGL/DirectX c++ based game engines slowly more and more irrelevant. It was scriptish (although AS1 was a total hack of a language), cross-platform and it f*** ran inside a browser!!

But I couldn't understand, until I recently talked to fellow programmer, why anybody else would hate Flash:

  • Mac users hate flash
    • because it never worked well on Mac (100% cpu, crashes)
  • Web-folks (bloggers, tweeps..) hate flash
    • because it's only for online video and we have that now (almost)
    • because it's used in aggressive banner ads
  • Web-developers hate flash
    • because they don't have a clue how some people made some awesome things with it
    • because it's not their python/ruby/.. and doesn't have MVC
    • because the whole-website-in-flash is a REALL REALLY bad idea

Now consider, that most of the fancy vocal modern-geeky people are more or less web-developers that blog and twitt and of course use Macs.

---

This is getting too long. I will write another part next time.

Rebol notes, when code is data

I render PDF invoices for Cebelca.biz using RebHaruPDF. Right now, I'm adding an option of multiple invoice variants. To give me full flexibility each variant will be a pack of code that will turn input data into invoice. Looking at the code I noticed that I do a pre-processing of most data items before calling the PDF rendering functions. It's obvious that to reduce the code size of PDF variants I want to move all this pre-processing out of variant code.

Quite an big amount of data goes in. These are the key/value dictionaries of data that comes in:

head - invoice header data
body - invoice body data (list of dictionaries)
settings - pdf-related settings of user
imgs - logo and images related data and settings
my-comp - data about user's company that belongs into invoice

After looking at code I noticed that most pre-processing takes just 1 function and function has the returning data value as input.

 

Do it yer simply & manually

We need to deliver, no time to play! So let's just swallow our pride and prepare for some copy pasting:

c-head: copy head
c-head/doc-id: encode-for-pdf head/doc-id
c-head/inv-location: encode-for-pdf head/inv-location
c-head/date-to-pay: format-date-nice head/date-to-pay
c-head/date-served: format-date-nice head/date-served
...

Huh.. but noise to information ratio is extremely high here! Imagine I'll be making 30-50 lines like that. Pure pain of ugliness and a nest of stupid bugs and typos that you then have to solve.

 

Bring in the Rebol machinery

Looking at the specific case I saw that while much much better even apply-to that I already had implemented looks noisy:

apply-to head 'h [
  doc-id: [ encode-for-pdf h/doc-id ]
  inv-location: [ encode-for-pdf h/inv-location ]
  date-to-pay: [ format-date-nice h/date-to-pay ]
  date-served: [ format-date-nice h/date-served ]
]

So after few minutes of playing in Rebol console I came out with this usage:

apply-func head [
   doc-id: encode-for-pdf
   inv-location: encode-for-pdf
   date-to-pay: format-date-nice
   date-served: format-date-nice
 ]

Function apply-func that does this is:

apply-funcs: func [ d f /local r n ] [ r: copy d 
  forskip f 2 [ 
    r/(n: to-word first f): do reduce [ second f select d n ] 
  ] r 
]

Let's see the short session from Rebol shell:

>> d: copy [ name "jimny" age 30 surname "Metko" ]
== [name "jimny" age 30 surname "Metko"]
>> c: copy [ name: uppercase age: inc ]
== [name: uppercase age: inc]
>> apply-funcs d c
== [name "JIMNY" age 31 surname "Metko"]


Add the powers of FP

You maybe noticed that body is a list of dictionaries. Does this mean we'll make it all ugly again? No. we use apply-func with a FP classic map:

mapeach 'line body [
  appy-func line [
    title: encode-for-pfd
    price: format-money-nice
    qty: format-num-nice
  ]
]

--

And people look at me strangely when I say that I love REBOL and pragmatic FP.

Q: So what is that second argument of apply-func.. code or data? :)

Filed under  //   code is data   functional programming   rebol  

About

Janko is an instance of human species (or so it assumes). It thinks it's something special, but so do the other 6.791 billion of its kind.

Twitter