Skip to content

Instantly share code, notes, and snippets.

View msakai's full-sized avatar

Masahiro Sakai msakai

View GitHub Profile
module CohensKappa where
import Data.Hashable
import qualified Data.HashMap.Strict as HashMap
-- | Cohen's kappa coefficient (κ)
--
-- https://en.wikipedia.org/wiki/Cohen%27s_kappa
cohensKappa :: (Hashable c, Fractional a) => [(c,c)] -> a
cohensKappa xs = (po - pe) / (1 - pe)
from typing import Literal, Optional, Tuple, overload
@overload
def f(with_extra_info: Literal[False]) -> Tuple[int, int]: ...
@overload
def f(with_extra_info: Literal[True]) -> Tuple[int, int, str]: ...
# This overload is necessafy for type checking the last `f(with_extra_info)`.
@overload
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
-- https://en.wikipedia.org/wiki/Okapi_BM25
module OkapiBM25
( Database
, mkDatabase
, query
) where
import psycopg2
dsn = "postgresql://user:password@localhost:5432/"
db_name = "test_db"
conn = psycopg2.connect(dsn)
try:
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
with conn.cursor() as cur:
import Control.Monad
import Turtle
main :: IO ()
main = sh $
forM_ [(1::Int)..2] $ \i -> do
j <- select [(1::Int)..3]
liftIO $ print (i, j)
{-
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module QueryInterface where
-- Maybe (GHC.Exts.DictBox a)
data QueryIntefaceResult a where
{-# OPTIONS_GHC -Wall #-}
-----------------------------------------------------------------------------
-- |
-- Module : RWLock
-- Copyright : (c) Masahiro Sakai 2023
-- License : BSD-3-Clause
--
-- Simple implement of various variants of RWLocks.
--
{-# OPTIONS_GHC -Wall #-}
-----------------------------------------------------------------------------
-- |
-- Module : FairLock
-- Copyright : (c) Masahiro Sakai 2023
-- License : BSD-3-Clause
--
-- Simple Lock implemented using STM.
-- When multiple threads are blocked on a Lock, they are woken up in FIFO order.
@msakai
msakai / Main.hs
Created April 2, 2023 03:47
GHC creates too many windows threads
module Main (main) where
import Control.Concurrent.Async
import Control.Concurrent
import Control.Exception
import Control.Monad
import Data.IORef
import Foreign.C
import System.Random.MWC
import sys
for mname, m in sys.modules.items():
fname = getattr(m, '__file__', None)
if fname is not None and fname.endswith('.so'):
print(f"{mname}: {fname}")