<commit_before>---
layout: post
title: Joy of OCaml - Part III(Unfinished post)
published: true
---
In this installment of the series there is OCaml functional code that shows a 'Game of life' implementation. As usual I will explain the functional part of the code. I specifically point out
functional pieces because there is indispensable boilerplate code needed because I use _lablgtk_ which is a UI toolkit. And unfortunately this toolkit uses the Object-oriented parts of OCaml.

Did I mention that OCaml is a practical functional language ? Unlike Haskell it includes many imperative constructs and OO features. I do not explain the OO part of it because that is not the focus of this series.

{% highlight OCaml %}
{let evolve area (backing:GDraw.pixmap ref) x y width height= 
          let update_rect = Gdk.Rectangle.create ~x ~y ~width ~height in
          !backing#set_foreground (`RGB (154*256, 205*256, 50*256));
          !backing#rectangle ~x ~y ~width ~height ();
          area#misc#draw (Some update_rect);
;;% endhighlight %}

{% highlight OCaml %}

let drawrect area (backing:GDraw.pixmap ref)= 
let rec loop1 m y=
  match m with
    | m when m < 5 ->
      (let rec loop x  n =
        match n with
        | n when n < 5 ->
          let x = x + 20 in
          let width, height = 20,20 in
          evolve area backing x y width height;
          (*Printf.printf "%3d %3d\n" x y;*)
          loop x   (n + 1)
        | n when n >= 5 -> loop1 (m + 1) (y + 20)
      in loop 0  0)
   (* when m >= 5 *)  
    | m when m >= 5 ->  ()
in loop1 0 0
;;
{% endhighlight %}

{% highlight OCaml %}
(* Backing pixmap for drawing area *)
let backing = ref (GDraw.pixmap ~width:200 ~height:200 ())
{% endhighlight %}

% highlight OCaml %}
{(* Create a new backing pixmap of the appropriate size *)
let configure window backing ev =
  let width = GdkEvent.Configure.width ev in
  let height = GdkEvent.Configure.height ev in
  let pixmap = GDraw.pixmap ~width ~height ~window () in
  pixmap#set_foreground `WHITE;
  pixmap#rectangle ~x:0 ~y:0 ~width ~height ~filled:true ();
  backing := pixmap;
  true
;;
{% endhighlight %}

{% highlight OCaml %}<commit_msg>Fixed tag highlight issue - Causes build failure<commit_after>---
layout: post
title: Joy of OCaml - Part III(Unfinished post)
published: true
---
In this installment of the series there is OCaml functional code that shows a 'Game of life' implementation. As usual I will explain the functional part of the code. I specifically point out
functional pieces because there is indispensable boilerplate code needed because I use _lablgtk_ which is a UI toolkit. And unfortunately this toolkit uses the Object-oriented parts of OCaml.

Did I mention that OCaml is a practical functional language ? Unlike Haskell it includes many imperative constructs and OO features. I do not explain the OO part of it because that is not the focus of this series.

{% highlight OCaml %}
{let evolve area (backing:GDraw.pixmap ref) x y width height= 
          let update_rect = Gdk.Rectangle.create ~x ~y ~width ~height in
          !backing#set_foreground (`RGB (154*256, 205*256, 50*256));
          !backing#rectangle ~x ~y ~width ~height ();
          area#misc#draw (Some update_rect);
;;% endhighlight %}

{% highlight OCaml %}

let drawrect area (backing:GDraw.pixmap ref)= 
let rec loop1 m y=
  match m with
    | m when m < 5 ->
      (let rec loop x  n =
        match n with
        | n when n < 5 ->
          let x = x + 20 in
          let width, height = 20,20 in
          evolve area backing x y width height;
          (*Printf.printf "%3d %3d\n" x y;*)
          loop x   (n + 1)
        | n when n >= 5 -> loop1 (m + 1) (y + 20)
      in loop 0  0)
   (* when m >= 5 *)  
    | m when m >= 5 ->  ()
in loop1 0 0
;;
{% endhighlight %}

{% highlight OCaml %}
(* Backing pixmap for drawing area *)
let backing = ref (GDraw.pixmap ~width:200 ~height:200 ())
{% endhighlight %}

{% highlight OCaml %}
{(* Create a new backing pixmap of the appropriate size *)
let configure window backing ev =
  let width = GdkEvent.Configure.width ev in
  let height = GdkEvent.Configure.height ev in
  let pixmap = GDraw.pixmap ~width ~height ~window () in
  pixmap#set_foreground `WHITE;
  pixmap#rectangle ~x:0 ~y:0 ~width ~height ~filled:true ();
  backing := pixmap;
  true
;;
{% endhighlight %}

{% highlight OCaml %}
{'fraction_non_alphanumeric': 0.09972552607502287, 'fraction_numerical': 0.02241537053979872, 'mean_word_length': 2.971843778383288, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2113756', 'n_tokens_mistral': 1472, 'n_tokens_neox': 1322, 'n_words': 580}