Skip to content

Instantly share code, notes, and snippets.

// So called van Laarhoven lenses, named after their discoverer, have a number
// of nice properties as explained by Russell O'Connor:
//
// http://r6.ca/blog/20120623T104901Z.html
//
// Unfortunately their typing (in Haskell)
//
// type Lens s t a b = forall f. Functor f => (a -> f b) -> (s -> f t)
//
// seems to be well outside of what can be achieved in F#.
@atifaziz
atifaziz / MSubstitute.bas
Last active June 29, 2016 12:40
SUBSTITUTE-like VBA function for Excel except works with a range of texts to find and replace
Option Explicit
' MSubstitute is like the SUBSTITUTE function of Excel except it allows
' multiple substitutions at once. It replaces all occurrences of texts in the
' first column of SubstitutionsRange in Text with the corresponding text in
' second column.
Public Function MSubstitute(ByVal Text As String, _
ByVal SubstitutionsRange As Range, _
Optional ByVal IgnoreCase As Boolean = True) As String
@zeux
zeux / diaenum.fs
Created April 22, 2012 22:34
F# generic enumeration helper
// helpers to iterate IDiaEnum* objects
let inline toSeq (o: ^T) =
let e = { new IEnumerable with member this.GetEnumerator () = (^T: (member GetEnumerator: unit -> _) (o)) }
if true then
Seq.cast e
else
// Dummy expression to constrain return type to that of o.Item
seq [| (^T: (member Item: _ -> _) (o, Unchecked.defaultof<_>)) |]
module Event =
open System.Threading
let concat (source: IEvent<IEvent<_,_>>) =
let event = new Event<_>()
source.Add (Event.listen event.Trigger)
event.Publish
let collect f =
Event.map f >> concat
@thinkbeforecoding
thinkbeforecoding / Zip computation
Created January 20, 2014 23:30
This is an example of the Zip (applicative functor) experimental extension to Computation Expression. using for .. and ... do, sources are merges using the .Merge(xs,f) method. If Select is defined and the rest of the expression is a simple projections, the result is Select(Merge(xs,ys), projection) else it is Bind(Merge(xs,ys), rest of the expr…
open System
type ZipBuilder() =
member t.Zip(xs,ys) = List.zip xs ys
member t.For(xs,f) = List.collect f xs
member t.Yield x = [x]
member t.Select(xs,f) =
List.map f xs
member t.Zero() = []
@rgrempel
rgrempel / Main.purs
Created June 30, 2016 22:41
Alternative approach to effect types in Purescript
module Control.Monad.Eff.Alt where
import Control.Bind (bind)
import Control.Monad.Eff.Exception (Error)
import Partial.Unsafe (unsafeCrashWith)
import Prelude (liftA1, class Functor, class Applicative, class Monad, ap, class Apply, class Bind, class Show, Unit)
-- This illustrates an alternative way to use the type system to model
-- effects, where the effects type is a "regular" type, and the effects
@panesofglass
panesofglass / apply.fsx
Last active August 21, 2017 22:34
F# async applicative using a custom operation
open System
type FSharp.Control.AsyncBuilder with
[<CustomOperation("and!", IsLikeZip=true)>]
member __.Merge(x, y, f) =
async {
let! token = Async.CancellationToken
let! x' = Async.StartChildAsTask x
let! y' = Async.StartChildAsTask y
do System.Threading.Tasks.Task.WaitAll([|x';y'|], cancellationToken = token)
type First =
{ Name: string
Items: string list }
type Second =
{ Name: string
Numbers: int list }
open FSharpPlus
@zecl
zecl / Program.fsx
Created March 31, 2013 13:03
FsControl (https://github.com/gmpl/FsControl) を拡張してお遊び
#r @"bin\Debug\FsControl.Core.dll" // from https://github.com/gmpl/FsControl
module Monad =
open FsControl.Core.Abstractions
let do' = new Monad.DoNotationBuilder()
module MonadPlus =
open Monad
open FsControl.Core.Abstractions.MonadPlus
// Install http://www.nuget.org/packages/Fleece
open Fleece
open FSharpPlus
let json = """{
"1": "one",
"2": "two",
"3": "three"
}"""