This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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&; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Either Monad for Nim! | |
type Either[Right, Left] = object | |
case success: bool | |
of true: | |
right: Right | |
of false: | |
left: Left | |
# or unitEither for right |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Maybe Monad for Nim! | |
type | |
SomePointer = ref | ptr | pointer | proc | |
type Maybe[T] = object | |
when T is SomePointer: | |
val: T | |
else: | |
val: T |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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)` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
NewerOlder