You are currently browsing the archives for the code tag.

JSlide, JavaScript slideshows

A few days ago I came across S5, a nice script for doing slideshows in the browser. However, I didn't like a few things in it, and one of them is that one presentation has to take up all the space on the screen. One nice thing I wanted to be able to do is to embed a presentation in, say, a blog article. That's how I came with JSlide…

You can watch an example here, and see how it degrades when there is no javascript here.

Here's the way to create your own presentations :

First you have to download the following files (and the prototype library) :

Next, add the following markup to your page/article/whatever :

<div style="width:400px; height:300px; margin:0 auto">
  <div class="jslide">
    <!– Meta information : each line is optional –>
    <div class="meta">
      <div class="title">[title]</div>
      <div class="author">[author]</div>
      <div class="company">[company]</div>
      <div class="date">[date]</div>
    </div>

    <!– This generates automatically the titlepage –>
    <div class="frame titlepage"> </div>

    <!– A simple slide –>
    <div class="frame">
      <h3>[title]</h3>
      [content]
    </div>

    <!– A slide, shown steps by steps –>
    <div class="frame">
      <ul class="steps">
        <li>[step 1]</li>
        <li>[step 2]</li>
        <li>[step 3]</li>
      </ul>
    </div>
  </div>
</div>

That's it. You can also add the fullscreen class to the div having the jslide class if you want the slideshow to start in fullscreen (of course, this will work for only one slideshow on the page…).

Ciml : C in OCaml

Olivier has been busy extending my previous work on inlining C in OCaml, and it is pretty cool. You can get it from the following darcs repository.

darcs get http://chadok.info/darcs/ciml/

Basically, it allows you to inline C in OCaml code, in a very fashionable way. Type conversion between OCaml and C is (almost always) automatically dealt with for base types (int, float, bool, string, etc.) and you can register and unregister custom converters. A few examples are shown in the test.ml.

letext hello (s:string) : unit =  <<
  printf("Hello %s !\n", s);
  Return();
>>
letext add (a:int) (b:int) : int =  <<
  int c;
  c = a + b;
  Return(c);
>>

(* Unregister the converters for int *)
unregister_fromval : int
unregister_toval : int

(* Register converters *)
register_fromval "Int_val" "int" : int
register_toval "Val_int" : int

The idea behind all this syntaxic sugar for inlining C code in OCaml is to provide a natural way of using C and OCaml together to whoever wants to bind C libraries to OCaml. Of course, this is still under developement, and any ideas to improve it are welcome.

Inlining C code in OCaml, part II

I finally managed to do what I was talking about this morning. There are a few awful things in the code, but it works.

Here is what one can write:

<:c<
#include <stdio.h>
>>

ext add (a:int) (b:int) : int =  <:cfun<
  int c;
  c = Int_val(a) + Int_val(b);
  CAMLreturn(Val_int(c));
>>

ext hello (s:string) : unit =  <:cfun<
  printf("Hello %s !\n", String_val(s));
  CAMLreturn(Val_unit);
>>

ext ping_pong (n:int) : int = <:cfun<
  int c = Int_val(caml_callback( *caml_named_value("fact"), n));
  printf("C : ping. fact 4 = %d\n", c);
  CAMLreturn(Val_int(c));
>>

let fact n =
  let rec fact acc = function
      0 -> acc
    | n -> fact (n*acc) (n-1)
  in
  fact 1 n

let _ =
  Callback.register "fact" fact;
  Printf.printf "%d\n%!" (add 1 5);
  Printf.printf "Ocaml : pong. fact 4 = %d\n%!" (ping_pong 4);
  hello "World";

The ext keyword is the equivalent of let to define external functions. All types must be specified (for the arguments and for the return value. Camlp4 the generates the C function, the external declaration etc.

Downloaded : 153 times
File : inlinec-0.42.tar.gz
Size: 1.4 ko

Inlining C code in OCaml

A great thing about OCaml is the possibility of creating bindings with C. The bad thing about it is that it's a pain to do. Olivier and I are currently thinking about a way to make that less painful. Here is a first draft : inlining C code in OCaml, in order to avoir multiple files.

Using camlp4, we extract the quotations containing C code and we dump that in a temporary .c file, and compile it. Here is an example:

<:c<
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/custom.h>
>>

external add: int -> int -> int = "add"
<:c<
value add(value a, value b) {
  int c;
  CAMLparam2(a,b);
  c = Int_val(a) + Int_val(b);
  CAMLreturn(Val_int(c));
}
>>

let _ =
  Printf.printf "La réponse est %d\n%!" (add 21 21);

The thing we would like to do is the following:

let add a b = <:cfun<
  int c = Int_val(a) + Int_val(b);
  CAMLreturn(Val_int(c));
>>

Which would be converted to:

external add: int -> int -> int = "add"

value add (value a, value b) {
  CAMLparams2(a,b);
  int c = Int_val(a) + Int_val(b);
  CAMLreturn(Val_int(c));
}

Downloaded : 145 times
File : inlinec.tar.gz
Size: 732 o

Prototype based PHP

Another proof of concept, using PHP this time :

<?php
class Proto {
  private $elems;
  private $proto;

  function __construct($proto = null) {
    $this->elems = array ();
    $this->proto = $proto;
  }

  function __set($name, $value) {
    $this->elems[$name] = $value;
  }

  function __get($name) {
    if ($this->elems[$name])
      return $this->elems[$name];
    else
      if ($this->proto)
        return $this->proto->$name;
      else
        return null;
  }

  function __call($name, $args) {
    if ($this->elems[$name] && is_callable($this->elems[$name]))
    {
      array_unshift($args, $this);
      return call_user_func_array($this->elems[$name], $args);
    }
    else
      return null;
  }

}
?>

The idead is completely different from the OCaml implementation. This time we use the magical methods to do whatever we want. Here is yesterday's example :

<?php
function write ($s) { echo $s; }
function method ($args,$body) { return create_function('$self,'.$args, $body); }

header('Content-Type:text/plain');

$p = new Proto;
$p->foo = 'p.foo';
$p->baz = 'p.baz';

$o = new Proto($p);
$o->foo = 'o.foo';
$o->bar = method('$f', '$f($self->baz);');

echo $p->foo, "\n";
echo $p->baz, "\n";
echo $o->foo, "\n";
$o->bar('write');
?>

There are several differences here too. First, echo is a statement, not a function, so it can't be passed to a function. This is why I use this workaround with the write function. Next, because of PHP's scoping, there is no direct access to the global variable $foo without calling global $foo;), I decided to $self as the first argument of all methodes and to bind it to the current element (this is actually how JavaScript works);

There are not much differences though…