Skip to content

Instantly share code, notes, and snippets.

View ianfun's full-sized avatar

ianfun ianfun

View GitHub Profile
@ianfun
ianfun / pty_redirect.c
Last active January 23, 2023 02:35
Linux pseudo console - Rediecting child process's stdin/stdout/stderr.
// gcc -Wall -Wextra pty_redirect.c -lutil -luring
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <pty.h>
#include <fcntl.h>
@ianfun
ianfun / calculator.py
Created January 4, 2023 08:42
Simple determinant calculator in python
def clac(l):
return (
l[0][0] * l[1][1] * l[2][2] +
l[0][1] * l[1][2] * l[2][0] +
l[0][2] * l[1][0] * l[2][1]) - (
l[2][0] * l[1][1] * l[0][2] +
l[1][0] * l[0][1] * l[2][2] +
l[0][0] * l[2][1] * l[1][2] )
print(clac(
@ianfun
ianfun / iterator.cpp
Created December 13, 2022 10:43
How to write a C++ custom iterator(which can used in std::distance ...).
#include <iterator>
#include <cstdio>
// A number interator
struct MyIterator {
using value_type = int;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using reference = int&;
@ianfun
ianfun / calc.nim
Created August 8, 2022 05:20
Simple calculator written in Nim
import std/[strutils, streams, math]
type
Token = enum
TInvalid, TEOF, TNumber, TIdentifier, TOperator, TOpen, TClose, TNewLine
Parser = object
fd: Stream
c: char # lookahead
line: int
col: int
@ianfun
ianfun / main.nim
Created August 1, 2022 14:08
Identity Monad for Nim
# identity functor and monad for Nim https://hackage.haskell.org/package/base/docs/Data-Functor-Identity.html
type Identity*[T] = object
runIdentity*: T
# return(unit) for identity
proc identity*[T](a: T): Identity[T] = Identity[T](runIdentity: a)
proc `$`*[T](a: Identity[T]): string =
"Identity " & $(a.runIdentity)
@ianfun
ianfun / main.nim
Created August 1, 2022 13:43
IO Monad for Nim
# IO Monad for Nim!
type IO*[T] = proc (): T
type EmptyTuple* = tuple[]
proc callIO*[T](f: IO[T]): auto {.inline.} = f()
proc `>>`*[A, B](a: IO[A], b: IO[B]): IO[B] =
discard callIO(a)
@ianfun
ianfun / main.nim
Created August 1, 2022 13:39
Either Monad in Nim
# Either Monad for Nim!
type Either[Right, Left] = object
case success: bool
of true:
right: Right
of false:
left: Left
# or unitEither for right
@ianfun
ianfun / main.nim
Created August 1, 2022 13:38
Maybe Monad in Nim
# Maybe Monad for Nim!
type
SomePointer = ref | ptr | pointer | proc
type Maybe[T] = object
when T is SomePointer:
val: T
else:
val: T
@ianfun
ianfun / state.nim
Created August 1, 2022 13:37
State Monad in Nim
## A functional, State monad for Nim.
## see haskell mtl package https://hackage.haskell.org/package/mtl-2.3/docs/Control-Monad-State-Lazy.html
## or wikibook https://en.wikibooks.org/wiki/Haskell/Understanding_monads/State
import std/[random]
# newtype State s a = State {runState:: s -> (a, s)}
type
State*[S, A] = proc (s: S): (A, S)
## state is a function that accept a input state, and returns `(produced result, new state)`
@ianfun
ianfun / Main.hs
Created August 1, 2022 13:36
state Monad in Haskell
module Main where
import Control.Monad as M
import System.Random
newtype State s a = State {runState:: s -> (a, s)}
instance Functor (State s) where
fmap = M.liftM
instance Applicative (State s) where