Skip to content

Instantly share code, notes, and snippets.

@fxfactorial
Last active January 19, 2016 07:12
Show Gist options
  • Save fxfactorial/573065eb4be7296b3ef9 to your computer and use it in GitHub Desktop.
Save fxfactorial/573065eb4be7296b3ef9 to your computer and use it in GitHub Desktop.
OCaml presentation, download this one file and open in any browser.
<!doctype html>
<html lang="en">
<!--
The MIT License (MIT)
Copyright (c) 2015 Tom Panning
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<head>
<meta charset="utf-8">
<title>OCaml Presentation Mon Jan 18, 2016</title>
<meta name="author" content="Edgar Aroutiounian">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet"
href="http://cdn.jsdelivr.net/reveal.js/3.0.0/css/reveal.min.css">
<link rel="stylesheet"
href="http://cdn.jsdelivr.net/reveal.js/3.0.0/css/theme/black.css"
id="theme">
<!-- Code syntax highlighting -->
<link rel="stylesheet"
href="http://cdn.jsdelivr.net/reveal.js/3.0.0/lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'http://cdn.jsdelivr.net/reveal.js/3.0.0/css/print/pdf.css' : 'http://cdn.jsdelivr.net/reveal.js/3.0.0/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="http://cdn.jsdelivr.net/reveal.js/3.0.0/lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<p> OCaml Meetup, San Francisco</p>
<p> A Survey of OCaml for new and experienced
programmers </p>
<img src="mixrank-meetup-ocaml-01.png"></img>
<p>
<small>By <a href="http://twitter.com/edgararout">@edgararout</a></small>
</p>
</section>
<section>
<h1> About Me</h1>
<p> My name is Edgar Aroutiounian, I'm a programmer at
MixRank </p>
<ul>
<li> Have been actually paid for functional programming. </li>
<li>First introduced to OCaml in compiler's class at
Columbia University, but ignored it after that.
</li>
<li> Found it again when I started looking for a new
language that was fast, type safe, not verbose, not
Haskell.
</li>
<li>Now an OCaml fantatic, first non trivial OCaml
project was this space invaders
game
</li>
<img src="space_invader.gif"></img>
</ul>
</section>
<section>
<h2>What's to love</h2>
<ol>
<li> OCaml is a <em>real</em> functional programming
language; data is immutable by default and you can define
control flow by using functions. </li>
<li> You can run OCaml interactively, compile to JavaScript,
compile to bytecode, native code</li>
<li> No noisy type declarations everywhere</li>
<li> Emacs/Vim support is fantastic, merlin is a magician
after all</li>
<li> Standard Unix tools work well with it, gdb, etc </li>
<li> Monads when you need them, not forced on you.</li>
<li> Understandable execution model </li>
<li> On tons of platforms</li>
<li> Pragmatic, but never too far from research community </li>
</ol>
</section>
<section>
<h2> Famous users </h2>
<ul>
<li>OCaml is used at Jane Street, come to the next meetup to get
this t-shirt I'm wearing </li>
<li> Bloomberg, they just released another OCaml to
JavaScript compiler </li>
<li> Facebook, multitude of tools like flow and pfff </li>
<li> Ahrefs, entire backend is OCaml</li>
<li> Citrix, no idea but OCaml</li>
<li> Mirage OS, unikernel implemented in OCaml </li>
<li> MixRank let me write an iPhone port forwarder in
OCaml </li>
</ul>
<p> And of course there's more I haven't mentioned </p>
</section>
<section>
<h2>Jargon Crash course</h2>
<ul>
<li> ocamlopt is the native code compiler, ocamlc is
bytecode compiler, ocaml is the plain repl</li>
<li>utop is an enchanced repl, great for interactive
use </li>
<li>opam is the package manager</li>
<li> merlin provides code completion and typechecking in
your text editor </li>
</ul>
<p>...and there's much more, I recommend reading my blog
post <a href="http://hyegar.com/blog/2015/10/19/so-you're-learning-ocaml">here</a>
it will get you up to speed on everything OCaml tooling ecosystem
</p>
</section>
<!-- Start off with easy stuff -->
<section>
<h2>Code!</h2>
<p> Let's start with some easy stuff, sum up only the odds </p>
<pre>
<code class="ocaml">
List.fold_left begin fun running item ->
if item mod 2 = 0
then running
else running + item
end 0 [3;2;4;6;5;7;8;0;1];;
</code>
</pre>
</section>
<section>
<p> Okay that nice, how about some real code... </p>
<p> Real code will require the use of some libraries, </p>
<pre>
<code class="ocaml">
let rec walk_and_action action node =
if Sys.is_directory node
then (Sys.readdir node
|> Array.to_list
|> List.map (Filename.concat node)
|> Lwt_list.iter_p (walk_and_action action))
else action node
</code>
</pre>
<p> Explanation: Go over a directory, apply a function to a
file in the directory, if you find any directory then
recurse on it, btw do this all in parallel </p>
</section>
<section>
<p> And now something nontrivial ... </p>
<pre>
<code class="ocaml">
open Lwt.Infix
let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 5000)
let cleanup l : Yojson.Basic.json list =
l |> List.map begin fun a_line ->
let chopped =
Stringext.full_split a_line ~on:' ' |>
List.filter begin fun str ->
if str = " " then false else true
end |>
Array.of_list in
`Assoc [("USER", `String chopped.(0));
("PID", `Int (int_of_string chopped.(1)));
("COMMAND", `String chopped.(10))]
end
let program =
let server = Lwt_io.establish_server addr begin fun (ic, oc) ->
let command = Lwt_process.shell "ps aux" in
let stream = Lwt_process.pread_lines command in
(Lwt_stream.to_list stream >>= fun ps_output ->
let cleaned = cleanup (List.tl ps_output) in
cleaned |> Lwt_list.iter_s begin fun the_json ->
let to_string = Yojson.Basic.pretty_to_string the_json in
Lwt_io.write_from_string_exactly oc to_string 0 (String.length to_string - 1)
>>= fun _ -> Lwt_io.write_char oc '\n'
end
>>= fun _ -> Lwt_io.close oc)
|> Lwt.ignore_result
end
in
(* Lwt.wait gives you back (thread, waker) *)
(* since we don't want the server to ever stop *)
(* we throw away the wakener *)
Lwt.wait () |> fst
let () =
Lwt_main.run program
</code>
</pre>
<p> Explanation: Create a simple server that will give back
JSON that describes the some information about PS on the
server </p>
</section>
<section>
<p> Shameless plug, ...compile and run on node </p>
<pre>
<code class="ocaml">
open Nodejs
let _ =
Fs.create_read_stream "code.ml" >|>
Zlib.create_gzip () >|>
Fs.create_write_stream "NEWCODE_TEST.ml"
</code>
</pre>
</section>
<section>
Out of time, any questions?
</section>
</div>
</div>
<script src="http://cdn.jsdelivr.net/reveal.js/3.0.0/lib/js/head.min.js"></script>
<script src="http://cdn.jsdelivr.net/reveal.js/3.0.0/js/reveal.min.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'http://cdn.jsdelivr.net/reveal.js/3.0.0/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'http://cdn.jsdelivr.net/reveal.js/3.0.0/plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'http://cdn.jsdelivr.net/reveal.js/3.0.0/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'http://cdn.jsdelivr.net/reveal.js/3.0.0/plugin/highlight/highlight.js', async: true, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'http://cdn.jsdelivr.net/reveal.js/3.0.0/plugin/zoom-js/zoom.js', async: true },
{ src: 'http://cdn.jsdelivr.net/reveal.js/3.0.0/plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment