Skip to content

Instantly share code, notes, and snippets.

View ruxo's full-sized avatar

Ruxo ruxo

  • Bangkok
View GitHub Profile
@ruxo
ruxo / gist:d86a1cf7128784f89963
Last active February 9, 2023 23:15
Split sequence into groups
// code from https://stackoverflow.com/questions/6736464/split-seq-in-f/6737659#6737659
let splitBy f input =
let i = ref 0
input
|> Seq.groupBy (fun x ->
if f x then incr i
!i)
|> Seq.map snd
@ruxo
ruxo / OptionParser.fs
Last active February 9, 2023 23:15
F# Command line parser, inspired by NDesk options lib.
module RZ.OptionParser
open System
open System.Collections.Generic
type Handler<'ctx> = 'ctx -> string -> 'ctx
let fst3 (x,_,_) = x
let snd3 (_,x,_) = x
let thd (_,_,x) = x
let flip f a b = f b a
type Car = {
name: string
horsepower: int
dollar_value: double
in_stock: bool
}
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
@ruxo
ruxo / promise.js
Created August 4, 2015 07:04
[sketch] Promise as functor in Javascript
var Promise__map = function(g){ // g: a -> b
var _f = function(){};
var h = function(f){ // h : (b -> c) -> 0
_f = f;
};
this.fp(function(a){
var b = g(a);
_f(b);
return b;
});
@ruxo
ruxo / ObservableExtension.cs
Last active August 29, 2015 14:27
Allow reactive pattern in C# with Observable pattern.
using System;
namespace RZ.Extensions{
public class DisposableProxy : IDisposable{
public static readonly IDisposable Dummy = new DisposableProxy(delegate {});
readonly Action dispose;
public DisposableProxy(Action dispose){
this.dispose = dispose;
}
public void Dispose(){
@ruxo
ruxo / NBString.fs
Last active February 9, 2023 23:15
String Extensions
module RZ.String
/// <summary>
/// New Non-blank string
/// </summary>
type NBString = NBString of string
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module NBString =
let from s =
if String.IsNullOrWhiteSpace s then invalidArg "s" "String cannot be blank or empty"
@ruxo
ruxo / guessing_game.fs
Last active February 11, 2023 16:57
F# IO monad
open System
open System.Runtime.CompilerServices
#nowarn "46" // no warning for using reserved word "pure"
type IOError<'T> = Result<'T, exn>
type IO<'T> = unit -> IOError<'T>
exception UnwrapError of obj
type Option<'a> with
@ruxo
ruxo / pair.fs
Last active February 9, 2023 23:14
Pairs useful operations
module RZ.Pair
let inline ofSame a = (a,a)
let inline of' a b = (a,b)
let inline with' b a = (a,b)
let inline sndWith a b = (a,b)
let inline call f (a,b) = f(a,b)
let inline map f (a,b) = (f a, f b)
let inline fapply (f,g) x = (f x, g x)
let inline cross (a,b) x = ((a,x), (b,x))
@ruxo
ruxo / build-browserify.js
Last active September 14, 2015 05:35
Browserify with Gulp
'use strict';
var gulp = require('gulp');
var gutil = require('gulp-util');
var source = require('vinyl-source-stream');
var R = require('ramda');
// Browserify example from: https://github.com/gulpjs/gulp/blob/master/docs/recipes/fast-browserify-builds-with-watchify.md
var browserify = require('browserify');
var watchify = require('watchify');
@ruxo
ruxo / SeqApplicative.fs
Last active February 9, 2023 23:14
F# Applicative Functor
// ref: http://bugsquash.blogspot.com/2010/12/zipping-with-applicative-functors-in-f.html
module SeqApplicative =
let puree v = Seq.initInfinite (fun _ -> v)
let (<*>) f a = Seq.zip f a |> Seq.map (fun (f', a') -> f' a')
let (<!>) f a = puree f <*> a