Skip to content

Instantly share code, notes, and snippets.

View jmsdnns's full-sized avatar

Jms Dnns jmsdnns

View GitHub Profile
@jmsdnns
jmsdnns / jmap.ml
Created February 3, 2025 00:14
A port scanner in Ocaml. Rudimentary form done to learn how Ocaml does networking.
module Scanner = struct
(* attempt to open a port and return an option *)
let open_port host port timeout = fun () ->
let open Lwt.Infix in
let sockaddr = Unix.ADDR_INET (Unix.inet_addr_of_string host, port) in
let socket = Lwt_unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
Lwt.catch
(* Port is open *)
(fun () ->
@jmsdnns
jmsdnns / jsonlite.ml
Created January 26, 2025 07:15
Querying a sqlite DB from Ocaml. JSON is stored in a column and selects are done on JSON values.
module UserModel = struct
module DB = Sqlite3
module JSON = Yojson.Basic
let create_table db =
let create_sql =
"CREATE TABLE IF NOT EXISTS users (" ^
"id INTEGER PRIMARY KEY, " ^
"name TEXT, " ^
"age INTEGER, " ^
@jmsdnns
jmsdnns / downloader.ml
Last active January 17, 2025 20:24
My first Ocaml program. It downloads two files at the same time using LWT.
(*
compile with:
`ocamlfind ocamlopt -thread -o downloader -linkpkg -package lwt,cohttp-lwt-unix downloader.ml`
*)
module Downloader = struct
(* download a single file *)
let dl_file url filename =
let open Lwt.Infix in
@jmsdnns
jmsdnns / main.go
Last active November 28, 2024 01:45
Simple demonstration of streaming chunks to a bubbletea TUI
package main
import (
"fmt"
"io"
"log"
"os"
"time"
"github.com/charmbracelet/bubbles/viewport"
@jmsdnns
jmsdnns / lexing_parsing.rs
Created November 14, 2024 05:45
A simple example of an EBNF parser written in Rust, without support from external crates.
// <expression ::= <term> { ("+" | "-") <term> }
// <term> ::= <factor> { ("*" | "/") <factor> }
// <factor> ::= <number> | "(" <expression> ")"
// <number> ::= <digit> { <digit> }
// <digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
use std::str::FromStr;
#[derive(Debug, PartialEq, Clone)]
pub enum Token {
@jmsdnns
jmsdnns / read_chunks.rs
Created October 9, 2024 22:28
Reading a file into chunks using Tokio's File tools
// TOTAL CHUNKS: 5
// - chunk: 5242880
// - chunk: 5242880
// - chunk: 5242880
// - chunk: 5242880
// - chunk: 1974756
use tokio::{fs::File, io, io::AsyncReadExt, io::AsyncSeekExt};
pub async fn read_chunk(file_path: &str, start: u64, end: u64) -> io::Result<Vec<u8>> {
@jmsdnns
jmsdnns / paths_w_inquire.rs
Created October 6, 2024 18:21
Creating a file path verifying type with Rust's inquire crate
use inquire::{validator::Validation, CustomType};
use std::path::{self, Path};
fn full_path(p: String) -> String {
path::absolute(p.as_str())
.unwrap()
.canonicalize()
.unwrap()
.to_str()
.unwrap()
@jmsdnns
jmsdnns / midwest.rs
Last active November 18, 2024 01:53
Trying to help my friends in the midwest feel comfortable with Rust
use std::fs::File;
enum ComeWith<T, E> {
YouBetcha(T),
Ope(E),
}
impl<T, E> From<std::result::Result<T, E>> for ComeWith<T, E> {
fn from(r: std::result::Result<T, E>) -> Self {
match r {
@jmsdnns
jmsdnns / isitfluent.py
Created September 17, 2024 06:13
Fluent interfaces technically return self or this from each chained function. Pandas returns new instances of the same type. Technically, that isn't fluent. I suspect it is meant to be indistinguishable for optimization reasons.
#!/usr/bin/env python
import pandas as pd
def getDataframe(url_table, ind):
df = pd.read_html(url_table)[ind]
return df
@jmsdnns
jmsdnns / rust_parse.log
Last active September 3, 2024 21:43
Pretty printed the way Rust's parse_macro_input! parses a simple struct. Handy to know the full thing for pattern matching.
// rust has pretty print support for printing structures. this is how rust's proc_macros
// parse the following struct:
//
// pub struct Book {
// id: u64,
// title: String,
// pages: u64,
// author: String,
// }
//