Skip to content

Instantly share code, notes, and snippets.

@theanalyst
Last active November 23, 2018 14:02
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theanalyst/8903086 to your computer and use it in GitHub Desktop.
Save theanalyst/8903086 to your computer and use it in GitHub Desktop.
Hy, Python & C play nicely with each other
#include "fibs.h"
long fib(int n)
{
if (n == 0)
return 1;
else if (n == 1)
return 1;
else
return fib(n-1) + fib(n-2);
}
#ifndef FIBS_H
#define FIBS_H
#include <stdio.h>
long fib(int n);
#endif
(import [cffi [FFI]])
(def ffi (FFI))
(.cdef ffi "
long fib(int n);
")
(defreader b [word] `(.encode ~word "UTF-8"))
(let [[kwargs {#b"sources" [#b"fibs.c"]
#b"include_dirs" [#b"."]}]
[lib (apply ffi.verify ["
#include <fibs.h>
#include <stdio.h>
long fib(int n);"]
kwargs)]]
(print (.fib lib 40)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment