Skip to content

Instantly share code, notes, and snippets.

mrange

Block or report user

Report or block mrange

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@mrange
mrange / cool.cpp
Created May 26, 2019
Cool stuff todo with C++
View cool.cpp
#include <cstdio>
#include <string>
#include <type_traits>
namespace
{
template<int i>
struct fac
{
static constexpr int value = i*fac<i-1>::value;
View lenses.fs
module FsLenses =
type [<AbstractClass>] Lens<'S, 'T, 'A, 'B>() =
class
abstract View: 'S -> 'A
abstract Over: ('A -> 'B) -> 'S -> 'T
abstract Set : 'B -> 'S -> 'T
end
module Lens =
let inline view (l : Lens<_, _, _, _>) s = l.View s
@mrange
mrange / church_list.fs
Last active Jun 1, 2019
ChurchLists in F#
View church_list.fs
type [<AbstractClass>] ChurchList<'T>() =
class
abstract RunList: ('T -> 'S -> 'S) -> 'S -> 'S
end
module ChurchList =
module Details =
let inline adapt f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt f
let inline invoke f v s = (f : OptimizedClosures.FSharpFunc<_, _, _>).Invoke (v, s)
View ps.py
# (v -> Boolean) -> Boolean
class State:
pass
def empty():
return lambda r: True
def singleton(v):
return lambda r: r(v)
@mrange
mrange / unique_ptr.cpp
Created May 11, 2019
unique_ptr example
View unique_ptr.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <memory>
#include <type_traits>
namespace
{
struct hello
{
std::unique_ptr<FILE, decltype(&fclose)> create_file (char const * file)
@mrange
mrange / out_ptr.cpp
Created May 11, 2019
example on out pointers around unique_ptr
View out_ptr.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <memory>
#include <type_traits>
namespace
{
template<typename T>
struct unique_out_ptr
{
@mrange
mrange / on_exit.cpp
Created May 11, 2019
on_exit - useful with C Apis
View on_exit.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <memory>
#include <type_traits>
namespace
{
template<typename TAction>
struct scope_guard
{
@mrange
mrange / README.md
Last active Jun 10, 2019
[F#] Implementing Coroutines (async/await) using continuations
View README.md

[F#] Implementing Coroutines (async/await) using continuations

Coroutines or async/await is not a new concept as it was named in 1958 by Melvin Conway. Coroutines had support in languages such as Simula(1962), Smalltalk(1972) and Modula-2(1978) but I think one can argue that coroutines came into mainstream with C# 5(2012) when Microsoft added async/await to C#.

The problem with subroutines

A thread can only execute a single subroutine at a time as a subroutine starts and then runs to completion. In a server environment subroutines can be problematic as in order to service connections concurrently we would usually would start a thread per connection, this was the idiom a few years ago. A thread depending on the operating system and settings needs about 1 MiB of user stack space meaning 1,024 threads needs 1 GiB of for just user stack space. In addition there's the cost of kernel stack space, book-keeping and scheduling of threads. This drives cost of VM/hardwa

View coroutines.fs
module MinimalisticCoroutine =
type [<Struct>] Coroutine<'T> = Co of (('T -> unit) -> unit)
module Coroutine =
open FSharp.Core.Printf
open System
open System.Diagnostics
open System.Threading
@mrange
mrange / FsTranducers.fs
Created Apr 25, 2019
F# Transducers test
View FsTranducers.fs
module Common =
let inline adapt f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt f
let inline invoke f s v = (f : OptimizedClosures.FSharpFunc<_, _, _>).Invoke (s, v)
module MinimalisticTransducers =
// Disable Tiered compilation using powershell: $env:COMPlus_TieredCompilation=0
let inline (^>) l r = l r
let inline (>->) l r v = l (r v)
You can’t perform that action at this time.