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
let uri = "https://countries.trevorblades.com/" | |
let query = {| query { | |
countries { | |
name | |
} | |
}|} | |
let send_graphql_query query = | |
let open Lwt.Infix in |
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
open Mithril | |
open Brr | |
module State = struct | |
type t = Jv.t | |
let state = Jv.obj [| ("id", Jv.of_int 0) |] | |
let get_id () = Jv.get state "id" |> Jv.to_int |
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
open Lwt.Infix | |
let add_to_table tbl str = | |
Base.(Hashtbl.set tbl ~key:str ~data:(Hashtbl.find_or_add tbl str ~default:(fun () -> 0) + 1)) | |
let stream_stdin () = | |
Lwt_stream.from (fun () -> try Lwt.return (Some (input_line stdin)) with End_of_file -> Lwt.return None) | |
let main c tbl = | |
Lwt_stream.iter_n ~max_concurrency:c (fun a -> Lwt.return (add_to_table tbl a)) (stream_stdin ());; |
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
(* A fibonacci function *) | |
let rec fib x = if x = 0 || x = 1 then 1 else fib (x - 1) + fib (x - 2);; | |
(* A function that allows for partial evaluation *) | |
let h x = let z = fib x in fun y -> y + z;; | |
(* fib 30 will only be calculated once *) | |
List.map (h 30) [1; 2; 3; 4; 5];; |
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
(* Part of the %inline expr_attrs productions *) | |
| FUN ext_attributes labeled_simple_pattern fun_def | |
{ let (l,o,p) = $3 in | |
Pexp_fun(l, o, p, $4), $2 } | |
(* the fun_def, lident_list and ext_attributes is then defined as the following *) | |
fun_def: | |
MINUSGREATER seq_expr | |
{ $2 } | |
| mkexp(COLON atomic_type MINUSGREATER seq_expr |
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
(* lowercase definition *) | |
let lowercase = ['a'-'z' '_'] | |
(* identchar definition *) | |
let identchar = ['A'-'Z' 'a'-'z' '_' '\'' '0'-'9'] | |
(* Some stuff before *) | |
| lowercase identchar * as name | |
{ try Hashtbl.find keyword_table name | |
with Not_found -> LIDENT name } |
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
# First we calculate mu_s | |
alpha = 2. | |
K_xy = rbf_kernel(xs_train, xs_test, alpha) | |
K_yy = rbf_kernel(xs_test, xs_test, alpha) | |
K_xx = rbf_kernel(xs_train, xs_train, alpha) | |
np.random.seed(20) | |
mu_s = np.matmul(K_xy.T, np.matmul(np.linalg.pinv(K_xx), (ys_train))) | |
# Second we calculate sigma_s | |
sigma_s = K_yy - np.matmul(K_xy.T, np.matmul(np.linalg.pinv(K_xx), K_xy)) |
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
from sklearn.gaussian_process.kernels import RBF | |
# First we set up the training data | |
xs_train = training_points # The points that will reign in our prediction | |
ys_train = train_temp # The corresponding temperatures for those points | |
# Then the space for which we are trying to predict | |
xs_test = np.atleast_2d(np.linspace(0, 20, 1000)).T | |
# We will use the SKLearn RBF function |
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
# Get the New York temperatures | |
n = 20 | |
new_york_temp = city_temps.loc[city_temps['City'] == "New York"][-n:] | |
temps = np.array(new_york_temp["AverageTemperature"]) | |
temps = temps / np.linalg.norm(temps) | |
# Extract some random training points from the 20 we are using (set random seed for reproducibility) | |
training_points = np.atleast_2d([1, 5, 7, 10, 14, 16, 19]).T | |
np.random.seed(20) | |
train_temp = temps[training_points] |
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
# Generate the linear evenly spaced xs and ys | |
n = 50 | |
three_xs = np.linspace(-5, 5, n) | |
three_ys = np.linspace(-5, 5, n) | |
# Generate the values for both the X and Y (two parts to the Z) | |
three_zs_a = np.array(list(map(lambda x: guass_func(x, 0, 1), three_xs))) | |
three_zs_b = np.array(list(map(lambda y: guass_func(y, 0, 2), three_ys))) | |
# Use the Kronecker product of the two https://docs.scipy.org/doc/numpy/reference/generated/numpy.kron.html |
NewerOlder