Skip to content

Instantly share code, notes, and snippets.

@ebresafegaga
ebresafegaga / main.cc
Last active February 11, 2022 12:20
Just a functional programmer playing with pointers.
#include <vector>
#include <string>
#include <iostream>
struct Node {
int element;
Node* next;
Node* prev;
};
let (>=>) p1 p2 input k = p1 input (fun input -> p2 input k)
let (>=>>) p1 p2 input = p1 input (fun input -> p2 input List.isEmpty)
let digit input k =
let rec loop input =
match input with
| [] -> k []
| x :: input when Char.IsDigit x -> loop input
| input -> k input
match input with
@ebresafegaga
ebresafegaga / excel.ml
Last active March 27, 2022 21:31
Excel Formula Leetcode problem in OCaml (https://leetcode.com/problems/design-excel-sum-formula/)
let rec range start stop = if start > stop then [] else start :: range (succ start) stop
let sum xs = List.fold_left (+) 0 xs
let (>>) f g a = a |> f |> g
module type Excel = sig
type t
val create: int -> char -> unit
val set: int -> char -> int -> unit
val get: int -> char -> int
val sum: int -> char -> string list -> int
@ebresafegaga
ebresafegaga / rank.ml
Last active March 28, 2022 12:01
Rank Transform of a Matrix Leetcode in OCaml (https://leetcode.com/problems/rank-transform-of-a-matrix/)
type constr = Equal | Less | Greater
let adjacents matrix (x,y) =
matrix
|> Array.mapi (fun ri row -> row |> Array.mapi (fun ci _col -> ri,ci))
|> Array.to_list
|> Array.concat
|> Array.to_list
|> List.filter (fun (ri, ci) -> (ri = x) <> (ci = y))
@ebresafegaga
ebresafegaga / one.cs
Created April 13, 2022 11:02
MS Codility Test
using System;
using System.Collections.Generic;
using System.Linq;
namespace interview {
class Program {
// check if the last 2 chars with c are the same
public static bool predicate (string s, char c) {
if (s.Length < 2) {
@ebresafegaga
ebresafegaga / grep.ml
Last active July 5, 2022 10:38
I'd rather write grep in OCaml. Just a small utility to search the output of `ls -R`, and then search each file. Basically searching all files in a directory for a particular "key" (or filtering some files out)
let (>>) f g a = a |> f |> g
let input_lines file =
let rec loop chan =
match input_line chan with
| line -> line :: loop chan
| exception End_of_file -> []
in
let chan = open_in file in
let result = loop chan in
close_in chan;
@ebresafegaga
ebresafegaga / add.sh
Last active July 22, 2022 16:21
little shell script
#! /bin/sh
for line in `echo add.txt | cat | awk '{print $2}'`; do
git add "$line"
done
@ebresafegaga
ebresafegaga / git.sh
Last active July 27, 2022 10:52
another little script
git status | tail -n +5 | head -n -8 | awk '{print $2}' | grep -v snarkyjs | xargs git add
@ebresafegaga
ebresafegaga / count.rs
Last active August 4, 2022 08:35
Counting sort
fn sort<const N: usize>(arr: [i32; N]) -> Vec<i32> {
let mut max = arr[0];
// Find the max of the array
for i in 0..arr.len() {
if arr[i] > max {
max = arr[i]
}
}
fn calculate_offset(c: u32, l: u32, t: &String) -> usize {
let (c, l) = (c as usize, l as usize);
// Line is 0 indexed so we don't need to skip the
// line that the cursor is currently on.
// It will be done automatically during the `take`, because
// of the 0 index.
let count = t
.split('\n')
.take(l)
.collect::<Vec<_>>()