Skip to content

Instantly share code, notes, and snippets.

View shouya's full-sized avatar

shouya

  • Open for Hire!
  • UTC+9
View GitHub Profile
@shouya
shouya / ternary.rs
Last active February 29, 2024 07:24
// playground for ternary arithmetic
// {-1, 0, 1} are represented by {0b10, 0b00, 0b01} respectively
// 0b11 is unused.
use prettytable::table;
use rand::seq::SliceRandom;
fn main() {
test_add_1();
test_add_32(1000);
use std::{fs::File, io::Read, path::Path};
use pathfinding::prelude::dijkstra;
const PART1_MAX_STRAIGHT: usize = 3;
const PART2_MIN_STRAIGHT: usize = 4;
const PART2_MAX_STRAIGHT: usize = 10;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum Dir {
defmodule Structural do
@moduledoc """
This module is build around this invariance, which I call
"reconstruction property":
for all m_1, m_2: maps, let m_common = intersect(m_1, m_2),
then merge(m_common, diff(m_1, m_common)) == m_1,
and merge(m_common, diff(m_2, m_common)) == m_2.
This allows us to efficiently store a list of [m_1, m_2, ..., m_K]
try do
Mix.Project.get()
catch
:exit, _ ->
Mix.install([
{:nanoid, "~> 2.0.5"},
{:jason, "~> 1.3.0"},
{:timex, ">= 0.0.0"}
])
end
@shouya
shouya / README.md
Last active March 16, 2023 05:17
Simple ChatGPT used in a Unix pipe

To use this script, you need to install the openai package using pip. You also need to set the OPENAI_API_KEY environment variable to your OpenAI API key.

For example, to ask a natural language question, you can pipe the output of ls -al to the script like this:

ls -al | ./gpt "What are the permissions of the files?"

The script will use the OpenAI GPT-3 model to generate a response to your question with a temperature of 0.2. The response will be printed to the console.


@shouya
shouya / structural.ex
Last active May 13, 2022 01:56
Deep diff/intersect/merge
defmodule Manager.Util.Structural do
@moduledoc """
This module is build around this invariance, which I call
"reconstruction property":
for all m_1, m_2: maps, let m_common = intersect(m_1, m_2),
then merge(m_common, diff(m_1, m_common)) == m_1,
and merge(m_common, diff(m_2, m_common)) == m_2.
This allows us to efficiently store a list of [m_1, m_2, ..., m_K]
@shouya
shouya / gol.org
Last active March 27, 2023 15:30

Monad

class Functor m => Monad m where
  pure :: a -> m a
  join :: m (m a) -> m a
  -- or
  bind :: (a -> m b) -> m a -> m b
@shouya
shouya / coroutine.rkt
Last active July 13, 2021 15:34
A toy coroutine implementation (implemented premitives: spawn, join, yield, sleep, select)
#lang racket
;;; global states
(define task-queue '())
(define joiners '())
(define curr-task-handle 'main)
(define halt (lambda () (display "not started yet")))
;;; queue-related functions
import qualified Data.Text as T
import Control.Monad
import Data.List (unfoldr, intercalate)
import Data.Bits (testBit)
import Data.Function ((&))
data Node = Sat -- all 1
| Unsat -- all 0
| Mixed Node Node -- mixed (left, right)
deriving (Show)
@shouya
shouya / kubectl-exp
Last active June 3, 2021 08:54
Kubectl explain with syntax highlighting and cache
#!/bin/bash
KUBE_EXPLAIN_CACHE_DIR=$HOME/.kube/cache/explain
p="$1"
f="$KUBE_EXPLAIN_CACHE_DIR/$p"
if [[ -z "$p" ]]; then
echo "Usage: $0 <path>"
echo "Example: $0 deploy.spec.template.spec.volumes"