Last active
November 23, 2018 14:02
-
-
Save theanalyst/8903086 to your computer and use it in GitHub Desktop.
Hy, Python & C play nicely with each other
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef FIBS_H | |
#define FIBS_H | |
#include <stdio.h> | |
long fib(int n); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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